Stacked bar chart in matplotlib without hardcoding the fields - python

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)

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/

Easily show mean value for plotly express bar plot

Plotly Express's bar chart stacks the observations by default, showing the sum.
import seaborn as sns
import plotly.express as px
df =sns.load_dataset("penguins")
px.bar(data_frame=df, x="species", y="bill_depth_mm")
I'm trying to display the mean for each species, which is what most other popular Python libraries return.
I could manually calculate the mean of each species and make a new dictionary/Data Frame. However I feel like there should be an easy way to display the mean directly from Plotly.
I've checked the docs and SO with no luck. What am I missing?
I don't think you're missing anything. I imagine what the Plotly developers had in mind is that DataFrames being passed to the px.bar method have one y-value per unique category as evidenced by this documentation showing how Plotly Express works with long or wide format data. In the medals dataset, there are 9 bars for 9 unique categories.
As you said, this means that you would need to calculate the mean for each unique species, and this can be accomplished by passing a groupby mean of your DataFrame directly to the data_frame parameter, even if it's not the most elegant.
fig = px.bar(
data_frame=df.groupby(['species']).mean().reset_index(),
x="species",
y="bill_depth_mm"
)

Plotting a boxplot using Pandas

I'm trying to plot a box plot using pandas.
The code for the same using seaborn is as follows:
sns.boxplot(y='Salary', data=eda)
and this is the code I use while using pandas:
boxplot = eda.boxplot(column=['Salary'])
is there any way I could get the same box plot using pandas as I did while using seaborn?
Edit: I need the column 'salary' on the y-axis while using pandas
Thanks!

Footprint Style Stacked Bar Chart Using Matplotlib

I want to create a chart like the one given below. how can I achieve something like that in matplotlib.
.

How to make stacked bar chart in Python?

I have multiple categorical data (tool_id) in my data set and each has total count tested per day. I would like to make a stacked barplot where total count per tool_id that are stacked in a day for the whole month.
What python libraries can be used to easily plot a stacked bar chart and how to code it? I have tried seaborn but looks like it does not have stacked bar chart capability, correct me if I'm wrong.

Categories