How do we set number of x-ticks for a relplot in Python? Our graphs looks like the following:
Our present graph has included 7 dates in months, but we would like all months included as x-ticks. We would like to set the number of x-ticks to 12, with the following as x-axis labels:
months = ['2020-01', '2020-02', '2020-03', '2020-04', '2020-05', '2020-06', '2020-07', '2020-08', '2020-09', '2020-10', '2020-11', '2020-12']
Following is the function that generates the graph:
def create_lineplot(dataframe):
months = mdates.MonthLocator() # every month
years_fmt = mdates.DateFormatter('%Y-%m') # This is a format. Will be clear in Screenshot
# Filtering data to only select relevant columns and data from the year 2020
dataframe = dataframe[['dev_id', 'outside_temperature', 'datetime']]
dataframe["datetime"] = pd.to_datetime(dataframe["datetime"])
dataframe_2020 = dataframe[dataframe['datetime'].dt.year == 2020]
fig, axes = plt.subplots(figsize=(20, 2))
mdf = pd.melt(dataframe_2020, id_vars=['datetime', 'dev_id'], var_name=['Temperature'])
g = sns.relplot(data=mdf, x='datetime', y='value', kind='line', hue='Temperature', height=5, aspect=3)
g._legend.remove()
axes.xaxis.set_major_locator(months)
axes.xaxis.set_major_formatter(years_fmt)
axes.xaxis.set_minor_locator(months)
plt.xticks(rotation='vertical')
plt.tight_layout()
plt.legend(loc='upper left')
plt.savefig('lineplot.png')
plt.show()
An example of our data:
CodePudding user response:
The main problem of the code is that a dummy fig and axes are created while a