繁体   English   中英

在mpld3 pip包中获取图

[英]Getting plot figure in mpld3 pip package

我第一次尝试mpld3,并且想将图另存为json文件,因此我尝试了以下操作

f=open('my_first_graph.json', 'w+')
plt.hist(population_ages, bins, histtype='bar', rwidth=0.8)
fig = plt.Figure()
fig.show()
plt.title("Populations")
plt.legend()

mpld3.show()
mpld3.save_json(fig, fileobj=f)f= open('my_first_graph.json', 'w+')
plt.hist(population_ages, bins, histtype='bar', rwidth=0.8)
fig = plt.Figure()
fig.show()
plt.title("Populations")
plt.legend()

mpld3.show()
mpld3.save_json(fig, fileobj=f)

根据文档, save_json方法采用文件对象两个参数,并且

matplotlib Figure instance

The figure to write to file.

所以我成功添加了文件对象,并制作了一个json文件,问题是它为空。

如何将我的图制作成图形并将其作为参数传递给方法,以成功将其解析为json?

您需要在代码fig = plt.gcf() fig = plt.Figure()更改为fig = plt.gcf()

plt.Figure正在创建一个新的空图,然后将其保存。 这就是为什么您看到一个空的数字。

您要做的是获取使用plt创建的当前图形的句柄,因此您应该使用gcf (获取当前图形)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM