1. Install
pip install matplotlib
Or: pip install matplotlib -i https://pypi.doubanio.com/simple/
# Downloading from the Douban mirror is faster
and generally used with the numpy library:pip install numpy
2. Solve the problem of Chinese garbled characters
- Reason:
Chinese garbled characters appear because there are no Chinese fonts in the matplotlib library, so what is displayed does not look like real garbled characters, but are all boxes. - Solution
- Step 1: View the library file path of matplotlib (for example:
anaconda3/envs/pytorch/lib/python3.8/site-packages/matplotlib/mpl-data/matplotlibrc'
)import matplotlib matplotlib.matplotlib_fname()
- Step 2: Enter the path obtained in the previous step, open the matplotlibrc configuration file, find the line beginning with #font.sans-serif, remove #, and add SimHei to this parameter (as shown below)
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
- Step 3: Download the ttf file of the font to be added online, the download address of SimHei: https://www.fontpalace.com/font-details/SimHei/ . (Or found in the window system (C:/windows/Fonts)) After downloading, place the file in /fonts/ttf under the matplotlib package folder (that is, /fonts/ttf in the folder where the previous matplotlibrc configuration file is located) .
- simhei.ttf
- arial.ttf
- Song simsun.ttc
- Step 4: Delete the matplotlib cache (delete the matplotlib folder under .cache.)
# python 终端获取缓存路径 import matplotlib print(matplotlib.get_cachedir()) # /home/xxx/.cache/matplotlib # 删除缓冲目录 # (shell) rm -rf /home/xxx/.cache/matplotlib
- Step 5: Change the code
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['simsun']
- Step 1: View the library file path of matplotlib (for example:
Reference blog: Python: matplotlib Chinese garbled solution
3. Draw subgraphs
4. Scatterplot
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()
or
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()