Sunnie
Sunnie

Reputation: 91

How to set x ticks for seaborn (python) line plot

I made a line plot chart for years 1960-2014 using seaborn but the xticks aren't correct. I want only intervals to appear (like 1960, 1970, 1980, etc).How do i adjust the xticks? I tried rotating it but it didn't seem to work. Here is my code:

#plot figure using sns
g=sns.relplot(x="Year", y="Indicator_Value",
           data=Emissions_C_df,
           kind="line",
           style="Indicator_Name",
           hue="Indicator_Name",
           )
plt.show()

enter image description here

Upvotes: 1

Views: 2691

Answers (1)

tsj
tsj

Reputation: 792

You can use a MaxNLocator from matplotlib.ticker for the major ticks (decades) and manually set specific minor ticks with a FixedLocator.

import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FixedLocator, MaxNLocator

a = np.arange(50)
d = {'Year': 1953+a, 'Indicator_Value': a}
df = pd.DataFrame(data=d)

g = sns.relplot(x="Year", y="Indicator_Value",
           data=df,
           kind="line")
ax = plt.gca()
ax.xaxis.set_major_locator(MaxNLocator(steps=[10]))
ax.xaxis.set_minor_locator(FixedLocator(range(1960,1970)))
plt.show()

enter image description here

Upvotes: 1

Related Questions