【发布时间】:2014-11-27 14:53:13
【问题描述】:
我在将 matplotlib 图形保存为图像时遇到了问题。 图像的保存与我在图表上调用 .show() 方法时显示的不同。 这里有一个例子: http://s1.postimg.org/lbyei5cfz/blue5.png
我不知道还能做什么。我花了过去几个小时试图找出是什么原因造成的,但我无法弄清楚。
这是我的完整代码。
import matplotlib.pyplot as plt
import random
turn = 1 #for the x values
class Graph():
def __init__(self, name, color):
self.currentValue = 5 #for the y values
self.x = [turn]
self.y = [self.currentValue]
self.name = name
self.color = color
def update(self):
if random.randint(0,1): #just to show if the graph's value goes up or down
self.currentValue += random.randint(0,10)
self.y.append(self.currentValue)
else:
self.currentValue -= random.randint(0,10)
self.y.append(self.currentValue)
self.x.append(turn)
def plot(self):
lines = plt.plot(self.x,self.y)
plt.setp(lines, 'color',self.color)
plt.savefig(self.name + str(turn))
#plt.show() will have a different result from plt.savefig(args)
graphs = [Graph("red",'r'),Graph("blue",'b'),Graph("green",'g')]
for i in range(5):
for i in graphs:
i.update() #changes the x and y value
i.plot() #saves the picture of the graph
turn += 1
对不起,如果这是我犯的一个愚蠢的错误,我只是觉得 plt.show() 和 plt.savefig 的不同之处很奇怪。
感谢您的帮助。
【问题讨论】:
标签: python matplotlib