如何将命令“ rotate xticklabels”链接到seaborn图

时间:2019-02-14 19:57:52

标签: python pandas matplotlib seaborn

我正在用海底积木学习熊猫的滚滚方法: 大多数东西很容易以单线连接,但是我当时有 难以传递xticklabel rotations

该怎么做?

代码:

import numpy as np
import pandas as pd

# plotting
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
names = ['mpg','cylinders', 'displacement','horsepower','weight',
         'acceleration','model_year', 'origin', 'car_name']

url = "http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data"
df = pd.read_csv(url, sep='\s+', names=names)

情节

g = ( df.pipe((sns.factorplot, 'data'), x='model_year', y='mpg')
)

for ax in g.axes.flat: 
    plt.setp(ax.get_xticklabels(), rotation=45)

必需的骨架:

( df.pipe((sns.factorplot, 'data'), x='model_year', y='mpg')
.set(xlim=(0,90), ylim=(0,80))
.set (xticklabel_rotation = 45)
)

这可能吗?

所需图片:
enter image description here

但是我得到了:
enter image description here

1 个答案:

答案 0 :(得分:1)

您快到了。您希望使用.set(xticklabel_rotation = 45)

而不是.set_xticklabels(rotation=45)
import pandas as pd

import seaborn as sns
names = ['mpg','cylinders', 'displacement','horsepower','weight',
         'acceleration','model_year', 'origin', 'car_name']

url = "http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data"
df = pd.read_csv(url, sep='\s+', names=names)

(df.pipe((sns.factorplot, 'data'), x='model_year', y='mpg')
.set_xticklabels(rotation=45)
)

这给了我

enter image description here