matplotlib:最流行的Python底层绘图库,主要做数据可视化图表,名字取材于MATLAB,模仿MATLAB构建。
import random
from matplotlib import pyplot as plt
# 指的是时间: 2 4 6 8 .......24
x = range(2, 26, 2)
# 温度
y = [random.randint(10, 30) for i in range(12)] # 2 10
# 设置图形的大小;
plt.figure(figsize=(20, 8), dpi=100)
# 传递x和y的值
plt.plot(x, y)
# x轴和y轴显示的范围;
plt.xticks(x[:])
plt.yticks(range(min(y), max(y)+1))
plt.show()
# plt.savefig("./hello.png", dpi=100)
import numpy as np
from matplotlib import pyplot as plt
"""
'-' 实线样式
'--' 短横线样式
'-.' 点划线样式
':' 虚线样式
'.' 点标记
',' 像素标记
'o' 圆标记
'v' 倒三角标记
'^' 正三角标记
'<' 左三角标记
'>' 右三角标记
'1' 下箭头标记
'2' 上箭头标记
'3' 左箭头标记
'4' 右箭头标记
's' 正方形标记
'p' 五边形标记
'*' 星形标记
'h' 六边形标记 1
'H' 六边形标记 2
'+' 加号标记
'x' X 标记
'D' 菱形标记
'd' 窄菱形标记
'|' 竖直线标记
'_' 水平线标记
'b' 蓝色
'g' 绿色
'r' 红色
'c' 青色
'm' 品红色
'y' 黄色
'k' 黑色
'w' 白色
"""
# 设置x和y
x = np.arange(1, 11) # [1, 2, 3,.....10]
y1 = x + 5
y2 = x**2 + 5
y3 = x**3 + 5
# 设置标题, x周和y轴含义
plt.title("Matplotlib demo")
plt.xlabel("x")
plt.ylabel("y")
plt.plot(x, y1, 'ob') # 'o'代表圆点, ‘b’颜色, 蓝色blue
plt.plot(x, y2, "*y")
plt.plot(x, y3,)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
# 计算正弦和余弦曲线上点的 x 和 y 坐标,
# np.arange([start, ]stop, [step, ]dtype=None): arange函数用于创建等差数组
"""
start:可忽略不写,默认从0开始;起始值
stop:结束值;生成的元素不包括结束值
step:可忽略不写,默认步长为1;步长
dtype:默认为None,设置显示元素的数据类型
"""
x = np.arange(0, 3 * np.pi, 0.1)
y1 = np.sin(x)
y2 = np.cos(x)
# 设置标题和xy轴信息;
plt.title("sine wave form")
plt.xlabel('x')
plt.ylabel('y')
# 使用 matplotlib 来绘制点
plt.plot(x, y1)
plt.plot(x, y2)
plt.show()
import random
from matplotlib import pyplot as plt
from matplotlib.font_manager import FontProperties
"""
绘制10月和三月温度变化的散点图
"""
# 字体设置
myfont = FontProperties(fname="/usr/share/fonts/cjkuni-ukai/ukai.ttc", size=14)
titlefont = FontProperties(fname="/usr/share/fonts/cjkuni-uming/uming.ttc", size=25)
# x轴和Y轴数据
march_y = [random.randint(10, 20) for i in range(31)]
october_y = [random.randint(20, 30) for j in range(31)]
x = range(1, 32) # 1, 2, 3, ...31
# 设置图形大小
plt.figure(figsize=(10, 5), dpi=100)
# 绘制散点图
plt.subplot(2, 1, 1)
plt.scatter(x, march_y, )
# 调整刻度
_x3_labels = ["3月{}日".format(_) for _ in x]
# rotation: 旋转
plt.xticks(x[::3], labels=_x3_labels[::3], fontproperties=myfont, rotation=45)
# x, y和标题的说明
plt.xlabel("月份", fontproperties=myfont)
plt.ylabel("温度(摄氏度)", fontproperties=myfont)
plt.title("3月温度变化散点图", fontproperties=titlefont)
plt.subplot(2, 1, 2)
plt.scatter(x, october_y)
_x10_labels = ["10月{}日".format(_) for _ in x]
plt.xticks(x[::3], labels=_x10_labels[::3], fontproperties=myfont, rotation=45)
# x, y和标题的说明
plt.xlabel("月份", fontproperties=myfont)
plt.ylabel("温度(摄氏度)", fontproperties=myfont)
plt.title("10月温度变化散点图", fontproperties=titlefont)
# 添加网格
plt.grid(alpha=0.5)
# 显示图像
plt.show()
"""
bar(): pyplot 子模块提供 bar() 函数来生成条形图。
"""
from matplotlib import pyplot as plt
x = [5, 8, 10]
y = [12, 16, 6]
x2 = [6, 9, 11]
y2 = [6, 15, 7]
plt.bar(x, y, align='center')
plt.bar(x2, y2, color='g', align='center')
plt.title('Bar graph')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
import random
import matplotlib.pyplot as plt
names = ['电影%s' %(i) for i in range(100)]
times = [random.randint(40, 140) for i in range(100)]
# hist代表直方图, times为需要统计的数据, 20为统计的组数.
plt.hist(times, 20)
plt.show()
import random
from matplotlib import pyplot as plt
from matplotlib.font_manager import FontProperties
names = ["战狼%s" %(i) for i in range(10)]
# print(names)
scores = [random.randint(50, 100) for i in range(10)]
# 设置图形大小
plt.figure(figsize=(20, 15), dpi=80)
# 绘制条形图
plt.barh(range(len(names)), scores, height=0.5, color='orange')
# 设置字体格式
myfont = FontProperties(fname='/usr/share/fonts/cjkuni-uming/uming.ttc', size=32)
# # 设置字符串到x轴
# plt.xticks(range(len(names)), labels=names, fontproperties=myfont)
# 设置字符串到y轴
plt.yticks(range(len(names)), labels=names, fontproperties=myfont)
# 绘制网格
plt.grid(0.1)
# 显示图像
plt.show()