This question already has answers here:
X-axis not properly aligned with bars in barplot (seaborn)
(2 answers)
Bar labels in matplotlib/Seaborn
(1 answer)
How to get the label values on a bar chat with seaborn on a categorical data
(2 answers)
Closed 2 months ago.
The code below is the code I am using for a heart failure analysis project. But,
This method is not centering the values of each bar under the graph, pictured below.
I am not getting the percentage value above each bar in the graph
def plot_percentage(df, col, target):
x,y = col, target
temp_df = df.groupby(x)[y].value_counts(normalize=True)
temp_df = temp_df.mul(100).rename('percent').reset_index()
temp_df = temp_df[temp_df.HeartDisease != 0]
order_list = list(df[col].unique())
order_list.sort()
sns.set(font_scale=1.5)
g = sns.catplot(x=x, y='percent', hue=x,kind='bar', data=temp_df, height=8, aspect=2, order=order_list, legend_out=False)
g.ax.set_ylim(0,100)
plt.title(f'{col.title()} By Percent {target.title()}',
fontdict={'fontsize': 30})
plt.xlabel(f'{col.title()}', fontdict={'fontsize': 20})
plt.ylabel(f'{target.title()} Percentage', fontdict={'fontsize': 20})
return g
Related
This question already has answers here:
How to change the figure size of a displot
(2 answers)
How to change a figure's size in Python Seaborn package
(7 answers)
Closed 20 days ago.
How can we adjust the size of the chart in seaborn barplot? I have researched some of the possible parameters, however I didn't find how to adjust. Please see below for reference:
Code:
## Merge in the original country codes provided in the dataset
countries = df[['Country', 'Country_code']].drop_duplicates().reset_index(drop=True)
df_cam2 = df_cam.merge(countries, how='left', on='Country_code')
df_cam2.head()
## Bar graphs
g = sns.FacetGrid(df_cam2, col='Campaign', col_wrap=3)
g.map(sns.barplot, 'Country', 'Accepted (%)', width=0.8)
for ax, pval in zip(g.axes.flat, stat_results):
ax.text(0, 65, "Chisq p-value: "+str(pval), fontsize=9) #add text;
Parameters:
seaborn.barplot(data=None, *, x=None, y=None, hue=None, order=None, hue_order=None, estimator='mean', errorbar=('ci', 95), n_boot=1000, units=None, seed=None, orient=None, color=None, palette=None, saturation=0.75, width=0.8, errcolor='.26', errwidth=None, capsize=None, dodge=True, ci='deprecated', ax=None, **kwargs)
Current output:
This question already has answers here:
How to add value labels on a bar chart
(7 answers)
How to plot and annotate a grouped bar chart
(1 answer)
Annotate bars with values on Pandas bar plots
(4 answers)
Closed 8 months ago.
I want to display a feature from a panda's dataframe, with using the hue parameter.
I keep getting errors of
AttributeError: 'Text' object has no attribute 'get_xticklabels'
and
AttributeError: 'Text' object has no attribute 'patches'
First I tried this code:
graph = sns.countplot(x="Residence_type", hue='stroke' , data=data).set_title("Stroke ~ Residence", size =18, y=1)
sns.set(rc={'figure.figsize':(11,8)})
medians = data.groupby(['Residence_type'])['stroke'].median().values
nobs = data['Residence_type'].value_counts().values
nobs = [str(x) for x in nobs.tolist()]
nobs = ["n: " + i for i in nobs]
pos = range(len(nobs))
for tick,label in zip(pos,graph.get_xticklabels()):
graph.text(pos[tick], medians[tick] + 0.03, nobs[tick], horizontalalignment='center', size='x-small', color='w', weight='semibold')
Didn't work so used this (also didn't work):
graph = sns.countplot(x="Residence_type", hue='stroke' , data=data).set_title("Stroke ~ Residence", size =18, y=1)
sns.set(rc={'figure.figsize':(11,8)})
for p in graph.patches:
graph.annotate('{:.1f}'.format(p.get_height()), (p.get_x()+0.25, p.get_height()+0.01))
This is my plot. I just want the height (the value) of each bin to show up on top of it:
This question already has answers here:
Stacked bars are unexpectedly annotated with the sum of bar heights
(2 answers)
How to add value labels on a bar chart
(7 answers)
Closed 10 months ago.
I want to create a stacked horizontal bar plot with values of each stack displayed inside it and the total value of the stacks just after the bar. Using python matplotlib, I could create a simple barh. My dataframe looks like below:
import pandas as pd
df = pd.DataFrame({"single":[168,345,345,352],
"comp":[481,44,23,58],})
item = ["white_rice",
"pork_and_salted_vegetables",
"sausage_and_potato_in_tomato_sauce",
"curry_vegetable",]
df.index = item
Expect to get bar plot like below except that it is not horizontal:
The code I tried is here...and i get AttributeError: 'DataFrame' object has no attribute 'rows'. Please help me with horizontal bar plot. Thanks.
fig, ax = plt.subplots(figsize=(10,4))
colors = ['c', 'y']
ypos = np.zeros(len(df))
for i, row in enumerate(df.index):
ax.barh(df.index, df[row], x=ypos, label=row, color=colors[i])
bottom += np.array(df[row])
totals = df.sum(axis=0)
x_offset = 4
for i, total in enumerate(totals):
ax.text(totals.index[i], total + x_offset, round(total), ha='center',) # weight='bold')
x_offset = -15
for bar in ax.patches:
ax.text(
# Put the text in the middle of each bar. get_x returns the start so we add half the width to get to the middle.
bar.get_y() + bar.get_height() / 2,
bar.get_width() + bar.get_x() + x_offset,
# This is actual value we'll show.
round(bar.get_width()),
# Center the labels and style them a bit.
ha='center',
color='w',
weight='bold',
size=10)
labels = df.index
ax.set_title('Label Distribution Overview')
ax.set_yticklabels(labels, rotation=90)
ax.legend(fancybox=True)
Consider the following approach to get something similar with matplotlib only (I use matplotlib 3.5.0). Basically the job is done with bar/barh and bar_label combination. You may change label_type and add padding to tweak plot appearance. Also you may use fmt to format values. Edited code with total values added.
import matplotlib.pyplot as plt
import pandas as pd
import random
def main(data):
data['total'] = data['male'] + data['female']
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.suptitle('Plot title')
ax1.bar(x=data['year'].astype(str), height=data['female'], label='female')
ax1.bar_label(ax1.containers[0], label_type='center')
ax1.bar(x=data['year'].astype(str), height=data['male'], bottom=data['female'], label='male')
ax1.bar_label(ax1.containers[1], label_type='center')
ax1.bar_label(ax1.containers[1], labels=data['total'], label_type='edge')
ax1.legend()
ax2.barh(y=data['year'].astype(str), width=data['female'], label='female')
ax2.bar_label(ax2.containers[0], label_type='center')
ax2.barh(y=data['year'].astype(str), width=data['male'], left=data['female'], label='male')
ax2.bar_label(ax2.containers[1], label_type='center')
ax2.bar_label(ax2.containers[1], labels=data['total'], label_type='edge')
ax2.legend()
plt.show()
if __name__ == '__main__':
N = 4
main(pd.DataFrame({
'year': [2010 + val for val in range(N)],
'female': [int(10 + 100 * random.random()) for dummy in range(N)],
'male': [int(10 + 100 * random.random()) for dummy in range(N)]}))
Result (with total values added):
This question already has answers here:
How to rank plot in seaborn boxplot
(2 answers)
How can I sort a boxplot in pandas by the median values?
(4 answers)
Closed 10 months ago.
Mtcars is a public dataset in R. I'm not sure it's a public dataset in python.
mtcars <- mtcars
I created this boxplot in R and part of what I'm doing is reordering the y-axis with the reorder() function.
ggplot(mtcars, aes(x = mpg, y = reorder(origin, mpg), color = origin)) +
geom_boxplot() +
theme(legend.position = "none") +
labs(title = "Mtcars", subtitle = "Box Plot") +
theme(plot.title = element_text(face = "bold")) +
ylab("country")
Now in python I have this boxplot that I created with seaborn:
plt.close()
seaborn.boxplot(x="mpg", y="origin", data=mtcars)
plt.suptitle("Mtcars", x=0.125, y=0.97, ha='left', fontweight = 'bold')
plt.title("boxplot", loc = 'left')
plt.show()
I'm trying to render it now but the same kind of treatment for R doesn't work.
plt.close()
seaborn.boxplot(x="mpg", y=reorder("origin", 'mpg'), data=mtcars)
plt.suptitle("Mtcars", x=0.125, y=0.97, ha='left', fontweight = 'bold')
plt.title("boxplot", loc = 'left')
plt.show()
It's not surprising it doesn't work because it's a different language; I do know that! But how would I do this reordering in python using Seaborn? I'm having trouble understanding if this is even part of the plotting process.
You can compute a custom order and feed it to seaborn's boxplot order parameter:
import seaborn as sns
mtcars = sns.load_dataset('mpg')
order = mtcars.groupby('origin')['mpg'].median().sort_values(ascending=False)
sns.boxplot(x="mpg", y="origin", data=mtcars, order=order.index)
plt.suptitle("Mtcars", x=0.125, y=0.97, ha='left', fontweight = 'bold')
plt.title("boxplot", loc = 'left')
plt.show()
NB. order also acts like a filter, so if values are missing, of non-existent they will be omitted in the graph
output:
This question already has answers here:
Matplotlib pie chart: Show both value and percentage
(2 answers)
Closed 12 months ago.
we are trying with the below code to get the pie charts but we can only see the percentage in pie chart for each category and unable to print its exact values along with percentage.
dfinfoofbuss = pd.read_csv("file.csv", header=None)
dfinfoofbuss.columns = ['ID', 'INFO']
dfinfoofbuss['VALUE_INFO'] = dfinfoofbuss['ID'].str.split('_').str[1]
dfinfoofbusscnt = dfinfoofbuss.groupby(['VALUE_INFO']).size().reset_index(name='COUNT_INFO')
print("dfinfoofbusscnt:",dfinfoofbusscnt)
plotvar3 = dfinfoofbusscnt.groupby(['VALUE_INFO']).sum().plot(kind='pie' ,title='pie chart', figsize=(6,6), autopct='%.2f', legend = False, use_index=False, subplots=True, colormap="Pastel1")
fig3 = plotvar3[0].get_figure()
fig3.savefig("Info.jpg")
Sample Data
VALUE_INFO CountInfo
abc 1
defair 2
cdf 109
aggr 1
sum 1
normal 2
dev 1
Is there a way to print its original values along with percentage in pie chart.. Pls suggest
You will likely need to write your own custom function to get both value and percentage as labels.
Try:
def formatter(x):
return f"{total*x/100:.0f} ({x:.2f})%"
total = sum(dfinfoofbusscnt["CountInfo"])
plotdata = dfinfoofbusscnt.groupby("VALUE_INFO").sum()
>>> plotdata.plot(kind='pie',
title='pie chart',
figsize=(6,6),
autopct=formatter,
colormap="Pastel1",
legend=False,
subplots=True
)