This question already has answers here:
Seaborn Catplot set values over the bars
(3 answers)
How to add value labels on a bar chart
(7 answers)
Closed 4 months ago.
This post was edited and submitted for review 4 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I'm plotting a set of sns countplots with 6 different plots, and I'd like to add data labels to all of them without repeating the process. All of the other questions solve the problem for only one plot.
At the last part of the code (for p in ax.patches...) I'd expect the labels to appear on the top of the bars. But nothing happened. It works with only one plot, but not with several plots. This is the result I wanted, but for all of them at once:
This is the result I get (no data labels for any plot):
What am I doing wrong? Is there any easier way of doing it?
# creating the plots
fig, ([ax1, ax2], [ax3, ax4], [ax5, ax6]) = plt.subplots(nrows=3, ncols=2, figsize=(20,15))
sns.countplot(x='sex', data=df, order=df['sex'].value_counts().index, ax=ax1).set(title='Clients by Gender')
sns.countplot(x='age_range', data=df, ax=ax2).set(title='Clients by Age')
sns.countplot(x='children', data=df, ax=ax3).set(title='Clients by Children')
sns.countplot(x='region', data=df, order=df['region'].value_counts().index, ax=ax4).set(title='Clients by Region')
sns.countplot(x='smoker', data=df, order=df['smoker'].value_counts().index, ax=ax5).set(title='Clients by Smoker Option')
sns.countplot(x='bmi_range', data=df, order=df['bmi_range'].value_counts().index, ax=ax6).set(title='Clients by BMI')
for p in ax.patches:
height = p.get_height()
ax.text(x=p.get_x()+p.get_width()/2, y=height+20, s='{:.0f}'.format(height), ha='center')
plt.show()
Related
This question already has answers here:
How to plot seaborn lineplot and barplot on the same plot with same number of y-axes tickers and both y-axes aligned at 0 in Python
(2 answers)
How to line plot timeseries data on a bar plot
(1 answer)
Problem in combining bar plot and line plot (python)
(2 answers)
Closed 7 months ago.
I am trying to combine a line plot and bar plot in seaborn. Code is as below:)
fig, ax1 = plt.subplots(figsize=(10,6))
sns.barplot(x=df_mergedpa['Day'], y=df_mergedpa['pro_mean'],hue=df_mergedpa['Strain'], ax=ax1)
ax2 = ax1.twinx()
sns.lineplot(x=df_mergedpa['Day'],y=df_mergedpa['ami_mean'],hue=df_mergedpa['Strain'],
marker='o', ax=ax1)
The plot I am getting is as above:
Why the line plot is not rendering properly. It is extending in X-Axis. I am not able to figure out why?
Dataframe looks as below:
This question already has answers here:
Python Seaborn Facetgrid change xlabels
(2 answers)
Common xlabel/ylabel for matplotlib subplots
(8 answers)
How to add a shared x-label and y-label to a plot created with pandas plot
(4 answers)
One shared x-axis label for Seaborn FacetGrid subplots (layouts/spacing?)
(1 answer)
How to set common axes labels for subplots
(9 answers)
Closed last year.
What I need should be straighforward but I couldn't find a solution. Say we draw the following seaborn.lmplot:
import seaborn as sns; sns.set_theme(color_codes=True)
tips = sns.load_dataset("tips")
g = sns.lmplot(x="total_bill", y="tip", col="day", hue="day",
data=tips, col_wrap=2, height=3)
I simply want to have a single label for the x-axis and a single label for the y-axis instead of two as currently.
In other words, that the word 'tip' be printed only one time on the centre left of the graph, and that the word 'total_bill' be printed only one time on the bottom centre of the graph.
How do we do this?
EDIT: there is a similar question here One shared x-axis label for Seaborn FacetGrid subplots (layouts/spacing?) yet it is not elaborated and does not solve my issue.
This question already has answers here:
How to add a title to Seaborn Facet Plot
(7 answers)
Closed 5 months ago.
This should be easy, but I've tried many things (based on seaborn relplot: how to control the location of the legend and add title, How to add a title to Seaborn Facet Plot and also the documentation), but nothing worked.
Things I've tried:
g = sns.relplot(x="col1", y="col2", data=df, height=6, aspect=2);
g.add_legend(title="Col1 x Col2");
Also:
g = sns.relplot(x="col1", y="col2", data=df, height=6, aspect=2).set_title('Col1 x Col2')
And:
plt.title('Col1 x Col2')
g = sns.relplot(x="col1", y="col2", data=df, height=6, aspect=2);
Well, nothing worked. I want the title on the top of the graph, just like what plt.title() usually do.
As you failed to include the source DataFrame, I used another one:
df = sns.load_dataset("tips")
Then I created the relplot the following way:
g = sns.relplot(x="total_bill", y="tip", hue="day", data=df)
g.fig.suptitle('Col1 x Col2', fontsize=16)
g.fig.subplots_adjust(top=0.9);
The result I got was:
Of course, the content of the image is different, but at least you have a title here, with the image adjusted down, to leave some space below the
title.
This question already has answers here:
matplotlib colorbar in each subplot
(5 answers)
Closed 3 years ago.
I am creating a (10,7) subplot of multiple different gridded fields. The following code is what is being currently used:
fig, axes = plt.subplots(nrows=10, ncols=7, figsize=(18, 16), dpi= 100,
facecolor='w', edgecolor='k')
titles = ['Z1','Z2','Z3','ZDR1','ZDR2','ZDR3','Dist']
for i in range(0,10):
z = 1*10+i
for j in range(0,7):
aa = axes[i,j].matshow(alldata_sim[z,:,:,j], cmap='jet')
fig.colorbar(aa)
axes[0,j].set_title(titles[j])
axes[i,j].get_xaxis().set_visible(False)
axes[i,j].get_yaxis().set_ticks([])
axes[i,0].set_ylabel(allgauge_sim[z])
Which produces the following figure:
Figure1
The question is: how do I get the colorbars to be on the right-hand side of each respective individual subplot?
maybe try changing
fig.colorbar(aa)
to
fig.colorbar(aa,ax=axes[i,j])
Hope it helps!
This question already has answers here:
How do I change the plot size of a regplot in Seaborn?
(2 answers)
Closed 4 years ago.
I've tried as many solutions as I could find on here, but I'm not having much luck on this. I'm not sure if it's because some of my settings, but I am unable to reshape my Seaborn countplot. Here's my code where I plot the figure:
sns.set_style('whitegrid')
sns.set(font_scale=1.3)
sns.countplot(x=df['QuarterYear'], hue=df['Modifier'])
ax = plt.gca()
for p in ax.patches:
ax.text(p.get_x() + p.get_width()/2., p.get_height(), '%d' % int(p.get_height()),
fontsize=12, color='black', ha='center', va='bottom')
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
I am also editing the legend and labeling my countplot columns in the same block of code.
I am using Jupyter Notebook %inline. If anyone could explain what I am missing, that would be great. I've tried many, many variations of these solutions, to no avail.
How do I change the figure size for a seaborn plot?
How to make seaborn.heatmap larger (normal size)?
How do I change the plot size of a regplot in Seaborn?
Any help would be appreciated. Thank you for your time!
Have you tried:
fig = plt.gcf()
fig.set_size_inches( 16, 10)