0
我試圖收集一系列圖表,然後將它們全部輸出到Excel中。圖表對象列表但不是全部圖
該列表將所有圖表顯示爲「(示例)0xb8eae80」處的matplotlib.figure.Figure,但不會顯示所有圖表。當程序運行時,它們都顯示在iPython(Spyder)控制檯中。
如果我嘗試在iPython中單獨使用list [i]顯示它們,則會顯示一些圖表,但有些圖表不顯示(缺少相同的圖表)。
對於輸出到Excel,「缺失」圖表有一個默認大小的空白畫布,因此,雖然XL獲取有關圖表存在的一些信息,但它甚至沒有得到「無花果」信息。
在下面的示例代碼中缺少的圖表是多線圖圖表(它的一個簡單的例子,我意識到它可以更好的腳本)的任何幫助
感謝。
示例代碼(它有同樣的問題我原來的代碼):
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import xlwings as xw
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
xdf = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD')) # splits out list into components
xdf = xdf.cumsum()
figlist = [] # set up list to collect charts (a list of objects)
fig = plt.figure()
xdf.A.plot(figsize = (9,5), grid=True) #plots individual column
plt.axhline(0,lw=2, color = "black")
plt.title('A')
figlist.append(fig)
fig = plt.figure()
xdf.B.plot(figsize = (9,5), grid=True) #plots individual column
plt.axhline(0,lw=2, color = "black")
plt.title('B')
figlist.append(fig)
fig = plt.figure()
xdf.C.plot(figsize = (9,5), grid=True) #plots individual column
plt.axhline(0,lw=2, color = "black")
plt.title('C')
figlist.append(fig)
fig = plt.figure()
xdf.D.plot(figsize = (9,5), grid=True) #plots individual column
plt.axhline(0,lw=2, color = "black")
plt.title('D')
figlist.append(fig)
fig = plt.figure()
xdf.plot(figsize = (9,5), grid=True) #plots all columns on same chart
plt.axhline(0,lw=2, color = "black")
plt.title('All')
figlist.append(fig)
# output all charts to XL
xw.Workbook() # open new workbook
for i in range(len(figlist)):
plotno = 'Plot' + str(i)
plot = xw.Plot(figlist[i])
plot.show(plotno, left=30, top=(i*500+20))
問題解決了,非常感謝菲利克斯。任何想法爲什麼我的不正確的方法適用於某些圖表,但不是最後一個? – johnb
不,我不知道熊貓和matplotlib如何在引擎蓋下一起工作,但[this](http://stackoverflow.com/questions/19555525/saving-plots-axessubplot-generated-from-python-pandas -with-matplotlibs-savefi)描述了相同的錯誤。 –