图表对象列表但不是所有图表
List of chart objects but not all plot
我正在尝试将一系列图表收集到一个列表中,然后将它们全部输出到一个列表中 运行 Excel。
该列表将所有图表显示为 "matplotlib.figure.Figure at (example) 0xb8eae80",但不会显示所有图表。当 prog 为 运行 时,它们都显示在 iPython(在 Spyder 中)控制台中。
如果我尝试在 iPython 中使用 list[i] 单独显示它们,一些图表显示但有些不显示(相同的图表丢失)。
对于 Excel 的输出,"missing" 图表有默认大小的空白 canvas 因此,虽然 XL 正在获取有关图表存在的一些信息,但它不是'甚至得到 "figsize" 信息。
下面示例代码中缺少的图表是针对多线图的图表(这是一个简单的示例,我意识到它可以编写得更好)
感谢您的帮助。
示例代码(与我的原始代码存在相同问题):
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))
对于 Pandas 从 docs:
生成的绘图,您没有正确访问图形对象
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
ax = df.plot(kind='bar')
fig = ax.get_figure()
在你的情况下,类似于
ax = 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(ax.get_figure())
最后一个图表应该可以解决问题。
我正在尝试将一系列图表收集到一个列表中,然后将它们全部输出到一个列表中 运行 Excel。
该列表将所有图表显示为 "matplotlib.figure.Figure at (example) 0xb8eae80",但不会显示所有图表。当 prog 为 运行 时,它们都显示在 iPython(在 Spyder 中)控制台中。
如果我尝试在 iPython 中使用 list[i] 单独显示它们,一些图表显示但有些不显示(相同的图表丢失)。
对于 Excel 的输出,"missing" 图表有默认大小的空白 canvas 因此,虽然 XL 正在获取有关图表存在的一些信息,但它不是'甚至得到 "figsize" 信息。
下面示例代码中缺少的图表是针对多线图的图表(这是一个简单的示例,我意识到它可以编写得更好)
感谢您的帮助。
示例代码(与我的原始代码存在相同问题):
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))
对于 Pandas 从 docs:
生成的绘图,您没有正确访问图形对象import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
ax = df.plot(kind='bar')
fig = ax.get_figure()
在你的情况下,类似于
ax = 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(ax.get_figure())
最后一个图表应该可以解决问题。