2016-09-29 86 views
0

我想繪製下面的數據框xtick標籤旋轉,同時也有一個緊張的,despined佈局(使用tight_layout()從matplotlib和despine從seaborn)。下面不起作用,因爲標籤不露面:xlabels不顯示與seaborn和python

import matplotlib.pylab as plt 
import seaborn as sns 
import pandas 
df = pandas.DataFrame({"x": ["XYZ1", "XYZ2", "XYZ3", "XYZ4"], 
         "y": [0, 1, 0, 1]}) 
plt.figure(figsize=(5,5)) 
sns.set_style("ticks") 
g = sns.pointplot(x="x", y="y", data=df) 
sns.despine(trim=True, offset=2) 
g.set_xticklabels(g.get_xticklabels(), rotation=55, ha="center") 
plt.tight_layout() 

它產生:

enter image description here

的XTICK標籤( 「XYZ1」, 「XYZ2」,...)不出現。如果我刪除蝨子出現,但然後不排除。如果我在despine/tight_layout前更改刻度,它們會出現但不會旋轉。如何才能做到這一點?

+0

你可以做'plt.setp(ax.get_xticklabels(),旋轉= 50)' – mwaskom

回答

2

我的機器上的以下作品

import matplotlib.pylab as plt 
import seaborn as sns 
import pandas 
df = pandas.DataFrame({"x": ["XYZ1", "XYZ2", "XYZ3", "XYZ4"], 
         "y": [0, 1, 0, 1]}) 
plt.figure(figsize=(5,5)) 
sns.set_style("ticks") 
g = sns.pointplot(x="x", y="y", data=df) 
sns.despine(trim=True, offset=2) 
g.set_xticklabels(df['x'], rotation=55, ha="center") 
plt.tight_layout() 
plt.show() 

,併產生

enter image description here

+0

我不知道爲什麼你在y軸上得到''y'',我的版本產生''mean(y)''? – mvd