知识储备 -- jupyter 里面使用matplotlib画图

353 阅读1分钟

话不多说,直接上代码

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

%matplotlib ipympl

font = FontProperties(fname='/System/Library/Fonts/Supplemental/Songti.ttc')

f = plt.figure(dpi=300, figsize=(24, 60))
plt.subplots_adjust(wspace=0.1, hspace=0.4)
for index, data in enumerate(dataframe):
    if data is None:
        continue
    x, y = data
    axes = plt.subplot(15,4, index+1)
    axes.plot(x, y)
    axes.axhline(y=0.5, ls="-", c="black")
    axes.axvline(x=0.5, ls="-", c="green")
    axes.set_title("标题", fontproperties=font)
plt.show()
f.savefig("test.png")

Mac 环境中想要给图标标题显示中文名字

from matplotlib.font_manager import FontProperties
font = FontProperties(fname='/System/Library/Fonts/Supplemental/Songti.ttc')
axes.set_title("中文标题", fontproperties=font)

这里的fname是字体文件的路径,根据自己系统的实际情况调整

设置展示图片的dpi和尺寸大小

f = plt.figure(dpi=300, figsize=(24, 60))

设置图表的行距和列距

plt.subplots_adjust(wspace=0.1, hspace=0.4)

导出成png图片

f.savefig("test.png")

设定图表的行数和列数

axes = plt.subplot(15,4, index+1)

表示15行,每行4个图表,当前这个画在第几个位置。

在jupyter环境中使用matplotlib的引擎

%matplotlib ipympl
%matplotlib qt6

选择一个即可。

增加横轴纵轴的辅助线

axes.axhline(y=0.5, ls="-", c="black")
axes.axvline(x=0.5, ls="-", c="green")