i am dealing with a dataset that shows relationships between two points, such as bus stops. For example, we have bus stops A, B, C, and D.
I want to make histogram p
As seen from the documentation, sns.boxplot
has an argumen order
order
,hue_order
: lists of strings, optional
Order to plot the categorical levels in, otherwise the levels are inferred from the data objects.
Using this like
sns.boxplot(..., order=['A','B','C','D'])
would give you the desired plot.
Complete code:
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
time=np.random.randn(1000)
point1 = ['A','B','C','D'] * 250
point2 = ['A'] * 250 + ['B'] * 250 + ['C'] * 250 + ['D'] * 250
df_time = pd.DataFrame(
{'point1': point1,
'point2': point2,
'time': time
})
df_time=df_time[df_time['point1']!=df_time['point2']] ##cannot sell to another
fig, ax = plt.subplots(nrows=4, sharey=True)
for point1i, axi in zip(point1, ax.ravel()):
sns.boxplot(data=df_time[df_time['point1']==point1i], x='point2', y='time',
ax=axi, order=['A','B','C','D'])
plt.tight_layout()
plt.show()