How to create a histogram chart in excel using xlsxwriter? - python

I am trying to create histograms in multiple sheets of an excel workbook. I can very easily crate chart using pandas or matplotlib libraries and show it in python output window. However I would like to plot the histogram chart in excel. I am using office 360. Is there a way to do so?

You can create a histogram chart with XlsxWriter using an Excel "Column" chart type: Column chart example
See also the Working with Charts section of the XlsxWriter docs.

Related

Plotting a grouped stacked bar chart

I am trying to create a grouped, stacked bar chart. I was able to do it in excel and this image shows what I am trying to create but I want to do it through Python. I have all the data in a pandas data frame that is able to create separate stacked bar charts but I cannot get the grouping as seen in excel.
Excel Formatting:
If you could do it in Excel with easy then I strongly suggest you to do it with Excel. Unless you have other requirements.
There are many libraries you can use to create this type of plot: matplotlib, seaborn, or plotly. The one I use most is plotly. You can see the list of sample figures of plotly here: https://plotly.com/python/
Or you can join plotly community, there are many pros there might help with figure. I find there is few pros on figures in stackoverflow to plotly community: https://community.plotly.com/

Python Create boxplot chart in Excel

Using df I need to create boxplot chart in Excel. Thera are many examples how to create charts like ScatterChart but not boxplot.
I do as do succeded to create it as png but not as active chart into excel

Openpyxl to move the position of bar chart within excel sheet

I am using Openpyxl to write several tabs/sheets to an excel file. Why I chose Openpyxl over xlswriter is because I would like to load an existing workbook and create a bar chart using specific columns and rows.
The problem is the bar chart is positioned by default over the data and I would like to position it either below the data or to the right side of data.
Are there parameters that we can use to move the position of the container which holds the bar chart.
We can do this in xlswriter using x_offset and y_offset parameters while inserting the chart. Is there similar functionality with openpyxl.
Any help would be appreciated.
You can position the chart in a cell. For example, the following chart would be inserted in cell B5.
ws.add_chart(chart, "B5")
The top left of the chart is now anchored to B5. You can change the anchor parameter to another cell reference.

Assigning color on Creating Stacked Column chart with xlsxwriter Pandas Python

I was successfully able to generate Stacked Column charts in the newly created Excel sheet using pandas dataframe with xlsxwriter of Python Pandas. But, I can't figure out how to assign color yet.
Here is the picture.
This is from Pandas xlsxwriter documentation. In the given picture, each "Metric" has different colors. For Example, Metric 8 is pink and Metric 1 is blue. I want to assign specific colors to each metric in this example. Obviously, each metric belongs to each row of data in question.
I understand I can do this in excel individually. But, I am writing Python code to generate several dozens of stacked column charts and put it in excel using xlsxwriter. So, it is not practical to do this by hand.
Any help is appreciated !
You need to set the fill color for the series.
See the following Pandas-XlsxWriter stacked charts with colors example. The example uses brew colors but you can replace those with any Html like RGB color.
See also the XlsxWriter Chart documentation and Working with Charts which explain the API and give examples of setting the colors of different properties.

Insert pandas chart into an Excel file using XlsxWriter

I use python 3.4, pandas 0.14.1 and XlsxWriter 0.5.6.
I create a graph called 'graph' using pandas with the following code
graph=data_iter['_DiffPrice'].hist()
, which produces a beautiful histogram.
Now, how do I insert that graph into an Excel file using XlsxWriter?
I tried the XlsxWriter method
workbook.add_chart()
but this creates a graph in Excel, not what I want.
Thanks
If you would like to export Pandas data as charts in Excel using XlsxWriter then have a look at the following how-to (that I wrote): Using Pandas and XlsxWriter to create Excel charts.
If on the other hand you want the matplotlib style charts generated by Pandas then export them as images and insert them into a worksheet using the XlsxWriter insert_image() method.
See also Working with Python Pandas and XlsxWriter.

Categories