问题
I am trying to create some graphs based on a dataframe I have in Python 3 called back and export them into Excel. I have been using some of the code from the below response response, but when I use it for more than one graph it gives me some weird results:
Can I insert matplotlib graphs into Excel programmatically?
My code is:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import openpyxl
filepath = 'C:\Filepath\Template.xlsx'
writer = pd.ExcelWriter(filepath, engine='xlsxwriter')
back.to_excel(writer, sheet_name='test')
writer.save()
## PLOTS
## ts1 is company 1 and ts2 is company 2
def plot_results(df, ts1, ts2, filepath, cell):
months = mdates.MonthLocator() # every month
fig, ax = plt.subplots()
ax.plot(df['price_date'], df[ts1], label=ts1)
ax.plot(df['price_date'], df[ts2], label=ts2)
ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %Y'))
# ax.set_xlim(datetime.datetime(start_year, start_month_num, start_day_num), datetime.datetime(end_year, end_month_num, end_day_num))
ax.grid(True)
fig.autofmt_xdate()
plt.xlabel('Month/Year')
plt.ylabel('Cumulative Percent Growth')
plt.title('%s and %s Cumulative Percent Growth' % (ts1, ts2))
plt.legend()
plt.savefig('plot.png', dpi=150)
plt.show()
wb = openpyxl.load_workbook(filepath)
ws = wb.active
img = openpyxl.drawing.image.Image('plot.png')
img.anchor(ws.cell(cell))
ws.add_image(img)
wb.save(filepath)
def plot_scatter_ts(df, ts1, ts2, filepath, cell):
plt.xlabel('%s Price ($)' % ts1)
plt.ylabel('%s Price ($)' % ts2)
plt.title('%s and %s Price Scatterplot' % (ts1, ts2))
plt.scatter(df[ts1], df[ts2])
plt.show()
wb = openpyxl.load_workbook(filepath)
ws = wb.active
plt.savefig('plot.png', dpi=150)
img = openpyxl.drawing.image.Image('plot.png')
img.anchor(ws.cell(cell))
ws.add_image(img)
wb.save(filepath)
plot_results(back, 'adj_close_price4.0', 'adj_close_price26.0', filepath, 'P2')
plot_scatter_ts(back, 'adj_close_price4.0', 'adj_close_price26.0', filepath, 'P34')
When I run the functions plot_reults
or plot_scatter_ts
by themselves they run and go into Excel fine. But if I run them together only the graph run last ends up in the Excel document, so in this case the scatter plot. Furthermore I don't really want to see the graphs in the Python interface so if I get rid of the plt.show()
in the plot_results
function the scatter plot for some reason becomes a bar graph, which is weird as neither of those graphs are bar graphs and they are in different functions.
Does anyone know what I am doing wrong?
Thanks
Update 13/6/18
Sorry been a bit busy didn't have a chance to get back to this.
As suggested I have rewritten my code using Pandas xlsxwriter as Screenpaver suggested. But when I try to do more than one plot it still seems to muddle the plots. My code is below:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import openpyxl
filepath = 'C:\...\Template.xlsx'
## Chart 1
def plot_results(df, ts1, ts2, sheet_name, filepath, cell):
## Create Pandas Excel writer using Xlswriter as the engine
writer = pd.ExcelWriter(filepath, engine='xlsxwriter')
back.to_excel(writer, sheet_name=sheet_name, startrow=1, startcol=1)
## Access the Xlswriter workbook and worksheets objects from the dataframe.
workbook = writer.book
worksheet = writer.sheets[sheet_name]
## Create a chart object
chart = workbook.add_chart({'type':'line'})
## Calculate extremes for axes
min_x1 = back[ts1].min()
max_x1 = back[ts1].max()
min_x2 = back[ts2].min()
max_x2 = back[ts2].max()
min_x = min(min_x1, min_x2)
max_x = max(max_x1, max_x2)
## Configure the series of the chart from the dataframe data
chart.add_series({
'name':ts1,
'categories': '=test!$D$3:$D502',
'values':'=test!$C$3:$C502'
})
chart.add_series({
'name':ts2,
'categories': '=test!$D$3:$D502',
'values':'=test!$E$3:$E502'
})
## Configure chart axis
chart.set_x_axis({'name':'Month/Year',
'date_axis':True,
'num_format': 'mm/yy',
'major_gridlines':{
'visible':True,
'line':{'width':1, 'dash_type':'dash'}
}})
chart.set_y_axis({'name':'Cumulative Percent Growth',
'min':min_x,
'max':max_x,
'major_gridlines':{
'visible':True,
'line':{'width':1, 'dash_type':'dash'}
}
})
chart.set_title({'name':'%s and %s Cumulative Percent Growth' % (ts1, ts2)})
chart.set_legend({'position':'bottom'})
chart.set_chartarea({'border':{'none':True}})
## Insert chart into worksheet
worksheet.insert_chart(cell, chart)
writer.save()
## Chart 2
def plot_scatter_ts(df, ts1, ts2, sheet_name, filepath, cell):
## Create Pandas Excel writer using Xlswriter as the engine
writer = pd.ExcelWriter(filepath, engine='xlsxwriter')
back.to_excel(writer, sheet_name=sheet_name, startrow=1, startcol=1)
## Access the Xlswriter workbook and worksheets objects from the dataframe.
workbook = writer.book
worksheet = writer.sheets[sheet_name]
## Create a chart object
chart = workbook.add_chart({'type':'scatter'})
min_x1 = back[ts1].min()
max_x1 = back[ts1].max()
min_x2 = back[ts2].min()
max_x2 = back[ts2].max()
## Configure the series of the chart from the dataframe data
chart.add_series({
# 'name':'Series1',
'categories': 'test!$E$3:$E502',
'values':'=test!$C$3:$C502'
})
## Configure chart axis
chart.set_x_axis({'name':ts1,
'min':min_x2,
'max':max_x2})
chart.set_y_axis({'name':ts2,
'min':min_x1,
'max':max_x1})
chart.set_title({'name':'%s and %s Price Scatterplot' % (ts1, ts2)})
chart.set_legend({'none':True})
chart.set_chartarea({'border':{'none':True}})
## Insert chart into worksheet
worksheet.insert_chart(cell, chart)
writer.save()
plot_scatter_ts(back, 'adj_close_price4.0', 'adj_close_price26.0', 'test2', filepath, 'Q18')
plot_results(back, 'series1', 'series2', 'test2', filepath, 'Q2')
Individually when run with the other commented out each of the function turns out fine but when I run both functions I get a single jumbled graph.
Thanks
回答1:
Thanks screenpaver got it working by moving write out of the functions like below:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import openpyxl
filepath = 'C:\...\Template.xlsx'
sheet_name='test'
writer = pd.ExcelWriter(filepath, engine='xlsxwriter')
## Chart 1
def plot_results(writer, df, ts1, ts2, sheet_name, filepath, cell):
## Create Pandas Excel writer using Xlswriter as the engine
df.to_excel(writer, sheet_name=sheet_name, startrow=1, startcol=1)
## Access the Xlswriter workbook and worksheets objects from the dataframe.
workbook = writer.book
worksheet = writer.sheets[sheet_name]
## Create a chart object
chart = workbook.add_chart({'type':'line'})
## Calculate extremes for axes
min_x1 = df[ts1].min()
max_x1 = df[ts1].max()
min_x2 = df[ts2].min()
max_x2 = df[ts2].max()
min_x = min(min_x1, min_x2)
max_x = max(max_x1, max_x2)
## Configure the series of the chart from the dataframe data
chart.add_series({
'name':ts1,
'categories': '=test!$D$3:$D502',
'values':'=test!$C$3:$C502'
})
chart.add_series({
'name':ts2,
'categories': '=test!$D$3:$D502',
'values':'=test!$E$3:$E502'
})
## Configure chart axis
chart.set_x_axis({'name':'Month/Year',
'date_axis':True,
'num_format': 'mm/yy',
'major_gridlines':{
'visible':True,
'line':{'width':1, 'dash_type':'dash'}
}})
chart.set_y_axis({'name':'Cumulative Percent Growth',
'min':min_x,
'max':max_x,
'major_gridlines':{
'visible':True,
'line':{'width':1, 'dash_type':'dash'}
}
})
chart.set_title({'name':'%s and %s Cumulative Percent Growth' % (ts1, ts2)})
chart.set_legend({'position':'bottom'})
chart.set_chartarea({'border':{'none':True}})
## Insert chart into worksheet
worksheet.insert_chart(cell, chart)
## Chart 2
def plot_scatter_ts(writer, df, ts1, ts2, sheet_name, filepath, cell):
## Create Pandas Excel writer using Xlswriter as the engine
df.to_excel(writer, sheet_name=sheet_name, startrow=1, startcol=1)
## Access the Xlswriter workbook and worksheets objects from the dataframe.
workbook = writer.book
worksheet = writer.sheets[sheet_name]
## Create a chart object
chart = workbook.add_chart({'type':'scatter'})
min_x1 = df[ts1].min()
max_x1 = df[ts1].max()
min_x2 = df[ts2].min()
max_x2 = df[ts2].max()
## Configure the series of the chart from the dataframe data
chart.add_series({
# 'name':'Series1',
'categories': 'test!$E$3:$E502',
'values':'=test!$C$3:$C502'
})
## Configure chart axis
chart.set_x_axis({'name':ts1,
'min':min_x2,
'max':max_x2})
chart.set_y_axis({'name':ts2,
'min':min_x1,
'max':max_x1})
chart.set_title({'name':'%s and %s Price Scatterplot' % (ts1, ts2)})
chart.set_legend({'none':True})
chart.set_chartarea({'border':{'none':True}})
## Insert chart into worksheet
worksheet.insert_chart(cell, chart)
plot_scatter_ts(writer, back, 'series1', 'series2', sheet_name, filepath, 'Q18')
plot_results(writer, back, 'series1', 'series2', sheet_name, filepath, 'Q2')
writer.save()
来源:https://stackoverflow.com/questions/50694835/putting-multiple-matplotlib-graphs-into-excel-using-python-3