Reputation: 459
I'm attempting to create a faceted plot using seaborn in python, but I'm having issues with a number of things, one thing being rotating the x-axis labels.
I am currently attempting to use the following code:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
vin = pd.Series(["W1","W1","W2","W2","W1","W3","W4"])
word1 = pd.Series(['pdi','pdi','tread','adjust','fill','pdi','fill'])
word2 = pd.Series(['perform','perform','fill','measure','tire','check','tire'])
date = pd.Series(["01-07-2020","01-07-2020","01-07-2020","01-07-2020","01-08-2020","01-08-2020","01-08-2020"])
bigram_with_dates = pd.concat([vin,word1,word2,date], axis = 1)
names = ["vin", "word1","word2","date"]
bigram_with_dates.columns = names
bigram_with_dates['date'] = pd.to_datetime(bigram_with_dates['date'])
bigram_with_dates['text_concat'] = bigram_with_dates['word1'] + "," + bigram_with_dates['word2']
plot_params = sns.FacetGrid(bigram_with_dates, col="date", height=3, aspect=.5, col_wrap = 10,sharex = False, sharey = False)
plot = plot_params.map(sns.countplot, 'text_concat', color = 'c', order = bigram_with_dates['text_concat'])
plot_adjust = plot.fig.subplots_adjust(wspace=0.5, hspace=0.5)
for axes in plot.axes.flat:
axes.set_xticklabels(axes.get_xticklabels(), rotation=90)
When I use this I get an error that states:
AttributeError: 'NoneType' object has no attribute 'axes'
Which I think I understand to mean that there is no returned object so setting axes on nothing does nothing.
This code seems to work in other SO posts I've come across, but I can't seem to get it to work.
Any suggestions as to what I'm doing wrong would be greatly appreciated.
Thanks, Curtis
Upvotes: 10
Views: 18121
Reputation: 13
The solution did not work for me, when I tried something similar, I got the warning:
set_ticklabels() should only be used with a fixed number of ticks, i.e. after set_ticks() or using a FixedLocator.
This solution worked for me:
for ax in plot.axes.flat:
for label in ax.get_xticklabels():
label.set_rotation(90)
Upvotes: 0
Reputation: 153460
Try this, it seems you were over-writing the 'plot' variable.:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
vin = pd.Series(["W1","W1","W2","W2","W1","W3","W4"])
word1 = pd.Series(['pdi','pdi','tread','adjust','fill','pdi','fill'])
word2 = pd.Series(['perform','perform','fill','measure','tire','check','tire'])
date = pd.Series(["01-07-2020","01-07-2020","01-07-2020","01-07-2020","01-08-2020","01-08-2020","01-08-2020"])
bigram_with_dates = pd.concat([vin,word1,word2,date], axis = 1)
names = ["vin", "word1","word2","date"]
bigram_with_dates.columns = names
bigram_with_dates['date'] = pd.to_datetime(bigram_with_dates['date']).dt.strftime('%m-%d-%Y')
bigram_with_dates['text_concat'] = bigram_with_dates['word1'] + "," + bigram_with_dates['word2']
plot = sns.FacetGrid(bigram_with_dates, col="date", height=3, aspect=.5, col_wrap = 10,sharex = False, sharey = False)
plot1 = plot.map(sns.countplot,
'text_concat',
color = 'c',
order = bigram_with_dates['text_concat'].value_counts(ascending = False).iloc[:5].index)\
.fig.subplots_adjust(wspace=0.5, hspace=12)
for axes in plot.axes.flat:
_ = axes.set_xticklabels(axes.get_xticklabels(), rotation=90)
plt.tight_layout()
Output:
Upvotes: 14