Matplotlib - Value label on bar graph [duplicate] - matplotlib

This question already has answers here:
How to add value labels on a bar chart
(7 answers)
Closed 6 months ago.
Hi I would like to put value labels on the bar graph below:
df = pd.DataFrame({'Percentile':[25, 50, 75] , "Price in GBP":[
10.000000 ,
50.000000 ,
453.750000
]})
df.plot.bar(x='Percentile', y="Price in GBP", rot=0,grid=True)
plt.ylabel("Price in GBP")
plt.title("Business Coach - Price Distribution")
plt.show()
The graph should look like this:
I have searched a lot, but sadly can't find a relevant solution that works. Thanks

To add text to a chart, you'll need to add each label one at a time by looping through the data.
The bars are located at 0, 1, and 2, which is why range(len(df)) is used rather than df["Percentile"]. I also added in an offset (-.1 and +5) to x and y so that the text appears centered over the bar. Experiment with removing/adjusting those offsets to see how the output changes.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'Percentile':[25, 50, 75] , "Price in GBP":[
10.000000 ,
50.000000 ,
453.750000
]})
df.plot.bar(x='Percentile', y="Price in GBP", rot=0,grid=True)
# add the labels one at a time
for x, price in zip(range(len(df)), df["Price in GBP"]):
plt.text(x - .1, price + 5, price)
plt.show()
You can spend a ton of time adjusting the formatting, but this should get you started.

Related

How can I change my yticks in matplotlib? I Want to be like "0, 5, 10..." (currently getting a "0.5, 1.0..." [duplicate]

This question already has answers here:
set y-axis in millions [duplicate]
(3 answers)
How do I make the numbers on the y-axis show values in millions instead of in scientific notation in matplotlib?
(1 answer)
Closed 3 months ago.
enter image description here
import matplotlib.pyplot as plt
cidades = [city for city, df in mv_cidade.groupby('Loja')]
plt.bar(mv_cidade['Loja'], mv_cidade['Faturamento'])
plt.ylabel('Faturamento em milhões')
plt.xlabel('Loja (por Estado)')
plt.xticks(cidades, rotation= 'vertical', size= 9)
plt.show()
It's kinda simple. I just wanna know how to change the yticks (0.5, 1.0, 1.5) to something like (5, 10, 15, 20)
Can anyone help?
I've already tried to put the "original" values
plt.yticks([6480300, 6868600, 7060500, 7303000, 7441800, 13111300, 14087900, 14867800, 22098300])
that was the result

Stacked bar chart for a pandas df [duplicate]

This question already has answers here:
Using pandas crosstab to create a bar plot
(2 answers)
count plot with stacked bars per hue [duplicate]
(1 answer)
How to have clusters of stacked bars
(10 answers)
Closed 7 months ago.
I have a df like this and would like to plot stacked bar chart where in the x axis is Component and the y-axis shows the count by 'Major', 'Minor' etc.
Component Priority
0 Browse Groups Minor
1 Notifications Major
2 BI Major
3 BI Minor
4 BI Minor
For example, the first bar would have 1st component with a count of 1 minor,..so on.. and 3rd would have 'BI' in x-axis with 1 count of Major and 2 counts of Minor stacked.
What is the simplest way to do this in seaborn or something similar?
You can groupby both columns and count on Priority, then unstack and plot as stacked bar chart:
df.groupby(['Component', 'Priority']).Priority.count().unstack().plot.bar(stacked=True)
Example:
import pandas as pd
df = pd.DataFrame({'Component': list('abccc'), 'Priority': ['Minor', 'Major', 'Major', 'Minor', 'Minor']})
df.groupby(['Component', 'Priority']).Priority.count().unstack().plot.bar(stacked=True)
As an alternative, you can use a crosstab:
pd.crosstab(df.Component, df.Priority).plot.bar(stacked=True)
If you want to use seaborn (I only now saw the seaborn tag), you can use a displot:
import seaborn as sns
sns.displot(x='Component', hue='Priority', data=df, multiple='stack')

Apply "date tick labels" on seaborn chart [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'd like to apply date tick labels to my chart but the dates do not show up. There is no error messages so I guess the function cannot find the dates from my data.
For the final result I'd like to show ticks on the x-axis to mark each month but only show text (i.e. '2020-01') every 6 months.
Example
d = {'sel_date': pd.date_range('2020-01-01', '2021-01-01', 24).strftime('%Y-%m'), 'ID':list('abbb')*6, 'index':np.random.randint(10, size=24)}
df = pd.DataFrame(data=d)
df.head()
sel_date ID index
0 2020-01 a 5
1 2020-01 b 1
2 2020-02 b 0
3 2020-02 b 2
4 2020-03 a 2
import matplotlib.dates as mdates
from matplotlib import pyplot as plt
import seaborn as sns
fig, ax = plt.subplots(figsize=(15, 7))
plot = pd.pivot_table(df, values='index', index=['sel_date'],
columns=['ID'], aggfunc=np.mean)
plot.fillna(0, inplace=True)
ax = sns.lineplot(data=plot)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2)
# Major ticks every 6 months.
fmt_half_year = mdates.MonthLocator(interval=6)
ax.xaxis.set_major_locator(fmt_half_year)
# Minor ticks every month.
fmt_month = mdates.MonthLocator()
ax.xaxis.set_minor_locator(fmt_month)
I'd like the chart's xticks in this style.
If you want to have minor ticks every period and major ticks every six, then maybe this will help.
# Import tick-locator instance
from matplotlib.ticker import MultipleLocator
# Save pivoted data as `x`
x = pd.pivot_table(df, values='index', index='sel_date', columns='ID', aggfunc='mean')
x = x.fillna(0)
# Plot dates on x-axis and 'a' on y-axis
plt.plot(x.index, x['a'].values, ls='-', label='a')
# Plot dates on x-axis and 'b' on y-axis
plt.plot(x.index, x['b'].values, ls='--', label='b')
# Set minor ticks
plt.axes().xaxis.set_minor_locator(locator=MultipleLocator(1))
# Set major ticks
plt.axes().xaxis.set_major_locator(locator=MultipleLocator(6))
# Add legend and show
plt.legend()
plt.show()

How to change a seaborn histogram plot to work for hours of the day?

I have a pandas dataframe with lots of time intervals of varying start times and lengths. I am interested in the distribution of start times over 24hours. I therefore have another column entitled Hour with just that in. I have plotted a histogram using seaborn to look at the distribution but obviously the x axis starts at 0 and runs to 24. I wonder if there is a way to change so it runs from 8 to 8 and loops over at 23 to 0 so it provides a better visualisation of my data from a time perspective. Thanks in advance.
sns.distplot(df2['Hour'], bins = 24, kde = False).set(xlim=(0,23))
If you want to have a custom order of x-values on your bar plot, I'd suggest using matplotlib directly and plot your histogram simply as a bar plot with width=1 to get rid of padding between bars.
import pandas as pd
import numpy as np
from datetime import datetime
import matplotlib.pyplot as plt
# prepare sample data
dates = pd.date_range(
start=datetime(2020, 1, 1),
end=datetime(2020, 1, 7),
freq="H")
random_dates = np.random.choice(dates, 1000)
df = pd.DataFrame(data={"date":random_dates})
df["hour"] = df["date"].dt.hour
# set your preferred order of hours
hour_order = [8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,0,1,2,3,4,5,6,7]
# calculate frequencies of each hour and sort them
plot_df = (
df["hour"]
.value_counts()
.rename_axis("hour", axis=0)
.reset_index(name="freq")
.set_index("hour")
.loc[hour_order]
.reset_index())
# day / night colour split
day_mask = ((8 <= plot_df["hour"]) & (plot_df["hour"] <= 20))
plot_df["color"] = np.where(day_mask, "skyblue", "midnightblue")
# actual plotting - note that you have to cast hours as strings
fig = plt.figure(figsize=(8,4))
ax = fig.add_subplot(111)
ax.bar(
x=plot_df["hour"].astype(str),
height=plot_df["freq"],
color=plot_df["color"], width=1)
ax.set_xlabel('Hour')
ax.set_ylabel('Frequency')
plt.show()

bars not proportional to value - matplotlib bar chart [duplicate]

This question already has an answer here:
Difference in plotting with different matplotlib versions
(1 answer)
Closed 4 years ago.
I am new to matplotlib and am trying to plot a bar chart using pyplot. Instead of getting a plot where the height of bar represents the value, I am getting bars that are linearly increasing in height while their values are displayed on the y-axis as labels.
payment_modes = ['Q', 'NO', 'A', 'C', 'P', 'E', 'D']
l1=[]
l2=[]
for i in payment_modes:
l.append(str(len(df[df['PMODE_FEB18']==i])))
# here l = ['33906', '37997', '815', '4350', '893', '98', '6']
plt.figure()
plt.bar(range(7),l)
This is what I am getting:
The problem is that you seem to be feeding bar with strings, not with numerical quantities. If you instead use the actual numerical quantities, bar will behave as you would expect:
import matplotlib.pyplot as plt
l = [33906, 37997, 815, 4350, 893, 98, 6]
plt.figure()
plt.bar(range(7),l)
plt.show()
gives