Multiple histograms on same graph with Seaborn `displot` (not `distplot`) - python

Seaborn distplot is deprecated.
With distplot I get the following graph
with this code:
import matplotlib.pyplot as plt
import seaborn as sns
sns.distplot(random.normal(loc=177.8, scale=10.2, size=1000), kde=True)
sns.distplot(random.normal(loc=165.1, scale=8.9, size=1000), kde=True)
plt.show()
How do I achieve the same with displot please? If I just substitute displot for distplot, the histograms are displayed separately.

Any particular reason you want to use displot? Using histplot accomplishes your goal.

Related

Could not save whole figure of barplot with long yticklabel

I want to save a barplot, but found it was clipped when save it to a file.
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
sns.set_theme(font_scale=1.6)
fig,ax = plt.subplots(figsize=(8,6))
g = sns.barplot(x="tip", y="day", data=tips)
g.set(yticklabels=['Thur','Fri','Sat','Very long long long long Sun'])
fig.savefig('1.png',dpi=400)
Here is the figure shown in jupyter notebook
However, the figure saved was looked like this:
You should add bbox_index='tight' as a parameter and argument to plt.savefig()
fig.savefig('1.png',dpi=400, bbox_inches='tight')

Seaborn jointplot options erased with tight_layout

I have a problem with the use of tight_layout combined with jointplot. I have a dataframe df with my datas in two columns.
I create a jointplot with the command:
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
sns.jointplot(df["time (ns)"],df["intesity (counts/s)"], kind='kde', shade=True, cmap="RdBu_r", n_levels=1000, space=0)
The problem is that the axis labels are partially outside of the figure: I solved using
plt.tight_layout()
Anyway this have the side effect to "erase" the space=0 option, resulting in a graph different from what I would like to obtain. Does someone knows how I can solve this problem?
try adding
plt.subplots_adjust(hspace=0, wspace=0)
after tight_layout()

Reset colors back to default [duplicate]

I had a look at Kaggle's univariate-plotting-with-pandas. There's this line which generates bar graph.
reviews['province'].value_counts().head(10).plot.bar()
I don't see any color scheme defined specifically.
I tried plotting it using jupyter notebook but could see only one color instead of all multiple colors as at Kaggle.
I tried reading the document and online help but couldn't get any method to generate these colors just by the line above.
How do we do that? Is there a config to set this randomness by default?
It seems like the multicoloured bars were the default behaviour in one of the former pandas versions and Kaggle must have used that one for their tutorial (you can read more here).
You can easily recreate the plot by defining a list of standard colours and then using it as an argument in bar.
colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd',
'#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf']
reviews['province'].value_counts().head(10).plot.bar(color=colors)
Tested on pandas 0.24.1 and matplotlib 2.2.2.
In seaborn is it not problem:
import seaborn as sns
sns.countplot(x='province', data=reviews)
In matplotlib are not spaces, but possible with convert values to one row DataFrame:
reviews['province'].value_counts().head(10).to_frame(0).T.plot.bar()
Or use some qualitative colormap:
import matplotlib.pyplot as plt
N = 10
reviews['province'].value_counts().head(N).plot.bar(color=plt.cm.Paired(np.arange(N)))
reviews['province'].value_counts().head(N).plot.bar(color=plt.cm.Pastel1(np.arange(N)))
The colorful plot has been produced with an earlier version of pandas (<= 0.23). Since then, pandas has decided to make bar plots monochrome, because the color of the bars is pretty meaningless. If you still want to produce a bar chart with the default colors from the "tab10" colormap in pandas >= 0.24, and hence recreate the previous behaviour, it would look like
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
N = 13
df = pd.Series(np.random.randint(10,50,N), index=np.arange(1,N+1))
cmap = plt.cm.tab10
colors = cmap(np.arange(len(df)) % cmap.N)
df.plot.bar(color=colors)
plt.show()

Seaborn not showing full plot

I am trying to plot the "Tips" dataset from the Seaborn library, but when doing so I am only getting the dark background of the chart. The actual scatter plot and histogram along the edges are not showing.
I am running the code inside Spyder from the Anaconda distribution.
Where am I going wrong?
import seaborn as sns
import matplotlib as plt
sns.set(style="darkgrid", color_codes=True)
#Import data
tips = sns.load_dataset('tips')
tips.head()
sns.jointplot(x='total_bill', y='tip', data='tips')
plt.show()
here data="tips" is not a string
so you need to change the following line:
sns.jointplot(x='total_bill', y='tip', data=tips)

unwanted blank subplots in matplotlib

I am new to matplotlib and seaborn and is currently trying to practice the two libraries using the classic titanic dataset. This might be elementary, but I'm trying to plot two factorplots side by side by inputting the argument ax = matplotlib axis as shown in the code below:
import matploblib.pyplot as plt
import seaborn as sns
%matplotlib inline
fig, (axis1,axis2) = plt.subplots(1,2,figsize=(15,4))
sns.factorplot(x='Pclass',data=titanic_df,kind='count',hue='Survived',ax=axis1)
sns.factorplot(x='SibSp',data=titanic_df,kind='count',hue='Survived',ax=axis2)
I was expecting the two factorplots side by side, but instead of just that, I ended up with two extra blank subplots as shown above
Edited: image was not there
Any call to sns.factorplot() actually creates a new figure, although the contents are drawn to the existing axes (axes1, axes2). Those figures are shown together with the original fig.
I guess the easiest way to prevent those unused figures from showing up is to close them, using plt.close(<figure number>).
Here is a solution for a notebook
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
%matplotlib inline
titanic_df = pd.read_csv(r"https://github.com/pcsanwald/kaggle-titanic/raw/master/train.csv")
fig, (axis1,axis2) = plt.subplots(1,2,figsize=(15,4))
sns.factorplot(x='pclass',data=titanic_df,kind='count',hue='survived',ax=axis1)
sns.factorplot(x='sibsp',data=titanic_df,kind='count',hue='survived',ax=axis2)
plt.close(2)
plt.close(3)
(For normal console plotting, remove the %matplotlib inline command and add plt.show() at the end.)

Categories

Resources