0

This seems like it should be so simple but none of the online solutions work for me.

This is my code so far

sns.histplot(dataset['How long have you been cycling in Dublin?'], color="c", alpha=.6)
plt.title('How long have you been cycling in Dublin?', fontdict={'fontsize': 20})
plt.xlabel('Time')
plt.ylabel('Number of cyclists')

The histplot below simply counts the number of instances of each of the time period categories. The time periods of cycling experience are plotted on the x-axis but the categories have been plotted out of order. I just want to switch the first two bars.

The plot below shows the time periods of cycling experience on the x-axis and number of cyclists on the y-axis

The categories are the responses in the rows of the dataset and if I just list them this is the order I want them in:

lbls = list(np.unique(dataset['How long have you been cycling in Dublin?']))
lbls

['+3 years', '1 - 3 years', '3 months - 1 year', 'Less than 3 months']

Online solutions suggest including sort=False or or specifying order or labels within the sns.histplot call but these all produce the following error

AttributeError: 'PolyCollection' object has no property ___

Any simple solution that can be used across different datasets? i.e. a solution based on the author-defined categorical order not on alphabetical or numerical categorisation of these specific strings

speck
  • 31
  • 4
  • 2
    Did you try `df['col_name'] = pd.Categorical(df['col_name'], ['....', ....])`? Or `sns.countplot(..., order=...)`? – JohanC Jul 22 '22 at 14:56
  • sns.countplot(..., order=...) gives the error AttributeError: 'PolyCollection' object has no property 'order' but thank you the other option you give does the job – speck Jul 22 '22 at 15:29

0 Answers0