Reputation: 129
I have a dataframe with one column that has YYYYMM data as a float, and a second column has summaraized data for the month.
I have tried to create another dataframe column but it was never going to work as the intended output is not a date.
pd.to_datetime(mthly_PL_withDate_df['YearMth'].astype(str), format='%Y%m')
So when I plot, sns/mpl creates X axis ticks that are numerically evenly spaced when my data 'YearMth' is not. How do I tell sns/mpl to "just show what you are given and don't try to 'niceify' it". I would have thought "showing what your are given without the introduction of assumptions" would be the default.
with sns.axes_style('whitegrid'):
g = sns.relplot(x='YearMth', y='PL', data=mthly_PL_withDate_df, height=5, aspect=1.5)
Upvotes: 0
Views: 335
Reputation: 3118
You are using a float on the x-axis. It is normal for seaborn to assume that the data is numerical instead of categorical.
If you want to keep the dates as floats (strings are more correct in this case btw), you can easily fix that by setting the x_ticks property and passing your YearMth
column to it as ax.set_xticks([0,2,4,6])
.
This property, however, is not managed by seaborn, but instead by the underlying matplotlib.pyplot
package. Thus you will need to modify your code a bit to expose the subplot
object and later pass it to seaborn.
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
data = [(10, 1), (11, 2), (12, 3), (1000, 4)]
data = pd.DataFrame.from_records(data=data, columns=['x', 'y'])
fig, ax = plt.subplots(figsize = (12,6))
ax.set_title("Plot with controlled ticks")
ax.set_ylabel("Mode data")
ax.set_xlabel("X values with good ticks")
ax.set_xticks(range(0,len(data['x'])))
ax.set_xticklabels(list(data['x']), rotation=45)
rel_plot = sns.relplot(ax=ax, data=data, height=5, aspect=1.5)
The code above will give you the expected result:
Overall, subplots give you very fine control over the visualization of the displayed data (you can look up the full subplot
api here).
Upvotes: 1