虽然这部分有用
import matplotlib.pyplot as plt
from ggplot import *
import numpy as np
import pandas as pd
df = pd.DataFrame({'Height':np.random.randn(10),
'Weight':np.random.randn(10),
'Gender': ["Male","Male","Male","Male","Male",
"Female","Female","Female","Female","Female"]})
p=ggplot(aes(x='Height', y='Weight', color='Gender'), data=df) + geom_point()
p.make()
fig = plt.gcf()
ax = plt.gca()
fig.set_figwidth(25, forward=True)
plt.show()
当我尝试保存图像时,它无法生成空白图像。
plt.savefig("image.tiff", dpi=300)
有什么想法吗?谢谢!
答案 0 :(得分:0)
您是否尝试使用ggsave(plot = p, filename = 'image.tiff')
或p.save('image.tiff')
?
修改强> ggsave已从ggplot中删除,请使用第二种解决方案。
答案 1 :(得分:0)
我使用ggplot对象save
方法成功将图形保存到文件中。这是一个例子:
p.save('image.tiff', width=12, height=8, dpi=144)
有关详细信息,请参阅this SO响应中的@Johan评论,找到的源代码here或找到的ggplot | docs here中的示例。< / p>
在设置屏幕尺寸方面,使用当前API似乎不太可能。可能的黑客攻击是克隆存储库并将line 624修改为以下内容:
self.fig, self.subplots = plt.subplots(subplot_kw=subplot_kw, figsize=(my_x, my_y)
其中my_x和my_y是您希望屏幕显示的x和y尺寸。然后使用show
方法:
p.show()
答案 2 :(得分:0)
感谢所有评论 - 这就是最终的工作。代码中有一点重复,但最后都有效。
import matplotlib.pyplot as plt
from ggplot import *
import numpy as np
import pandas as pd
df = pd.DataFrame({'Height':np.random.randn(10),
'Weight':np.random.randn(10),
'Gender': ["Male","Male","Male","Male","Male",
"Female","Female","Female","Female","Female"]})
p=ggplot(aes(x='Height', y='Weight', color='Gender'), data=df) + geom_point()
p.make()
fig = plt.gcf()
ax = plt.gca()
w,h=25,10 #set width and height for both screen and file size
#this part prints on screen the chart
fig.set_figwidth(w, forward=True) #screen
fig.set_figheight(h, forward=True) #screen
plt.show()
#this part saves it to file
p.save(filename, width=w, height=h, dpi=300) #file