Matplotlib_库的安装

1.安装

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

2.解决中文乱码的问题

  • 原因:
    出现中文乱码是因为 matplotlib 库中没有中文字体,所以显示出来的不像是真正的乱码,而是都为方框。
  • 解决方法
    • 第一步:查看matplotlib的库文件路径(例如:anaconda3/envs/pytorch/lib/python3.8/site-packages/matplotlib/mpl-data/matplotlibrc' )
      import matplotlib
      matplotlib.matplotlib_fname()
      
    • 第二步:进入上一步得到的路径,打开 matplotlibrc 配置文件,找到以 #font.sans-serif 开头的那一行,去掉#,并添加 SimHei 在该参数中(如下所示)
      vim ../anaconda3/envs/your_env/lib/python3.8/site-packages/matplotlib/mpl-data/matplotlibrc
       修改的内容
      
      # 去掉前面的#  
      font.family         : sans-serif   
      
      # 去掉前面的#,并在冒号后面添加SimHei(或者:simsun、arial)
      font.sans-serif     : simhei, arial, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif  
      
      # 去掉前面的#,并将True改为False
      axes.unicode_minus  : False
      
    • 第三步:在网上下载需要添加的字体的 ttf 文件,SimHei 的下载地址:https://www.fontpalace.com/font-details/SimHei/。(或者window系统中找到(C:/windows/Fonts))下载后将文件放置在 matplotlib 包文件夹下的 /fonts/ttf 中(也就是前面matplotlibrc 配置文件所在的文件夹的 /fonts/ttf 中)。
      • simhei.ttf
      • arial.ttf
      • 宋体simsun.ttc
    • 第四步:删除matplotlib缓存(把.cache下面的matplotlib文件夹删除。)
      # python 终端获取缓存路径
      import matplotlib
      print(matplotlib.get_cachedir())
      # /home/xxx/.cache/matplotlib
      
      # 删除缓冲目录
      # (shell)
      rm -rf /home/xxx/.cache/matplotlib
      
    • 第五步:改代码
      import matplotlib.pyplot as plt
      plt.rcParams['font.sans-serif'] = ['simsun']
      

参考博客: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()

猜你喜欢

转载自blog.csdn.net/qq_42887760/article/details/107342560