I am using the seaborn
s displot()
for plot a CDF distribution of my dataframe.
Here is how I plot the figures.
First figure:
g = sns.displot(data=df, x=df['trip_duration']/60, kind='ecdf',)
g.set(xlim=(0, 180))
g.set_axis_labels("Time (min)","CDF")
plt.title('Trip durations')
And the second figure as:
g = sns.displot(data=df, x=df['trip_duration']/60, kind='ecdf', hue='travelmode', palette=palette)
g.set(xlim=(0, 180))
g.set_axis_labels("Time (min)","CDF")
plt.title('Trip durations per mode')
These figures are plotted separately. I would like to have them in a (1row, 2cols)
format, for easy comparison. Plus, that way the would have the same x-axis
scale. Right now, they have different scales even though I specify the range limit (0, 180)
edit
I tried this (first answer,)
fig, axs = plt.subplots(nrows=2, ncols=1, sharex=True)
# fig, axs = plt.subplots(nrows=1, ncols=2, sharey=True)
g = sns.displot(data=df, x=df['trip_duration']/60, kind='ecdf', ax=axs[0])
g.set(xlim=(0, 180))
g.set_axis_labels("Time (min)","CDF")
axs[0].set_title('Trip durations')
g = sns.displot(data=df, x=df['trip_duration']/60, kind='ecdf', hue='travelmode', palette=palette, ax=axs[1])
g.set(xlim=(0, 180))
g.set_axis_labels("Time (min)","CDF")
axs[1].set_title('Trip durations per mode')
CodePudding user response:
First: you need to pass over matplotlib, a create subplots.
Second:There is a problem in your question if you want one row two columns you cannot share the same X values only Y values. If you want 2 rows 1 column is ok. I leave the options in the code
Once created you need to pass the ax where you want seaborn to plot the figure
fig, axs = plt.subplots(nrows=2, ncols=1, sharex=True)
# fig, axs = plt.subplots(nrows=1, ncols=2, sharey=True)
g = sns.ecdfplot(data=df, x=df['trip_duration']/60, ax=axs[0])
g.set(xlim=(0, 180))
g.set_axis_labels("Time (min)","CDF")
axs[0].set_title('Trip durations')
g = sns.ecdfplot(data=df, x=df['trip_duration']/60, hue='travelmode', palette=palette, ax=axs[1])
g.set(xlim=(0, 180))
g.set_axis_labels("Time (min)","CDF")
axs[1].set_title('Trip durations per mode')