# 在命令行输入pip install matplotlib
import matplotlib
# 查看版本号
print(matplotlib.__version__)
大多数Matplotlib实用程序都位于pyplot子模块下,通常以plt别名导入
import numpy as np
import matplotlib.pyplot as plt
# 在图中从位置(0,0)到位置(6,250)画一条线
xpoints = np.array([0, 6])
ypoints = np.array([0, 250])
plt.plot(xpoints, ypoints)
plt.show()
# 不指定x轴的点,默认为0到1平均分
ypoints = np.array([0, 250])
plt.plot(ypoints)
plt.show()
仅绘制标记点,可以使用快捷字符串符号参数 ‘o’ ,这意味着“环”
xpoints = np.array([0, 6])
ypoints = np.array([0, 250])
plt.plot(xpoints, ypoints,'o')
plt.show()
xpoints = np.array([33, 7, 6, 13])
ypoints = np.array([3, 23, 88, 42])
plt.plot(xpoints, ypoints)
plt.show()
xpoints = np.array([1, 3, 5, 7])
ypoints = np.array([3, 23, 88, 42])
# plt.plot(xpoints, ypoints, marker='o')
plt.plot(xpoints, ypoints, marker='*')
plt.show()
字符 | 颜色 |
---|---|
‘b’ | 蓝色 |
‘g’ | 绿色 |
‘r’ | 红色 |
‘c’ | 青色 |
‘m’ | 品红色 |
‘y’ | 黄色 |
‘k’ | 黑色 |
‘w’ | 白色 |
xpoints = np.array([1, 3, 5, 7])
ypoints = np.array([3, 23, 88, 42])
plt.plot(xpoints, ypoints, 'o:r')
plt.show()
xpoints = np.array([1, 3, 5, 7])
ypoints = np.array([3, 23, 88, 42])
plt.plot(xpoints, ypoints, 'o:r',ms='20')
plt.show()
xpoints = np.array([1, 3, 5, 7])
ypoints = np.array([3, 23, 88, 42])
plt.plot(xpoints, ypoints, 'o:r', ms='20', markeredgecolor='b')
plt.show()
xpoints = np.array([1, 3, 5, 7])
ypoints = np.array([3, 23, 88, 42])
plt.plot(xpoints, ypoints, ls='dashed')
plt.show()
dotted写成:
dashed写成–
xpoints = np.array([1, 3, 5, 7])
ypoints = np.array([3, 23, 88, 42])
plt.plot(xpoints, ypoints, ls='--')
plt.show()
字符 | 描述 |
---|---|
‘-’ | 实线样式 |
‘–’ | 短横线样式 |
‘-.’ | 点划线样式 |
‘:’ | 虚线样式 |
‘.’ | 点标记 |
‘,’ | 像素标记 |
‘o’ | 圆标记 |
‘v’ | 倒三角标记 |
‘^’ | 正三角标记 |
‘<’ | 左三角标记 |
‘>’ | 右三角标记 |
‘1’ | 下箭头标记 |
‘2’ | 上箭头标记 |
‘3’ | 左箭头标记 |
‘4’ | 右箭头标记 |
‘s’ | 正方形标记 |
‘p’ | 五边形标记 |
’*’ | 星形标记 |
‘h’ | 六边形标记 1 |
‘H’ | 六边形标记 2 |
‘+’ | 加号标记 |
‘x’ | X 标记 |
‘D’ | 菱形标记 |
‘d’ | 窄菱形标记 |
‘enter键上面那个键,与markdown表格冲突打不出来,理解万岁’ | 竖直线标记 |
‘_’ | 水平线标记 |
xpoints = np.array([1, 3, 5, 7])
ypoints = np.array([3, 23, 88, 42])
plt.plot(xpoints, ypoints, lw='20.5')
plt.show()
xpoints = np.array([1, 3, 5, 7])
ypoints1 = np.array([3, 23, 88, 42])
ypoints2 = np.array([78, 13, 44, 99])
plt.plot(xpoints, ypoints1)
plt.plot(xpoints, ypoints2)
plt.show()
# 设置字体为楷体
matplotlib.rcParams['font.sans-serif'] = ['KaiTi']
xpoints = np.array([1, 3, 5, 7])
ypoints = np.array([78, 13, 44, 99])
plt.plot(xpoints, ypoints)
plt.xlabel('时间节点')
plt.ylabel('收入')
plt.show()
# 参数对应字体、颜色、大小
font1 = {'family': 'KaiTi', 'color': 'blue', 'size': 20}
font2 = {'family': 'KaiTi', 'color': 'darkred', 'size': 15}
xpoints = np.array([1, 3, 5, 7])
ypoints = np.array([78, 13, 44, 99])
plt.title('我是标题', fontdict=font1)
plt.xlabel('时间节点', fontdict=font1)
plt.ylabel('收入', fontdict=font2)
plt.plot(xpoints, ypoints)
plt.show()
xpoints = np.array([1, 3, 5, 7])
ypoints = np.array([78, 13, 44, 99])
plt.title('我是标题')
plt.xlabel('时间节点')
plt.ylabel('收入')
plt.grid()
plt.plot(xpoints, ypoints)
plt.show()
xpoints = np.array([1, 3, 5, 7])
ypoints = np.array([78, 13, 44, 99])
plt.title('我是标题')
plt.xlabel('时间节点')
plt.ylabel('收入')
plt.grid(axis='x')
plt.plot(xpoints, ypoints)
plt.show()
plt.grid(color='color',linestyle='linestyle',linewidth='number')
# 图一
xpoints = np.array([1, 3, 5, 7])
ypoints = np.array([78, 13, 44, 99])
plt.subplot(1, 2, 1)
plt.plot(xpoints, ypoints)
# 图二
xpoints = np.array([1, 3, 5, 7])
ypoints = np.array([18, 63, 4, 56])
plt.subplot(1, 2, 2)
plt.plot(xpoints, ypoints)
plt.show()
xpoints = np.array([1, 3, 5, 7])
ypoints = np.array([78, 13, 44, 99])
plt.scatter(xpoints, ypoints)
plt.show()
colors = np.array(['red', 'green', 'gray', 'black'])
)
xpoints = np.array([1, 3, 5, 7, 9, 11])
ypoints = np.array([78, 13, 44, 99, 150, 8])
colors = np.array([0, 20, 40, 60, 80, 100])
plt.scatter(xpoints, ypoints, c=colors, cmap='viridis')
plt.colorbar() # 可以把颜色图展示在旁边
plt.show()
x = np.random.randint(100, size=(100))
y = np.random.randint(100, size=(100))
colors = np.random.randint(100, size=(100))
sizes = 10 * np.random.randint(100, size=(100))
plt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='nipy_spectral')
plt.colorbar()
plt.show()
x = np.array(["A", "B", "C", "D", "E", "F"])
y = np.array([78, 13, 44, 99, 150, 8])
plt.bar(x, y)
plt.show()
# 随机创建一个均值170,方差为10,数量为250的数组
x = np.random.normal(170, 10, 250)
plt.hist(x)
plt.show()
y = np.array([20, 20, 45, 15])
plt.pie(y)
plt.show()
my_labels = ["A", "B", "C", "D"]
y = np.array([20, 20, 45, 15])
plt.pie(y, labels=my_labels)
plt.show()
my_labels = ["A", "B", "C", "D"]
y = np.array([20, 20, 45, 15])
my_explode = [0, 0, 0, 0.5]
plt.pie(y, labels=my_labels, explode=my_explode)
plt.show()
my_labels = ["A", "B", "C", "D"]
y = np.array([20, 20, 45, 15])
plt.pie(y, labels=my_labels)
plt.legend()
plt.show()
matplotlib.rcParams['font.sans-serif'] = ['KaiTi']
my_labels = ["A", "B", "C", "D"]
y = np.array([20, 20, 45, 15])
plt.pie(y, labels=my_labels)
plt.legend(title="我是图例标题")
plt.show()