Hi I have some basic code below which has M values on the x-axis and N-values on the y-axis.
I would like to be able to create the chart which I get from using matplotlib but in Microsoft Excel. Is this possible?
I've tried: chart = workbook.add_chart({'type': 'scatter'})
but without any success. Is there an alternative 'type' which would allow me to get a similar chart to that which I get in matplotlib?
M = [-2.29, 33.63, 66.52, 96.37, 123.10, 146.79, 167.49, 185.20, 199.94, 211.68, 220.44, 226.22, 229.02, 230.25, 227.07, 220.91, 211.77, 199.64, 184.53, 164.48, 141.67, 0.00]
N = [-292.20, 6.65, 305.47, 604.25, 901.94, 1198.81, 1495.67, 1792.54, 2089.41, 2386.27, 2683.14, 2980.01, 3276.88, 3561.89, 3858.76, 4155.63, 4452.49, 4749.36, 5046.23, 5361.80, 5666.67, 5666.67]
import pandas as pd
# Create a Pandas dataframe from some data for M.
df1 = pd.DataFrame({'M (kNm)': [M[0], M[1], M[2],
M[3], M[4], M[5], M[6], M[7],
M[8], M[9], M[10], M[11], M[12],
M[13], M[14], M[15], M[16], M[17],
M[18], M[19], M[20], M[21]]})
# Create a Pandas dataframe from some data for N.
df2 = pd.DataFrame({'N (kN)': [N[0], N[1], N[2],
N[3], N[4], N[5], N[6], N[7],
N[8], N[9], N[10], N[11], N[12],
N[13], N[14], N[15], N[16], N[17],
N[18], N[19], N[20], N[21]]})
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('MN_example.xlsx', engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
df1.to_excel(writer, sheet_name='Sheet1', index=False)
df2.to_excel(writer, sheet_name='Sheet1', startcol=1, index=False)
# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Create a chart object.
chart = workbook.add_chart({'type': 'scatter'})
# Configure the series of the chart from the dataframe data.
chart.add_series({'values': '=Sheet1!$A$2:$B$23'})
# Insert the chart into the worksheet.
worksheet.insert_chart('D2', chart)
# Close the Pandas Excel writer and output the Excel file.
writer.save()
import matplotlib.pyplot as plt
plt.plot(M, N, linewidth=2)
plt.show()
You almost have it, you just need to specify the chart subtype and also specify the data categories that the chart should be plotted on.
Replace the chart initialization and configuration with the following and your should get what you want:
# Create a chart object.
chart = workbook.add_chart({'type': 'scatter',
'subtype': 'smooth'})
# Configure the series of the chart from the dataframe data.
chart.add_series({
'categories': ['Sheet1', 1, 0, len(df2), 0],
'values': ['Sheet1', 1, 1, len(df2), 1]})
Output:
Related
Task: I am writing the dataframe/tables into excel file with the insightful charts. To write into excel file I am using xlsxwriter for adding charts and everything.
I have attached one sample output which currently I am getting.
Generated the pie chart from the percentages reference. For adding percentages into the excel file I have set the formulas. So I don't have any reference to the values.
I tried different configurations but I didn't get any satisfactory results.
Here, is one sample code to get the same chart with the same problem which currently I am facing.
The data points are overlapping.
import xlsxwriter
# Create a workbook and add a worksheet
workbook = xlsxwriter.Workbook('pie_chart.xlsx')
worksheet = workbook.add_worksheet()
# Add the data to the worksheet
data = [84,11,2,0,2]
labels = ['A', 'B', 'C', 'D', 'E']
worksheet.write_column('A1', data)
worksheet.write_column('B1', labels)
# Create a pie chart
pie_chart = workbook.add_chart({'type': 'pie'})
pie_chart.add_series({
'name': 'Pie Chart',
'categories': '=Sheet1!$B$1:$B$5',
'values': '=Sheet1!$A$1:$A$5',
'data_labels': {'percentage': True},
})
# Insert the pie chart into the worksheet
worksheet.insert_chart('D1', pie_chart)
# # Save the workbook
workbook.close()
Current output:
Expected output:
The pie chart without data points overlapping.
Just to be clear, XlsxWriter isn't overlapping or positioning the labels, Excel is doing it when it renders the file format.
In general Excel tries to avoid overlapping labels when rendering charts but it can happen if the data segments are very small or close together. You can maybe make it a bit better by specifying leader lines and best_fit positioning. Something like this:
import xlsxwriter
# Create a workbook and add a worksheet
workbook = xlsxwriter.Workbook('pie_chart.xlsx')
worksheet = workbook.add_worksheet()
# Add the data to the worksheet
data = [84,11,2,0,2]
labels = ['A', 'B', 'C', 'D', 'E']
worksheet.write_column('A1', data)
worksheet.write_column('B1', labels)
# Create a pie chart
pie_chart = workbook.add_chart({'type': 'pie'})
pie_chart.add_series({
#'name': 'Pie Chart',
'categories': '=Sheet1!$B$1:$B$5',
'values': '=Sheet1!$A$1:$A$5',
'data_labels': {'percentage': True,
'leader_lines': True,
'position': 'best_fit'},
})
# Insert the pie chart into the worksheet
worksheet.insert_chart('D1', pie_chart)
# # Save the workbook
workbook.close()
Output:
In Excel it is also possible to manually position the data labels for finer grained adjustment but that isn't supported by XlsxWriter.
I need to plot a line chart of the below data frame. Columns are shown in yellow and blue need to come in the value axis(except 0.8 and 1.6) and the other three(Device_ID, Temp, and supply ) need to come in the categories axis.A sample of my data frame is given below. May I know how to approach this? Below is my code. Could you please tell me how to put more than one value in the axis.
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
multi_index.to_excel(writer, sheet_name='Sheet1')
workbook = writer.book
worksheet = writer.sheets['Sheet1']
chart = workbook.add_chart({'type': 'line'})
chart.add_series({
'values': '=Sheet1!$D$2:$D$'+rows,
'categories':'=Sheet1$A$2:$A$'+rows,
})
worksheet.insert_chart('F2', chart)
writer.save(
I run the below code
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
multi_index.to_excel(writer, sheet_name='Sheet1')
workbook = writer.book
worksheet = writer.sheets['Sheet1']
chart = workbook.add_chart({'type': 'line'})
chart.add_series({
'categories': '=(Sheet1!$A$2:$A$456)',
'values': '=(Sheet1!$D$2:$D$456,Sheet1!$E$2:$E$456)'
})
worksheet.insert_chart('H2', chart)
writer.save()
But in the plot, only one cure was there and the X-axis parameters are coming on top.Please see the below image
I am trying to create a bar chart in excel in reverse order categories.
The following does the trick:
chart.set_y_axis({ 'reverse': True})
But then the the x-axis values are on top of the chart, while I want to keep them on the bottom.
I tried to reverse the order then move the x-axis labels via:
chart.set_x_axis({ 'label_position': 'low'})
This does not seem to do anything. That is strange, because if I don't reverse the y-axis and just set the x-axis position to 'high", then it does move the x-axis values to the top. So I don't know why it won't also move it to the bottom.
How can I reverser the order of categories in an excel bar chart, while keeping the x-axis values at the bottom?
Example code:
import pandas as pd
# Some sample data to plot.
list_data = [10, 20, 30, 20, 15, 30, 45]
# Create a Pandas dataframe from the data.
df = pd.DataFrame(list_data)
# Create a Pandas Excel writer using XlsxWriter as the engine.
excel_file = 'column.xlsx'
sheet_name = 'Sheet1'
writer = pd.ExcelWriter(excel_file, engine='xlsxwriter')
df.to_excel(writer, sheet_name=sheet_name)
workbook = writer.book
worksheet = writer.sheets[sheet_name]
# Create a chart object.
chart = workbook.add_chart({'type': 'bar'})
# Reverse Order
chart.set_y_axis({ 'reverse': True,
'label_position': 'low'
})
# Try and fail to get x-axis values on bottom instead of top
chart.set_x_axis({ 'label_position': 'low'
})
# Configure the series of the chart from the dataframe data.
chart.add_series({
'values': '=Sheet1!$B$2:$B$8',
})
# Insert the chart into the worksheet.
worksheet.insert_chart('D2', chart)
# Close the Pandas Excel writer and output the Excel file.
writer.save()
P.s. Not sure if such comments are allowed here, but xlsxwriter is the best thing since sliced bread!
To get the type of output that you want in Excel (and XlsxWriter) you need to set the crossing point for the axis using the "Axis crosses at maximum category" option.
With XlsxWriter you can do it using the axis crossing parameter. Like this:
import pandas as pd
# Some sample data to plot.
list_data = [10, 20, 30, 20, 15, 30, 45]
# Create a Pandas dataframe from the data.
df = pd.DataFrame(list_data)
# Create a Pandas Excel writer using XlsxWriter as the engine.
excel_file = 'column.xlsx'
sheet_name = 'Sheet1'
writer = pd.ExcelWriter(excel_file, engine='xlsxwriter')
df.to_excel(writer, sheet_name=sheet_name)
workbook = writer.book
worksheet = writer.sheets[sheet_name]
# Create a chart object.
chart = workbook.add_chart({'type': 'bar'})
# Reverse Order
chart.set_y_axis({'reverse': True,
'crossing': 'max'})
# Configure the series of the chart from the dataframe data.
chart.add_series({'values': '=Sheet1!$B$2:$B$8'})
# Insert the chart into the worksheet.
worksheet.insert_chart('D2', chart)
# Close the Pandas Excel writer and output the Excel file.
writer.save()
Output:
I have a plot that needs names as the values for the x-axis. I imagine this is accomplished via the set_x_axis function for chart objects but can't find the proper key in the documentation (http://xlsxwriter.readthedocs.io/chart.html#set_x_axis). The following code produces the graph below:
chart = workbook.add_chart({'type':'scatter'})
colLetter = alphabet[1] #alphabet is list of alphabet
for ii in range(4):
colLetter = alphabet[ii+1]
chart.add_series({
'name': '=Sheet1!$%s$1'%colLetter,
'categories': '=Sheet1!$A$2:$A$'+str(lastRowNumber),
'values': '=Sheet1!$%s$2:$%s$6'%(colLetter, colLetter),
})
chart.set_title({'name': 'Cityblock'})
chart.set_x_axis({'name': 'Subject'})
chart.set_y_axis({'name': 'Distance'})
chart.set_style(11)
worksheet.insert_chart('F1', chart)
Any suggestions? I'm using xlsxwriter with python 2.7.
Just set the series categories to point to the strings, or numbers, or dates that you wish to plot.
For example:
import xlsxwriter
workbook = xlsxwriter.Workbook('chart.xlsx')
worksheet = workbook.add_worksheet()
# Add a column chart.
chart = workbook.add_chart({'type': 'column'})
# Write some data to add to plot on the chart.
worksheet.write_column('A1', ['Bob', 'Eve', 'Ann'])
worksheet.write_column('B1', [5, 10, 7])
# Configure the chart.
chart.add_series({'categories': '=Sheet1!$A$1:$A$3',
'values': '=Sheet1!$B$1:$B$3'})
# Insert the chart into the worksheet.
worksheet.insert_chart('D1', chart)
workbook.close()
Output:
I have created a for loop where I am writing a dataframe to an excel file and then plot the datafame and saving the image as a png file. Within the loop, i would like to add each of the saved images to each excel worksheet. I know how to do this outside the loop using openplyx but that would be way too much unnecessary code.
Any ideas of how to insert the figs into each of the worksheets within the workbook?
Here is my code:
writer = pd.ExcelWriter('america.xlsx')
countrylist = countrylist.set_index(['COUNTRY_NAME'])
for i in countrylist.index:
var = i
test = likes[likes['COUNTRY_NAME'].map(str.strip)== str((var))]
test.rename(columns={'YEAR': 'Year'}, inplace=True)
test.to_excel(writer, var, index=False, columns = ['Year', 'Area', 'Region'])
ax = test.plot.bar('Year', ['Area', 'Region'], align = 'center', alpha=0.5, title=var)
fig = ax.get_figure()
folder = 'ra/'
fig.savefig(folder + var + '.png')