代码之家  ›  专栏  ›  技术社区  ›  astro123

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

  •  0
  • astro123  · 技术社区  · 7 年前

    我在学习“熊猫管道法”和“海生地块: 大部分的东西很容易被锁在一条直线上,但是我 管道输送困难 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 回复  |  直到 7 年前
        1
  •  1
  •   jwalton    7 年前

    你就快到了。而不是 .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