【问题标题】:Rotate x-axis labels FacetGrid seaborn not working旋转 x 轴标签 FacetGrid seaborn 不工作
【发布时间】:2020-02-05 13:55:32
【问题描述】:

我正在尝试在 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 帖子中有效,但我似乎无法让它工作。

任何关于我做错了什么的建议将不胜感激。

谢谢, 柯蒂斯

【问题讨论】:

  • 什么是g?不应该是plot吗?
  • @NicolasGervais 是正确的,对不起,我在帖子中编辑了这个,但代码中仍然存在同样的问题
  • 代码看起来不错。我认为您的问题只是您没有minimal reproducible example,因此将自己与变量名混淆了。
  • @ImportanceOfBeingErnest 我已经编辑了帖子,现在应该可以重现了。我已经创建了数据集并指定了所需的模块。我仍然收到相同的错误消息
  • 好的,所以你把很多东西放在一行里。使用单独的行并将内容存储在不同的变量中。不要做FacetGrid.map(..).fig.some_method()之类的事情(除非你知道自己在做什么)

标签: python matplotlib plot seaborn


【解决方案1】:

试试这个,看来你重写了'plot'变量。:

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()

输出:

【讨论】:

  • 感谢您的帮助,非常感谢。这似乎对我有用!
  • 我遇到的另一个问题是,在我的图中,鉴于'text_concat' 字段中有 50 多个类别,我希望标签应该从一个方面更改为下一个方面,但它们是在每个情节中都相同。那里还有我想念的东西。我认为order 参数会解决这个问题。
猜你喜欢
  • 2019-08-02
  • 2020-08-05
  • 2020-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-10
  • 2020-08-19
相关资源
最近更新 更多