matplotlib的选项
- matplotlib保存为pdf文件时无法在ps、AI等软件下编辑的问题
import matplotlib as mpl
mpl.rcParams['pdf.fonttype'] = 42
坐标标注的数据范围较大时,如何优雅的设置标注?(如图y轴)

from matplotlib.ticker import ScalarFormatter
y_formatter = ScalarFormatter(useMathText=True)
y_formatter.set_powerlimits((0, 0)) # 设置指数部分显示的范围
if __name__ == "__main__":
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(111)
# 绘制上图所有的数据为dist_array
ax.hist(dist_array, bins=np.arange(0, 30, 0.5), color="gray")
ax.set_xlabel("Hypocenter separation distance (km)")
ax.set_xlim([0, 10])
ax.set_ylabel("Counts")
# 刻度设置, 该方式主要实现在笛卡尔轴坐标系下坐标刻度的指数形式表示
ax.yaxis.set_major_formatter(y_formatter)
# ax.ticklabel_format(style='sci', axis='y', scilimits=(0, 0)) # 可以尝试一下
# bbox_inches="tight"保存文件时常用的选项,保证图片边框空白没有那么大
plt.savefig("example.png", bbox_inches='tight')
plt.savefig("example.pdf", bbox_inches='tight')
plt.show()
多个子图下,各个子图之间的间距设置
plt.subplots_adjust(left=0.1, bottom=0.2, right=0.9, top=0.8, hspace=0.1, wspace=0.1)
横线和竖线的绘制
# 绘制竖线
ax.axvline(x=361, color="green", linestyle="--", linewidth=0.5)
# 绘制水平线
ax.axhline(y=342, color="blue", linestyle="--", linewidth=0.5)