python – matplotlib图形对象可以被腌制然后检索?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – matplotlib图形对象可以被腌制然后检索?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2586字,纯文字阅读大概需要4分钟。
内容图文
我试图挑选一个matplotlib图形对象,以便能够在以后用x和y数据以及标签和标题重新生成图形.这可能吗?
当尝试使用open和dump来pickle时,我得到了这个回溯:
#3rd Party Imports and built-in
import random
import matplotlib.pyplot as plt
import pickle as pk
#Initializing lists for x and y values. Voltage trim and current measure is our x and y in this case.
voltage_trim = range(100, 150)
current_meas = []
# A change of parameters modelled by a multiplier in this case
multiplier = range(1,4)
# Initializing lists to store the output current if wanted
current_storage = []
# Required for Matplotlib
plt.close()
plt.ion() #Required method call in order for interactive plotting to work
# SPECIFY GRAPH
fig1 = plt.figure()
ax = fig1.add_subplot(1,1,1) # Creates an axis in order to have multiple lines
plt.title('Voltage Trim Vs Current \nsome fancy sub-title here')
plt.xlabel('Voltage Trim / V')
plt.ylabel('Current Measured/ A')
plt.grid(True)
color_choices = ['k', 'g','r','b','k','c', 'm', 'y'] # Add more according to number of graphs
# MAIN TEST LOOPS
for this_mult in multiplier:
current_meas = [] # Clears the output list to graph with different multipier
#Enumerates input in order to manipulate it below
for index, value in enumerate(voltage_trim):
#Generating random current values in this case
current_meas.append(random.randint(0,10)*this_mult)
print index ,'Generating results...'
print index, value
# Increments index so that lists match dimensiosn and graphing is possible
index += 1
# Optional real time plotting function, comment out if not wanted
live_plotting = ax.plot(voltage_trim[:index], current_meas, color = color_choices[this_mult])#,label = 'Line'+str(this_mult)
# A pyplot method that pauses the loop, updates the graph and continues to enable for real time graphing, set to small number to be considered insignificant
plt.pause(1e-124)
# Deletes the real time line to save memory in the loop
live_plotting[0].remove()
# This is the actual storage of plot objects, specify legend label here, and all other arguments the same
ax.plot(voltage_trim, current_meas,color = color_choices[this_mult],marker = 'o', label = 'Line'+str(this_mult))
#Stores the measured current (A)
current_storage.append(current_meas)
#Calls legend - must be in outer loop
plt.legend()
f = open('testt','wb')
pk.dump(fig1, f)
f.close()
解决方法:
是.尝试
import pickle
import matplotlib.pyplot as plt
file = open('myfile', 'wb')
fig = plt.gcf()
pickle.dump(fig, file)
file.close()
然后阅读
file = open('myfile', 'rb')
pickle.load(file)
plt.show()
file.close()
内容总结
以上是互联网集市为您收集整理的python – matplotlib图形对象可以被腌制然后检索?全部内容,希望文章能够帮你解决python – matplotlib图形对象可以被腌制然后检索?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。