Matplotlib_库的安装

2022-08-03

1.安装

pip install matplotlib
或者:pip install matplotlib -i https://pypi.doubanio.com/simple/ # 从豆瓣镜像中下载速度比较快
一般配合numpy库使用:pip install numpy

2.解决中文乱码的问题

  • 原因:
    出现中文乱码是因为 matplotlib 库中没有中文字体,所以显示出来的不像是真正的乱码,而是都为方框。
  • 解决方法
    参考博客:Python:matplotlib 中文乱码的解决方案

3.绘制子图

Matplotlib如何绘制子图

4.散点图

matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, edgecolors=None, *, data=None, **kwargs)

fig, subs = plt.subplots(8, 8)
lables = ['', '', '', '', '', '', '', '']
for i in range(8):
     for j in range(8):
         subs[i][j].scatter(temp_a[y == 0], temp_b[y == 0], c="", label='标签为0', alpha=1, marker='o', s=10, edgecolors='salmon')
         subs[i][j].scatter(temp_a[y == 1], temp_b[y == 1], c="", label='标签为0', alpha=1, marker='o', s=10, edgecolors='SkyBlue')
         subs[i][j].set_xticks([])  # 去掉坐标轴刻度
         subs[i][j].set_yticks([])
         subs[i][j].set_xlabel(lables[j], fontsize=8)  # 添加坐标轴标签
         subs[i][j].set_ylabel(lables[i], fontsize=8)
plt.subplots_adjust(hspace=0.2)
plt.show()

或者

fig=plt.figure(figsize=(20, 10))
# 绘制第一个子图
ax1 = fig.add_subplot(121)  # 121:一行两列的第一个子图
plt.scatter(a[y == 0], b[y == 0], c="salmon", label='标签为0')
plt.scatter(a[y == 1], b[y == 1], c="SkyBlue", label='标签为1')
ax1.set_ylabel('y轴标签', fontsize=10)
ax1.set_xlabel('x轴标签', fontsize=10)
ax1.set_title('子图1的title', fontsize=15)
ax1.legend(fontsize=10)  # 图例说明

# 绘制第二个子图
ax2 = fig.add_subplot(122)  # 122:一行两列的第二个子图
plt.scatter(a[y == 0], b[y == 0], c="salmon", label='标签为0')
plt.scatter(a[y == 1], b[y == 1], c="SkyBlue", label='标签为1')
ax2.set_ylabel('x轴标签', fontsize=10)
ax2.set_xlabel('y轴标签', fontsize=10)
ax2.set_title('子图2的title', fontsize=15)
ax2.legend(fontsize=10)  # 图例说明

plt.show()

本文地址:https://blog.csdn.net/qq_42887760/article/details/107342560

《Matplotlib_库的安装.doc》

下载本文的Word格式文档,以方便收藏与打印。