如何使用 seaborn 从 pandas 系列创建水平条形图?
How to create horizontal barplot from pandas series using seaborn?
我想使用 seaborn 水平绘制以下系列:
fruity -0.380938
hard -0.310382
pluribus -0.247448
nougat 0.199375
caramel 0.213416
crispedricewafer 0.324680
peanutyalmondy 0.406192
bar 0.429929
chocolate 0.636517
Name: winpercent, dtype: float64
我尝试了以下代码:
ax = sns.barplot(win_corr.index, win_corr.values, orient='h') # win_corr is the variable
ax.set_ylabel('Pearson')
ax.set_xlabel('')
plt.title('Horizontal')
ax.bar_label(ax.containers[0], fmt= '%0.1f' )
plt.show()
我有以下错误:
TypeError Traceback (most recent call last)
<ipython-input-59-0e19fa0939fd> in <module>()
----> 1 ax = sns.barplot(win_corr.index, win_corr.values, orient='h') # win_corr is the variable
2 ax.set_ylabel('Pearson')
3 ax.set_xlabel('')
4 plt.title('Horizontal')
5 ax.bar_label(ax.containers[0], fmt= '%0.1f' )
4 frames
/usr/local/lib/python3.7/dist-packages/seaborn/_core.py in infer_orient(x, y, orient, require_numeric)
1336 elif str(orient).startswith("h"):
1337 if require_numeric and x_type != "numeric":
-> 1338 raise TypeError(nonnumeric_dv_error.format("Horizontal", "x"))
1339 return "h"
1340
TypeError: Horizontal orientation requires numeric `x` variable.
但是,它适用于普通条形图:
ax = sns.barplot(win_corr.index, win_corr.values)
ax.set_ylabel('pearson')
ax.set_xlabel('Features')
plt.title('Pearson')
ax.bar_label(ax.containers[0], fmt= '%0.1f' )
plt.show()
最后一段代码的结果是:
我应该在我的代码中更改什么来创建水平条形图?
提前致谢!
我想你只需要交换 win_corr.index 和 win_corr.values
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.DataFrame.from_dict(data = {'name': ["fruity", "hard", "pluribus", "nougat"],'win_corr': [-0.380938, -0.310382, -0.247448, 0.199375]})
ax = sns.barplot(df.win_corr.values,df.name, orient='h')
ax.set_ylabel('Pearson')
ax.set_xlabel('')
plt.title('Horizontal')
ax.bar_label(ax.containers[0], fmt= '%0.1f' )
plt.show()
产生这个结果:
我想使用 seaborn 水平绘制以下系列:
fruity -0.380938
hard -0.310382
pluribus -0.247448
nougat 0.199375
caramel 0.213416
crispedricewafer 0.324680
peanutyalmondy 0.406192
bar 0.429929
chocolate 0.636517
Name: winpercent, dtype: float64
我尝试了以下代码:
ax = sns.barplot(win_corr.index, win_corr.values, orient='h') # win_corr is the variable
ax.set_ylabel('Pearson')
ax.set_xlabel('')
plt.title('Horizontal')
ax.bar_label(ax.containers[0], fmt= '%0.1f' )
plt.show()
我有以下错误:
TypeError Traceback (most recent call last)
<ipython-input-59-0e19fa0939fd> in <module>()
----> 1 ax = sns.barplot(win_corr.index, win_corr.values, orient='h') # win_corr is the variable
2 ax.set_ylabel('Pearson')
3 ax.set_xlabel('')
4 plt.title('Horizontal')
5 ax.bar_label(ax.containers[0], fmt= '%0.1f' )
4 frames
/usr/local/lib/python3.7/dist-packages/seaborn/_core.py in infer_orient(x, y, orient, require_numeric)
1336 elif str(orient).startswith("h"):
1337 if require_numeric and x_type != "numeric":
-> 1338 raise TypeError(nonnumeric_dv_error.format("Horizontal", "x"))
1339 return "h"
1340
TypeError: Horizontal orientation requires numeric `x` variable.
但是,它适用于普通条形图:
ax = sns.barplot(win_corr.index, win_corr.values)
ax.set_ylabel('pearson')
ax.set_xlabel('Features')
plt.title('Pearson')
ax.bar_label(ax.containers[0], fmt= '%0.1f' )
plt.show()
最后一段代码的结果是:
我应该在我的代码中更改什么来创建水平条形图? 提前致谢!
我想你只需要交换 win_corr.index 和 win_corr.values
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.DataFrame.from_dict(data = {'name': ["fruity", "hard", "pluribus", "nougat"],'win_corr': [-0.380938, -0.310382, -0.247448, 0.199375]})
ax = sns.barplot(df.win_corr.values,df.name, orient='h')
ax.set_ylabel('Pearson')
ax.set_xlabel('')
plt.title('Horizontal')
ax.bar_label(ax.containers[0], fmt= '%0.1f' )
plt.show()
产生这个结果: