Matplotlib bar chart X-axis Labels order - python

Is there a way I can rearrange the x-axis of my bar chart so that the ages are listed in order?

Add a sort_index() after value_counts()
www['Age'].value_counts().sort_index().plot(kind='bar')

Related

Plotting stacked bar chart

I'm trying to create a stacked bar chart, with
xaxis = 'customer_id(count), yaxis = 'age_band',
and the 3 different loyalty groups stacked in the chart (hue), so I should see 6 bars each with 2-3 different colours.
code I've tried:
df.groupby(['age_band','loyalty']).agg(Loyalty=('customer_id', pd.Series.nunique)).plot(kind='bar', stacked=True)
Picture for reference:
IIUC use DataFrame.pivot_table:
(df.pivot_table(index='age_band',columns='loyalty', values='customer_id', aggfunc='nunique')
.plot(kind='bar', stacked=True))

X-axis over crowding using matplotlib

I'm plotting data points for ever day of the year but the x-axis is very overcrowded as a result.
How can I reduce the number of ticks on the x-axis?
I've tried using:
ax.set_xticks(ax.get_xticks()[::2])
and
plt.locator_params(axis='x', nbins=10)
But that then just seems to cut the range of xticks instead of condensing them:

Plot sub-bar charts on a dataframe groupby

Hi I am having some trouble plotting sub-bar charts after a dataframe groupby
Post groupby, the data is as per the below :
I tried the below to create a bar chart.
df_temp[df_temp.index =='ABC'].unstack().plot.bar(figsize=(10,2))
How can I plot a bar charts where the x-axis is the date and y-axis is the count and each row (ABC and EFG) is its own subplot (vertically stacked)
Example below
thanks for your help !
thanks to #r-beginnners
#remove the multi-level column
df.columns = df.columns.droplevel()
#plot the sub-plots
# if y-axis scale to be the same, use sharey=True
df.T.plot(subplots=True, layout=(2,1), kind='bar', sharey=True)

pandas - draw a distribution of a column

The DataFrame is as the following:
And I'd like to draw a distribution of population of the groupby geo_name, but when I use the following command:
df.hist(column='population')
The histogram is not each bar for geo_name's population:
For example, there should be two top bars from (Ont.) and (Que.), but there is only one bar which is much higher than others.
What's the matter? How to resolve it?
I think you're looking for a bar chart of populations, one bar per province, with provinces arranged along the horizontal axis. If so, try this:
df['population'].plot(kind='bar')

how to plot a pie chart from a dataframe using crosstab function in python

i am trying to plot a pie chart using crosstab function from 2 columns in a dataframe where until now i am able to plot a bar chart using the below statement.
sample of the dataframe:
pd.crosstab(df['event_location'],df['event_type']).iplot(kind="bar", bins=20, theme="white", title="Event type over Location",xTitle='location', yTitle='Number of person')
my question is how to convert this bar chart into a pie chart ?
I guess you are trying to display the number of occurrences for every event type. This simple code will help you plot pie charts per location.
import matplotlib.pyplot as plt
ct = pd.crosstab(df['event_location'],df['event_type'])
ct.plot.pie(subplots=True)
plt.legend(title='XYZ')
plt.show()

Categories

Resources