![](/img/trans.png)
[英]How can I group multiple plots and save them in png on Matplotlib?
[英]How to create specific plots using Pandas and then store them as PNG files?
所以我試圖為我的數據集中的每個特定變量創建直方圖,然后將其保存為 PNG 文件。
我的代碼如下:
import pandas as pd
import matplotlib.pyplot as plt
x=combined_databook.groupby('x_1').hist()
x.figure.savefig("x.png")
我不斷收到“AttributeError: 'Series' object 沒有屬性 'figure'”
使用matplotlib
創建圖形和軸對象,然后使用ax
參數告訴pandas
哪個軸到 plot。 最后,使用matplotlib
(或圖)保存圖。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Sample Data (3 groups, normally distributed)
df = pd.DataFrame({'gp': np.random.choice(list('abc'), 1000),
'data': np.random.normal(0, 1, 1000)})
fig, ax = plt.subplots()
df.groupby('gp').hist(ax=ax, ec='k', grid=False, bins=20, alpha=0.5)
fig.savefig('your_fig.png', dpi=200)
而不是使用*.hist()
我會使用matplotlib.pyplot.hist()
。
例子:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
y =[10, 20,30,40,100,200,300,400,1000,2000]
x = np.arange(10)
fig = plt.figure()
ax = plt.subplot(111)
ax.plot(x, y, label='$y = Values')
plt.title('my plot')
ax.legend()
plt.show()
fig.savefig('tada.png')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.