Reputation: 31
The x axis is year-month of date of two years. Hence 24 values. Because the relplot function only allows numeric type for x and y axis. So the x axis is all clustered to two ends since 201801 to 201912 is not equally spaced. How I could make it equally spaced with correct label like this: 201801,201802....201912. (24 date values)
import seaborn as sns
sns.set(style="ticks")
palette = dict(zip(rel['Sub-Category'].unique(),
sns.color_palette("rocket_r", 17)))
r=sns.relplot(x='YearMonth', y="Profit",
hue="Sub-Category", col="Category",
#size="Year", size_order=["2019", "2018"],
palette=palette,
height=5, aspect=.7, facet_kws=dict(sharex=True),
kind="line", legend="full", data=rel)
r.set(yticks=[i for i in range(int(min(rel['Profit'])), int(max(rel['Profit'])) + 50, 500)],
xticks=[i for i in rel.YearMonth.unique()])
Upvotes: 2
Views: 679
Reputation: 1130
As described in the comments you just have to convert your YearMonth
column into a datetime:
# Input data
df = pd.DataFrame({'YearMonth': ['2018-01','2018-01','2018-02','2018-04','2018-03','2018-05'],
'Category':['Clothing','Furniture','Clothing','Clothing','Furniture','Clothing'],
'Sub-Category':['Henkerchief','Table','Skirt','Henkerchief','Table','Skirt'],
'Profit':[16,40,110,33,44,55]})
# Create datetime column
df['date'] = pd.to_datetime(df['YearMonth'], format = '%Y-%m')
# Plot
sns.set(style="ticks")
palette = dict(zip(df['Sub-Category'].unique(),
sns.color_palette("rocket_r", 17)))
r=sns.relplot(x='date', y="Profit",
hue="Sub-Category", col="Category",
palette=palette,
height=5, aspect=.7, facet_kws=dict(sharex=True),
kind="line", legend="full", data=df)
# Adjust xticks
xticks = pd.date_range(start='2017-12',end='2018-05',
freq='MS',closed='right')
r.set(xticks=xticks)
Update: If you want to rotate the xtick labels you can use:
for ax in r.axes.ravel():
ax.set_xticklabels(ax.get_xticklabels(), rotation=45)
Upvotes: 2