python - 旋转 x 轴标签 FacetGrid seaborn 不工作
问题描述
我正在尝试在 python 中使用 seaborn 创建一个多面图,但我遇到了很多问题,其中一件事是旋转 x 轴标签。
我目前正在尝试使用以下代码:
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)
当我使用它时,我收到一条错误消息:
AttributeError: 'NoneType' object has no attribute 'axes'
我想我理解这意味着没有返回的对象,因此将轴设置为无任何作用。
这段代码似乎在我遇到的其他 SO 帖子中有效,但我似乎无法让它工作。
任何关于我做错了什么的建议将不胜感激。
谢谢,柯蒂斯
解决方案
试试这个,看来你重写了“情节”变量。:
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()
输出:
推荐阅读
- c# - 从 Azure Function ping 服务器
- python - 满足条件时通过不和谐机器人发送消息
- ruby - “class << self”块内的Ruby访问类
- c++ - 在调用函数上实现 C++ 转换
- python - 需要帮助在不使用 bin() 或 hex() 等函数的情况下将十进制转换为十六进制
- kubernetes - 对 Istio ServiceRoleBinding 用户感到困惑
- python - pip 未与 python 一起安装,它不会自行安装
- c# - 检查 UWP 中的用户货币符号
- python - 仅在通用换行符处拆分 Unicode 字符串(\n、\r、\r\n)
- python - 在 Keras .h5 文件中保存自定义变量