0

This code was working before, but now it's not working.

market_segment_confirmed_df = pd.DataFrame(hotel_confirmed_df.groupby(['hotel','market_segment']).agg({'is_canceled':'count'})).reset_index().rename(columns = {'is_canceled':'confirmed_bookings'})


plt.figure(figsize = (8,4))
mpl.rcParams['font.size'] = 11
sns.barplot(x = 'market_segment', y= 'confirmed_bookings', hue = 'hotel', data=market_segment_confirmed_df).set_xticklabels(market_segment_confirmed_df['market_segment'], rotation=30)
plt.title('Market segment wise confirmed bookings.')
plt.grid()

It's throwing the following error:

ValueError                                Traceback (most recent call last)
<ipython-input-32-eb5bac22c40d> in <module>
      6 plt.figure(figsize = (8,3))
      7 mpl.rcParams['font.size'] = 11
----> 8 sns.barplot(x = 'market_segment', y= 'confirmed_bookings', hue = 'hotel', data=market_segment_confirmed_df).set_xticklabels(market_segment_confirmed_df['market_segment'], rotation=30)
      9 plt.title('Market segment wise confirmed bookings.')
     10 plt.grid()
/usr/local/lib/python3.8/dist-packages/matplotlib/axis.py in set_ticklabels(self, ticklabels, minor, **kwargs)
   1718             # remove all tick labels, so only error for > 0 ticklabels
   1719             if len(locator.locs) != len(ticklabels) and len(ticklabels) != 0:
-> 1720                 raise ValueError(
   1721                     "The number of FixedLocator locations"
   1722                     f" ({len(locator.locs)}), usually from a call to"

ValueError: The number of FixedLocator locations (7), usually from a call to set_ticks, does not match the number of ticklabels (13).

Not able to figure out what the problem is. Can anyone help me out?

  • You need to remove the part `set_xticklabels(market_segment_confirmed_df['market_segment'], rotation=30)` at the end of your call to `sns.barplot()`. `df['market_segment']` contains ticks for each row of the dataframe, while there is only one bar per unique market segment. To rotate the ticks, please use `ax = sns.barplot(....)` and then `ax.tick_params(axis='x', rotation=30)`. – JohanC Feb 26 '23 at 20:04

0 Answers0