如何将'旋转xticklabels'命令链接到seaborn plot

问题描述 投票:0回答:1

我正在学习大熊猫管道方法与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)

Plot

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

python pandas matplotlib seaborn
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.