Plotting a barplot with a vertical line in pyplot-seaborn-pandas

烈酒焚心 提交于 2021-01-28 06:45:09

问题


I am having trouble doing something that seems to me straightforward.

My data is:

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}}'

My code for the plot is:

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()

which returns the following image:

I would like to correct the following problems:

a) The ticklabels which are overly crowded in a strange way I do not understand

b) The vline which appears in the wrong place. I am using the wrong argument to set the thickness of the line and I get an error.


回答1:


Pass to parameter data DataFrame, check seaborn.barplot:

data : DataFrame, array, or list of arrays, optional

Dataset for plotting. If x and y are absent, this is interpreted as wide-form. Otherwise it is expected to be long-form.

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()



来源:https://stackoverflow.com/questions/52050883/plotting-a-barplot-with-a-vertical-line-in-pyplot-seaborn-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!