1. 导入包
import matplotlib.pyplot as plt
2. 基本绘图
(1)折线图
plt.plot(x,y)
3. 图像美化
(1)画布设置
# 设置画面大小
plt.figure(figsize=(6, 6.5))
# 设置图片分辨率
plt.rcParams['figure.dpi'] = 300
(2)手动设置坐标轴刻度间隔以及刻度范围
#从pyplot导入MultipleLocator类,这个类用于设置刻度间隔
from matplotlib.pyplot import MultipleLocator
#ax为两条坐标轴的实例
ax=plt.gca()
#把x轴的刻度间隔设置为1,把y轴的刻度间隔设置为10
x_major_locator=MultipleLocator(1)
y_major_locator=MultipleLocator(10)
ax.xaxis.set_major_locator(x_major_locator)
ax.yaxis.set_major_locator(y_major_locator)
# 把x轴的刻度范围设置为-0.5到11; 把y轴的刻度范围设置为-5到110
plt.xlim(-0.5,11)
plt.ylim(-5,110)
(3)调整字体大小
ax=plt.gca()
'''设置刻度字体大小(fontsize), 字体样式(fontname),加粗(weight)'''
plt.xticks(fontsize=20,fontname='Times New Roman',weight = 'bold')
plt.yticks(fontsize=20,fontname='Times New Roman',weight = 'bold')
'''设置坐标标签字体大小(fontsize), 字体样式(fontname),加粗(weight)'''
ax.set_xlabel(..., fontsize=20,fontname='Times New Roman',weight = 'bold')
ax.set_ylabel(..., fontsize=20,fontname='Times New Roman',weight = 'bold')
'''设置图例字体大小(fontsize), 字体样式(fontname),加粗(weight)'''
ax.legend(..., fontsize=20,fontname='Times New Roman',weight = 'bold')
# 图例字体样式另一种设置方法
font1 = {'family' : 'Times New Roman',
'weight' : 'normal',
'size' : 13,
}
plt.legend(prop=font1)
(4)设置legend位置
# 设置位置为左下
plt.legend(loc='lower left')
'''
左上:loc='upper left'
左下:loc='lower left'
右上:loc='upper right'
右下:loc='lower right'
'''
参考:
1. 画布设置:
(1)【数据展示】matplotlib设置画面大小_肥宅_Sean的博客-CSDN博客_matplotlib设置画布大小
(2)Python绘图问题:Matplotlib中指定图片大小和像素_MeteorMan99的博客-CSDN博客_matplotlib size
2. 刻度设置:
(1)Python设置matplotlib.plot的坐标轴刻度间隔以及刻度范围_宁宁Fingerstyle的博客-CSDN博客_matplotlib 刻度
3. 调整字体大小
(1)https://blog.csdn.net/u011008379/article/details/60478927
(2)python matplotlib 画图刻度、图例等字体、字体大小、刻度密度、线条样式设置_CAM-TAY的博客-CSDN博客_matplotlib 字体(3)Matplotlib作图中坐标轴字体的加粗斜体格式_PascalXie的博客-CSDN博客_matplotlib 字体加粗
4. legend 位置设置