Plotting a grouped stacked bar chart - python

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/

Related

Stacked bar chart in matplotlib without hardcoding the fields

I am trying to create a simple stacked bar chart using this data using ONLY matplotlib, pandas and numpy:
x-axis: Month
Labels: activity type
Height: distance
The examples I see, loop over the x-axis, but labels are always hard coded.
Can we loop over everything? In other words, can I create a chart without modifying this table:
No group by or pivot, just use the table as is and get the chart using matplotlib, pandas and numpy only (learning those at the moment)

Bokeh line chart is plotting weird

I am trying to plot chart in my data science project. And I want to plot chart in this way
which is produced by Matplotlib library. And now I want to plot in the same way. But it gives me this way in bokeh.
My data for this in the form of python list and it is as below:
ages = [45.0,50.0,55.0,40.0,60.0,35.0,65.0,30.0,70.0,25.0,75.0,20.0,80.0,85.0,15.0,90.0,10.0,0.0]
ageCounter = [4466,4270,3824,3576,3240,2850,2527,2358,1968,1544,981,655,419,149,132,80,17,2]
Is there any thing I am missing.
Thanks in advance.
You need to sort the arrays together by age. Bokeh plots the points in the order you pass them in. The data above is out of order which explains the "zigzag".

How to delete a plot from chart space in pptx python?

I'd like to delete plot that is in the chart space. Does any one know how to do that with pptx python? Since there's option to delete plots in powerpoint GUI. Is there any way I can do that with python pptx?
I have four charts in a slide and I want to first store the shapes of plots and then delete the plots inside of the charts.
Reading the docs I found this paragraph that shows in more detail how plots work:
A plot is like a sub-chart, containing one or more series and drawn as
a particular chart type, like column or line. This distinction is
needed for charts that combine more than one type, like a line chart
appearing on top of a column chart. A chart like this would have two
plot objects, one for the series appearing as columns and the other
for the lines. Most charts only have a single plot and python-pptx
doesn’t yet support creating multi-plot charts, but you can access
multiple plots on a chart that already has them.
From your question I assume that you want to delete the plot to replace it with a new one. I suggest that you delete the series inside the existing plot and add to it the new series you need.

How to create a histogram chart in excel using xlsxwriter?

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.

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.

Categories