This question already has an answer here:
Using pandas value_counts and matplotlib
(1 answer)
Closed 8 months ago.
I have a dataframe with several columns and I need to plot a graph based on the number of counts in the 'Total'column.
I performed the following code:
df['Total'].value_counts()
The output are as follows:
2 10
20 15
4 8
8 20
This means the the number 2 appears in the Total columns 10 times, number 20 appears 15 times and so on.
How do I plot a barchart with the x-axis as the number itself and the y-axis as the occurances and in ascending
order? The x-axis will plot 2 -> 4 -> 8 -> 20.
What are the next steps after:
%matplotlib inline
import matplotlib.pyplot as plt
Consider this as an example:
This denoted your 'Total' column -> [2,2,2,2,2,20,20,20,20,4,4,4,8,8,8,8,8]
import pandas as pd
import matplotlib.pyplot as plt
import collections
total = [2,2,2,2,2,20,20,20,20,4,4,4,8,8,8,8,8]
df = pd.DataFrame(total, columns=['total'])
#print(df.value_counts())
fig,ax = plt.subplots()
df['total'].value_counts().plot(ax = ax, kind = 'bar', ylabel = 'frequency')
plt.show()
This gives the following output:
Related
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')
This question already has answers here:
How to plot and annotate grouped bars in seaborn / matplotlib
(1 answer)
How to add value labels on a bar chart
(7 answers)
Closed 1 year ago.
I want to show my values in hbar diagram but somehow I'm not able to render the values right to the bars.
Here is my code:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
index = ['average', '50th', '95th', '99.9th', 'max']
aaa = [3180, 2153, 9172, 9368, 9432]
bbb = [3857, 3367, 11638, 14555, 14731]
ccc = [740, 716, 1326, 1927, 2591]
df = pd.DataFrame({'aaa': aaa, 'bbb': bbb, 'ccc': ccc}, index=index)
ax = df.plot.barh()
How can I display my values to each bar and is also possible to save the figure for e.g. .png file?
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()
I have the table with 5 columns with 8000 rows:
Market DeliveryWindowID #Orders #UniqueShoppersAvailable #UniqueShoppersFulfilled
NY 296 2 2 5
MA 365 3 4 8
How do I plot a graph in pandas or seaborn that will show the #Order, #UniqueShoppersAvailable, #UniqueShoppersFulfilled v/s the market and delivery window?
Using Seaborn, reshape your dataframe with melt first:
df_chart = df.melt(['Market','DeliveryWindowID'])
sns.barplot('Market', 'value',hue='variable', data=df_chart)
Output:
One way is to set Market as index forcing it onto the x axis and do a bar graph if you wanted a quick visualization. This can be stacked or not.
Not Stacked
import matplotlib .pyplot as plt
df.drop(columns=['DeliveryWindowID']).set_index(df.Market).plot(kind='bar')
Stacked
df.drop(columns=['DeliveryWindowID']).set_index(df.Market).plot(kind='bar', stacked=True)
I want to plot different values of x and y-axis from different CSVs into a simple plot.
csv1:
Time Buff
1 5
2 10
3 15
csv2:
Time1 Buff1
2 3
4 6
5 9
I have 5 different CSVs. I tried plotting to concatenate the dataframes into a single frame and plot it. But I was able to plot with only one x-axis:
df = pd.read_csv('csv1.txt)
df1 = pd.read_csv('csv2.txt)
join = pd.concat([df, df1], axis=1)
join.plot(x='Time', y=['Buff', 'Buff1'], kind='line')
join.plot(x='Time', y='Buff', x='Time1', y='Buff1') #doesn't work
I end up getting a plot with reference with only one x-axis (csv1). But how to plot both x and y column from the CSVs into the same plot?
you can plot two dataframes in the same axis if you specify that axis with ax=. Notice that I created the figure and axis using subplots before i plotted either of the dataframes.
import pandas as pd
import matplotlib.pyplot as plt
f,ax = plt.subplots()
df = pd.DataFrame({'Time':[1,2,3],'Buff':[5,4,3]})
df1 = pd.DataFrame({'Time1':[2,3,4],'Buff1':[5,7,8]})
df.plot(x='Time',y='Buff',ax=ax)
df1.plot(x='Time1',y='Buff1',ax=ax)