0

Using seaborn and matplotlib I was able to make a bar graph for a homework assignment, and want to know how I could reduce the amount of times the x/y axis information appears. As in, instead of appearing all the years from 1989 to 2014, for example, it only appears every 4 years or so.

Here's my graph:

graph I made

and what my homework assignment asks me to do:

graph I'm supposed to make

here's my code:

import matplotlib.pyplot as plt
import seaborn as sns

planets = sns.load_dataset("planets")
sns.set_style("whitegrid")
g = sns.catplot(x='year', kind='count', data=planets, aspect=2, palette="Blues")
g = g.set_axis_labels("year",'count').set(xlim=(0,25), ylim=(0,200))
plt.show(g)
  • 1
    how did you manage to do that plot without code? –  Mar 16 '22 at 21:25
  • @Sembei If this is an assignment, I guess the second graph is the provided output ;) – mozway Mar 16 '22 at 21:26
  • @propelledaviator you should provide your data and code. Check `set_xticks` – mozway Mar 16 '22 at 21:27
  • @mozway done, forgot completely – propelledaviator Mar 16 '22 at 21:44
  • 1
    You could use e.g. `g.ax.xaxis.set_major_locator(matplotlib.ticker.MaxNLocator())`. (Note that `plt.show(g)` should be `plt.show()`, or maybe `g.fig.show()`. You also might want to add `plt.tight_layout()` to nicely fit the labels into the surrounding plot) – JohanC Mar 16 '22 at 21:50
  • Also note that `g.set(xlim=(0,25))` cuts the first (very low) bar into two. You could try something like `g.set(xlim=(-0.5,22.5))`. – JohanC Mar 16 '22 at 22:07

0 Answers0