As you can see here, the X axis labels here are quite unreadable. This will happen regardless of how I adjust the figure size. I’m trying to figure out how to adjust the labeling so that it only shows certain points. The X axis are all numerical between -1 to 1, and I think it would be nice and more viewer friendly to have labels at -1, -.5, 0, .5 and 1.
Is there a way to do this? Thank you!
Here’s my code
sns.set(rc={'figure.figsize':(20,8)}) ax = sns.countplot(musi['Positivity']) ax.set_xticklabels(ax.get_xticklabels(), rotation=40, ha='right') plt.tight_layout() plt.show()
Advertisement
Answer
Basically seaborn
is wrapper on matplotlib
. You can use matplotlib ticker
function to do a Job. Refer the below example.
Let’s Plots tick every 1
spacing.
import matplotlib.pyplot as plt import matplotlib.ticker as ticker import seaborn as sns sns.set_theme(style="whitegrid") x = [0,5,9,10,15] y = [0,1,2,3,4] tick_spacing = 1 fig, ax = plt.subplots(1,1) sns.lineplot(x, y) ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing)) plt.show()
Now Let’s plot ticks every 5
ticks.
import matplotlib.pyplot as plt import matplotlib.ticker as ticker import seaborn as sns sns.set_theme(style="whitegrid") x = [0,5,9,10,15] y = [0,1,2,3,4] tick_spacing = 5 fig, ax = plt.subplots(1,1) sns.lineplot(x, y) ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing)) plt.show()
P.S.: This solution give you explicit control of the tick spacing via the number given to ticker.MultipleLocater(), allows automatic limit determination, and is easy to read later.