matplotlib绘图保存图片深入
系列目录
python数据处理1: 导入数据、片选数据、数据绘图
python数据处理2: 拟合数据、整合数据、导出数据
python数据处理3: 光谱曲线的洛伦兹函数拟合
python数据处理4: matplotlib嵌入到PyQt、os获取文件列表、cmd/bat直接执行python
python数据处理5: doc批量另存为docx
python数据处理6: 读取docx表格数据并汇总输出
模块导入
import matplotlib.pyplot as plt
图片大小、像素
plt.rcParams['figure.figsize'] = (8, 6) # 设置figure_size尺寸
plt.rcParams['figure.dpi'] = 100
plt.rcParams['savefig.dpi'] = 300
图形大小、边距
plt.subplots_adjust(left=1.2/8, bottom=1.2/6, right=7.8/8, top=5.5/6,wspace=1.2/4, hspace=1.2/3)
字体与大小
plt.rcParams['font.sans-serif'] = 'Arial'
plt.rcParams['xtick.labelsize'] = 23
plt.rcParams['ytick.labelsize'] = 23
plt.rcParams['axes.titlesize'] = 23
plt.rcParams['axes.labelsize'] = 23
坐标轴
源码
import numpy as np
import matplotlib.pyplot as plt
def f(t):
return np.exp(-t) * np.cos(2*np.pi*t)
plt.rcParams['figure.figsize'] = (8, 6) # 设置figure_size尺寸
plt.rcParams['figure.dpi'] = 100
plt.rcParams['savefig.dpi'] = 300
plt.rcParams['font.sans-serif'] = 'Arial'
plt.rcParams['xtick.labelsize'] = 23
plt.rcParams['ytick.labelsize'] = 23
plt.rcParams['axes.titlesize'] = 23
plt.rcParams['axes.labelsize'] = 23
plt.rcParams['lines.linewidth'] = 2
t1 = np.arange(0.0, 3.0, 0.01)
ax1 = plt.subplot(212)
ax1.margins(0,0) # Default margin is 0.05, value 0 means fit
ax1.plot(t1, f(t1))
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax2 = plt.subplot(221)
ax2.margins(0, 0) # Values >0.0 zoom out
ax2.plot(t1, f(t1))
ax2.set_title('Zoomed out')
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax3 = plt.subplot(222)
ax3.margins(x=0, y=0) # Values in (-0.5, 0.0) zooms in to center
ax3.plot(t1, f(t1))
ax3.set_title('Zoomed in')
ax3.set_xlabel('x')
ax3.set_ylabel('y')
plt.savefig('testsavefig.png')
plt.show()
定义
plt.subplots_adjust(left=1.2/8, bottom=1.2/6, right=7.8/8, top=5.5/6,wspace=1.2/4, hspace=1.2/3)
left, bottom, right, top 为归一化坐标,例如
plt.subplots_adjust(left=0, bottom=0, right=1, top=1)
表示占满全部画板。
wspace, hspace为子图归一化坐标。
问题
matplotlib:ValueError: bottom cannot be >= top
解决,采用管理员权限:
pip install -U matplotlib
savefig 空白
plt.savefig()要在plt.show()前面。
参考
https://blog.csdn.net/weixin_34613450/article/details/80678522
matplotlib:ValueError: bottom cannot be >= top 解决办法
https://vimsky.com/zh-tw/examples/usage/matplotlib-pyplot-subplots_adjust-in-python.html
https://secsilm.blog.csdn.net/article/details/52912439
https://www.osgeo.cn/matplotlib/gallery/subplots_axes_and_figures/axes_margins.html