问题描述
我想创建一个沿水平轴放大和缩小 matplotlib 图形的 python 脚本.我的图是一组水平条形图.
I want to create a python script that zooms in and out of matplotlib graphs along the horizontal axis. My plot is a set of horizontal bar graphs.
我也想使它能够拍摄任何通用的matplotlib图.
I also want to make that able to take any generic matplotlib graph.
我不想只加载图像并将其放大,我想沿水平轴放大图形.(我知道怎么做)
I do not want to just load an image and zoom into that, I want to zoom into the graph along the horizontal axis. (I know how to do this)
是否有某种方法可以将创建的图形保存和加载为数据文件,或者是否有可以稍后保存和加载的对象?
Is there some way I can save and load a created graph as a data file or is there an object I can save and load later?
(通常,我先创建图形,然后用matplotlib plt.show显示它,但是图形创建需要时间,并且我不想每次显示时都重新创建图形)
(typically, I would be creating my graph and then displaying it with the matplotlib plt.show, but the graph creation takes time and I do not want to recreate the graph every time I want to display it)
推荐答案
您可以使用 泡菜包,用于保存轴,然后将其加载回去.
You can use pickle package for saving your axes and then load it back.
将您的绘图保存到 pickle
文件中:
Save your plot into a pickle
file:
import pickle
import matplotlib.pyplot as plt
ax = plt.plot([1,2,5,10])
pickle.dump(ax, open("plot.pickle", "wb"))
然后加载回来:
import pickle
import matplotlib.pyplot as plt
ax = pickle.load(open("plot.pickle", "rb"))
plt.show()
这篇关于Python Matplotlib将图形另存为数据文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!