【问题标题】:Plotting a barplot with a vertical line in pyplot-seaborn-pandas在 pyplot-seaborn-pandas 中绘制带有垂直线的条形图
【发布时间】:2019-02-02 16:11:51
【问题描述】:

我在做一些我认为直截了当的事情时遇到了麻烦。

我的数据是:

ROE_SP500_Q2_2018_quantile.to_json()

'{"index":{"0":0.0,"1":0.05,"2":0.1,"3":0.15,"4":0.2,"5":0.25,"6":0.3,"7":0.35,"8":0.4,"9":0.45,"10":0.5,"11":0.55,"12":0.6,"13":0.65,"14":0.7,"15":0.75,"16":0.8,"17":0.85,"18":0.9,"19":0.95},"ROE_Quantiles":{"0":-0.8931,"1":-0.0393,"2":0.00569,"3":0.03956,"4":0.05826,"5":0.075825,"6":0.09077,"7":0.10551,"8":0.12044,"9":0.14033,"10":0.15355,"11":0.17335,"12":0.1878,"13":0.209175,"14":0.2357,"15":0.27005,"16":0.3045,"17":0.3745,"18":0.46776,"19":0.73119}}'

我的情节代码是:

plt.close()
plt.figure(figsize=(14,8))
sns.barplot(x = 'Quantile', y = 'ROE', data = ROE_SP500_Q2_2018_quantile)
plt.vlines(x = 0.73, ymin = 0, ymax = 0.6, color = 'blue', size = 2)

plt.show()

返回以下图像:

我想更正以下问题:

a) 以我不理解的奇怪方式过度拥挤的刻度标签

b) 出现在错误位置的 vline。我使用了错误的参数来设置线条的粗细,我得到了一个错误。

【问题讨论】:

    标签: python-3.x pandas matplotlib seaborn


    【解决方案1】:

    传递给参数dataDataFrame,检查seaborn.barplot

    data:DataFrame、数组或数组列表,可选

    用于绘图的数据集。如果 x 和 y 不存在,则将其解释为宽格式。否则它应该是长格式的。

    sns.barplot(x = 'index', y = 'ROE_Quantiles', data = ROE_SP500_Q2_2018_quantile)
    #TypeError: vlines() missing 2 required positional arguments: 'ymin' and 'ymax'
    plt.vlines(x = 0.73, ymin = 0, ymax = 0.6, color = 'blue', linewidth=5)
    

    j = '{"index":{"0":0.0,"1":0.05,"2":0.1,"3":0.15,"4":0.2,"5":0.25,"6":0.3,"7":0.35,"8":0.4,"9":0.45,"10":0.5,"11":0.55,"12":0.6,"13":0.65,"14":0.7,"15":0.75,"16":0.8,"17":0.85,"18":0.9,"19":0.95},"ROE_Quantiles":{"0":-0.8931,"1":-0.0393,"2":0.00569,"3":0.03956,"4":0.05826,"5":0.075825,"6":0.09077,"7":0.10551,"8":0.12044,"9":0.14033,"10":0.15355,"11":0.17335,"12":0.1878,"13":0.209175,"14":0.2357,"15":0.27005,"16":0.3045,"17":0.3745,"18":0.46776,"19":0.73119}}'
    

    import ast
    
    df = pd.DataFrame(ast.literal_eval(j))
    print (df)
        index  ROE_Quantiles
    0    0.00      -0.893100
    1    0.05      -0.039300
    10   0.50       0.153550
    11   0.55       0.173350
    12   0.60       0.187800
    13   0.65       0.209175
    14   0.70       0.235700
    15   0.75       0.270050
    16   0.80       0.304500
    17   0.85       0.374500
    18   0.90       0.467760
    19   0.95       0.731190
    2    0.10       0.005690
    3    0.15       0.039560
    4    0.20       0.058260
    5    0.25       0.075825
    6    0.30       0.090770
    7    0.35       0.105510
    8    0.40       0.120440
    9    0.45       0.140330
    
    plt.close()
    plt.figure(figsize=(14,8))
    sns.barplot(x = 'index', y = 'ROE_Quantiles', data = df)
    plt.vlines(x = 0.73, ymin = 0, ymax = 0.6, color = 'blue', linewidth=5)
    plt.show()
    

    【讨论】:

    • @user8270077 - 我再次检查您的代码,似乎不需要plt.xticks(np.arange(0,1, 0.05))
    • 谢谢我已经删除了。刻度标签的问题仍然存在,我无法控制垂直线(宽度/厚度)
    • @user8270077 - 你的 seaborn/matplotlib 版本是什么?因为我得到了不错的刻度标签,但第二个问题仍然存在(正在寻找解决方案)
    • @user8270077 - 我找到了解决方案plt.vlines(x = 0.73, ymin = 0, ymax = 0.6, color = 'blue', linewidth=5)
    • 我通过print (pd.show_versions()) print (sns.__version__)检查我的版本并得到matplotlib: 2.2.20.8.1
    猜你喜欢
    • 2015-07-26
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    • 2022-09-24
    • 2017-11-05
    • 2020-05-28
    • 2019-04-21
    相关资源
    最近更新 更多