Adjusting Size of Seaborn Plot [duplicate] - python

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)

Related

Add labels to several sns plots at once [duplicate]

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()

How can i plot multiple distribution plots with Seaborn? [duplicate]

This question already has answers here:
seaborn distplot / displot with multiple distributions
(6 answers)
Plotting multiple seaborn displot
(1 answer)
seaborn is not plotting within defined subplots
(1 answer)
Closed 9 months ago.
I am not experienced with plotting in Python. But I have managed to plot a signle distribution plot with Seaborn.
example code:
sns.displot(SD_frame_A,kind="kde")
example plot:
So I tryed to plot three of them in one graph:
example code:
sns.displot(SD_frame_A,kind="kde")
sns.displot(SD_frame_S,kind="kde")
sns.displot(SD_frame_D,kind="kde")
plt.show()
But this will only plot the three distribution separately. Does anyone how I can plot both 3 distribution in one plot?
Thanks for reading!
You can't do that with displot because that is a figure-level function. But you can use kdeplot and provide an axes object:
ax = plt.axes()
sns.kdeplot(SD_frame_A, ax=ax)
sns.kdeplot(SD_frame_S, ax=ax)
sns.kdeplot(SD_frame_D, ax=ax)
plt.show()

How to show the grid in python with squared cells? [duplicate]

This question already has answers here:
How do I equalize the scales of the x-axis and y-axis?
(5 answers)
Closed 2 years ago.
I want to plot a circle on a grid in python. I just need python to show the grid with squared cells. I wrote the following code, but it shows the grid with NON-squared cells.
Can anyone tell me how to make the grid cells be squared ?
import matplotlib.pyplot as plt
import math
p=8
R=0.484*p
t=np.linspace(0, 2*np.pi)
x=R*np.cos(t)
y=R*np.sin(t)
plt.axis("equal")
plt.grid(True, which='both', axis='both')
plt.plot(x,y)
plt.show()
Remove plt.axis("equal") and instead set plt.gca().set_aspect('equal'), which precisely sets the ratio of y-unit to x-unit of the axis scaling:
plt.grid(True, which='both', axis='both')
plt.plot(x,y)
plt.gca().set_aspect("equal")
plt.show()
Which would be the same as setting plt.axis('square').
Note that as mentioned in the docs, plt.axis("equal") is equal to setting plt.gca().set_aspect('equal', adjustable='datalim'), which will not produce the expected output, since data limits may not be respected in this case.
The above will give:
If you add this line after plt.grid() it will write all the x-ticks and the squares will be squared:
plt.xticks(range(-6, 6))

How to show colorbar on each individual matshow subplot [duplicate]

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!

Matplotlib overlapping tickslabels on log-log plot [duplicate]

This question already has an answer here:
Matplotlib: setting x-limits also forces tick labels?
(1 answer)
Closed 5 years ago.
I can't quite figure out how to ask my question properly.
I am trying to set custom x,y ticks on a log-log plot.
Following the answer here I did,
fig1, ax1 = plt.subplots()
ax1.plot([10, 100, 1000], [1,2,3])
ax1.set_xscale('log')
ax1.set_yscale('log')
ax1.set_xticks([20, 200, 500])
ax1.set_yticks([1, 2,3])
ax1.get_xaxis().set_major_formatter
(matplotlib.ticker.ScalarFormatter())
ax1.get_yaxis().set_major_formatter
(matplotlib.ticker.ScalarFormatter())
This results in an overlapping ticklabels on of the axis.
Does anyone knows why is this happening?
This is the relevant github issue :
https://github.com/matplotlib/matplotlib/issues/8386
Quick Solution:
ax.yaxis.set_major_formatter
(matplotlib.ticker.ScalarFormatter())
ax.yaxis.set_minor_formatter
(matplotlib.ticker.NullFormatter())

Categories

Resources