工具总结画图matplotlib画图
xiuqhou基本设置
设置中文
1 2
| plt.rcParams['font.sans-serif']=['SimHei'] plt.rcParams['axes.unicode_minus']=False
|
主题设置
1 2
| plt.style.available plt.style.use("seaborn")
|
主题列表:
‘Solarize_Light2’, ‘_classic_test_patch’, ‘bmh’, ‘classic’, ‘dark_background’, ‘fast’, ‘fivethirtyeight’, ‘ggplot’, ‘grayscale’, ‘seaborn’,‘seaborn-bright’, ‘seaborn-colorblind’,‘seaborn-dark’, ‘seaborn-dark-palette’, ‘seaborn-darkgrid’, ‘seaborn-deep’, ‘seaborn-muted’, ‘seaborn-notebook’, ‘seaborn-paper’, ‘seaborn-pastel’, ‘seaborn-poster’, ‘seaborn-talk’, ‘seaborn-ticks’, ‘seaborn-white’, ‘seaborn-whitegrid’, ‘tableau-colorblind10’
边框设置
1 2 3 4
| plt.axis('off')
ax.spines['right'].set_visible(False) ax.spines['bottom'].set_linewidth(5)
|
画布设置
1 2 3
| plt.rcParams['figure.dpi'] = 150 plt.figure(figsize=(15,10), dpi=150) fig.set_size_inches(5,4)
|
获取ax对象
1 2 3
| fig, ax = plt.subplots() ax = plt.gca()
|
画图
折线图
1 2 3 4 5 6 7
| plt.plot(x, y, 'r-v', linewidth=1.2, markersize=5) ax.plot(x, y, 'r-v')
|
bar图
1 2 3 4 5 6 7
| ax.bar(x, y,width=0.5,align="center",label='',color = colors, edgecolor = '', linestyle='-.', lw=2, hatch='/') plt.bar(x, y,width=0.5,align="center",label='',color = colors, edgecolor = '', linestyle='-.', lw=2, hatch='/')
plt.rcParams['hatch.color']="grey" plt.rcParams['hatch.linewidth']=0.5
|
heat map热图(图片)
1
| plt.imshow(matrix, aspect='auto')
|
等高线图、等高线轮廓图
1 2
| plt.contour() plt.contourf()
|
画布元素
标题
文本标注
1 2
| for a,b in zip(x, y): plt.text(a-0.05, b, '%.3f' % b, ha='center', va= 'bottom',fontsize=13,color='black')
|
横纵坐标
1 2 3 4 5 6 7 8 9 10 11
| ax1.set_xlabel('$\mu$',fontsize=15) ax1.set_ylabel('',fontsize=15)
plt.xticks(x, Mu, fontsize=15, rotation=-45) plt.yticks(fontsize=15)
plt.xlim([]) plt.ylim([5.75,7.2])
|
图例
1 2
| plt.plot(x, y, label="") plt.legend(loc='lower right')
|
网格
1
| plt.grid(True,which='major',linestyle='--',linewidth=5)
|
颜色
设置背景颜色
1
| ax.set_facecolor('#e3e3e3')
|
给定颜色
1 2 3 4 5
| import matplotlib as mpl colors = ['white', '#FFF8E1', '#FFD54F', '#F57F17','#E65100'] cmap = mpl.colors.ListedColormap(colors)
plt.imshow(np.random.randn(30,30),cmap=cmap)
|
自动改变颜色
1 2 3 4 5
| import matplotlib.colors as mcolors
colors=list(mcolors.TABLEAU_COLORS.keys()) for i in range(): color=mcolors.TABLEAU_COLORS[colors[i]]
|
获取渐变颜色
1 2 3 4
| map_vir = cm.get_cmap(name='viridis') norm = plt.Normalize(5.5,7) colors = map_vir(norm(MSE_loss))
|
渐变标注条
1 2 3 4
| sm = cm.ScalarMappable(cmap=map_vir,norm=norm) sm.set_array([]) plt.colorbar(sm)
|
子图
划分子图
1 2
| plt.subplot(num_x_subplot, num_y_subplot, i) plt.subplots_adjust(wspace=0, hspace=0)
|
子图图例合并放置于大图中
1 2 3 4 5 6 7 8 9 10 11 12
| lines = [] labels = []
for ax in fig.axes: axLine, axLabel = ax.get_legend_handles_labels() lines.extend(axLine[:-1]) labels.extend(axLabel[:-1]) lines.extend(axLine[-1:]) labels.extend(axLabel[-1:])
plt.legend(lines, labels, loc='lower right')
|
图片保存
1
| plt.savefig('图片名称.svg',dpi=600,bbox_inches='tight')
|
ubuntu下字体安装问题
matplotlib没有检测到ubuntu下的字体,首先查看matplotlib的缓存目录:
1 2
| import matplotlib.font_manager matplotlib.font_manager.get_cachedir()
|
1 2
| apt install font-manager # 安装字体管理器 rm -r ~/.cache/matplotlib # 删除matplotlib缓存,以便后续重建
|
最后重启程序。