2015-08-27 159 views
29

用下面的代码:如何大熊猫旋转x轴刻度标签barplot

import matplotlib 
matplotlib.style.use('ggplot') 
import matplotlib.pyplot as plt 
import pandas as pd 

df = pd.DataFrame({ 'celltype':["foo","bar","qux","woz"], 's1':[5,9,1,7], 's2':[12,90,13,87]}) 
df = df[["celltype","s1","s2"]] 
df.set_index(["celltype"],inplace=True) 
df.plot(kind='bar',alpha=0.75) 
plt.xlabel("") 

我做了这个情节:

enter image description here

我如何可以旋转x轴的刻度标记到0度?

我尝试添加这一点,但没有奏效:

plt.set_xticklabels(df.index,rotation=90) 

回答

71

通行证PARAM rot=0旋转xticks:

import matplotlib 
matplotlib.style.use('ggplot') 
import matplotlib.pyplot as plt 
import pandas as pd 

df = pd.DataFrame({ 'celltype':["foo","bar","qux","woz"], 's1':[5,9,1,7], 's2':[12,90,13,87]}) 
df = df[["celltype","s1","s2"]] 
df.set_index(["celltype"],inplace=True) 
df.plot(kind='bar',alpha=0.75, rot=0) 
plt.xlabel("") 
plt.show() 

得到情节:

enter image description here

+0

有没有一种方法可以将此设置为所有其他图的默认值? – Mitja

+1

@Mitja不确定,可以编辑matplotlibrc文件来设置默认值https://matplotlib.org/users/customizing.html#customizing-with-matplotlibrc-files,但我找不到x轴的设置旋转,我不是一个matplotlib专家,所有我能找到的是修改这个当前情节 – EdChum

5

的问题是明确,但标题并不尽如人意。我的回答是针对那些希望更改标签,而不是刻度标签,这是公认的答案是关于什么。

for ax in plt.gcf().axes: 
    plt.sca(ax) 
    plt.xlabel(ax.get_xlabel(), rotation=90)