0

I'm experiencing crowding on my x-axis when using lineplot in seaborn.

I initially plotted the data with the following:

import pandas as pd
import matplotlib
matplotlib.use('TkAgg')
import seaborn as sns

for state in states_list:
    starting_point = state_covid_data[(state_covid_data.cases > 9) & (state_covid_data.state == state)]
    plot = sns.lineplot(data=starting_point, x='date', y='cases')
plot

Here is the output: https://i.stack.imgur.com/eeh34.png

To try and fix the crowding, I adjusted my code as follows from the recommendation in this answer:

for state in states_list:
    starting_point = state_covid_data[(state_covid_data.cases > 9) & (state_covid_data.state == state)]
    plot = sns.lineplot(data=starting_point, x='date', y='cases')
    for ind, label in enumerate(plot.get_xticklabels()):
        if ind % 10 == 0:  # every 10th label is kept
            label.set_visible(True)
        else:
            label.set_visible(False)
    plot

This resulted in only one date showing up on my x-axis: https://i.stack.imgur.com/Md8x1.png

Any recommendations how to to fix this? Ideally I would like to select which ticks appear. Even better would to be able to substitute certain ticks for my own values.

Thanks for your time!

  • Try changing the 10 to 2. That will show every second label. In general, try playing around with that value. – adch99 May 16 '20 at 19:41
  • Hi, thanks for the comment. Unfortunately, other than 1, every other value ends up with the same chart as the below showing only the first date. https://i.stack.imgur.com/Md8x1.png – stevaineer May 16 '20 at 20:20

0 Answers0