1

New to Python and Seaborn - I am trying to plot a histogram which shows distribution of the below data against state.

df = pd.DataFrame([
    ['AZ',1,1],
    ['NC',1,1],
    ['AZ',1,0],
    ['CA',1,0],
    ['AZ',1,0],
    ['CA',1,0],
    ['FG',1,0],
    ['LL',1,0],
    ['ER',1,0],
    ['PO',1,0],
    ['ER',1,0],
    ['MN',1,0],
    ['UI',1,0],
    ['PP',1,0],
    ['QQ',1,0],
    ['GG',1,0],
    ['CV',1,0],
    ['CV',1,0],
    ['MM',1,0],
    ['OO',1,0],
    ['TT',1,0],
    ['AS',1,0],
    ['DF',1,0],
    ['GH',1,0],
    ['MN',1,0],
    ['LO',1,0],
    ['CA',1,0],
    ['CA',1,0],
    ['CA',1,0],
    ['ML',1,0],
    ['CA',1,0],
    ['CA',1,0],
    ['CA',1,0],
    ['CA',1,0],
    ['CA',1,0],
    ['NG',1,0],
    ['CA',1,0],
    ['CA',1,0],
    ['CA',1,0],
    ['IO',1,0],
    ['CA',1,0],
    ['KK',1,0],
    ['CA',1,0],
    ['CA',1,0],
    ['CA',1,0],
    ['AS',1,0],
    ['LL',1,0],
    ['CA',1,0],
    ['LO',1,0],
    ['CA',1,0],
    ['CA',1,0],
    ['GG',1,0],
    ['UU',1,0],
    ['II',1,0],
    ['CA',1,0],
    ['CA',1,0],
    ['PO',1,0]
], columns=['addr_state', 'B', 'C'])
sns.histplot(df['addr_state'])

The graph of the above comes like below enter image description here

The x axis ticks if you observe are very close to each other and there are going to be more states that might get added. How can I control the density of the ticks? Do i need to increase the overall space for the graph? If so, how can i do that?

Rebooting
  • 2,762
  • 11
  • 47
  • 70
  • Does this answer your question? [How to decrease the density of x-ticks in seaborn](https://stackoverflow.com/questions/38947115/how-to-decrease-the-density-of-x-ticks-in-seaborn) – medium-dimensional Sep 06 '22 at 09:23
  • You have categorical values in X-axis. It if was numbers or dates, you could decide to show fewer entries. But, Showing only a few states may not work. So, you can try to increase plot size and/or rotate ticklabels. Do those not work for you? – Redox Sep 06 '22 at 11:56

1 Answers1

2

You can create a Figure and Axes in matplotlib, and pass the Axes to seaborn.

When creating the Figure, you can specify its size like so:

import matplotlib.pyplot as plt
import seaborn as sns

fig, ax = plt.subplots(figsize=(12, 6))
sns.histplot(df['addr_state'], ax=ax)
plt.show()

enter image description here

You might also consider making this plot vertical instead of horizontal:

import matplotlib.pyplot as plt
import seaborn as sns

fig, ax = plt.subplots(figsize=(6, 8))
sns.histplot(df, y='addr_state', ax=ax)
plt.show()

enter image description here

Cameron Riddell
  • 10,942
  • 9
  • 19