【问题标题】:Seaborn Plot- Add the frequency along the axisSeaborn Plot-沿轴添加频率
【发布时间】:2020-06-11 10:49:46
【问题描述】:

将特征细节与目标变量进行比较

# Read the data
data=pd.read_csv("https://raw.githubusercontent.com/sharmaroshan/Online-Shoppers-Purchasing-Intention/master/online_shoppers_intention.csv")
data.head()

参考目标变量的区域法线图:

sns.countplot(x='Region',hue='Revenue',data=data)

请帮我获取沿图 X 轴的频率表:

(参考下图):

【问题讨论】:

  • 频率是什么意思..你能详细说明一些示例吗..我认为你必须指出 csv 的结构,否则欢迎提供示例。我建议你展示你的完整代码..
  • 您好,我已将 CSV(原始文件)作为数据附加,它是两个特征之间的双变量分析——区域和收入。使用计数图我可以获得一般格式。以表格的形式(在参考图像中)获得连同计数。申请代码
  • this question 可能重复。
  • @Quang Hoang,谢谢,但我认为这与 alpha 有关
  • 这能回答你的问题吗? Matplot table, legend, stacked barplot issues

标签: pandas matplotlib seaborn


【解决方案1】:

我找不到与“seaborn”一起使用的方法。我添加了一个带有“matplotlib”的表格来创建图表。

df = data.loc[:,['Revenue','Region']]
df2 = df.groupby(['Revenue','Region'])['Revenue'].agg('count').to_frame('Count')
df2.reset_index(inplace=True)
df3 = df2.pivot(values='Count', columns='Region', index='Revenue')
df3 = df3.rename_axis(columns=None).reset_index()
df3.set_index('Revenue',inplace=True)
df3
        1   2   3   4   5   6   7   8   9
Revenue                                 
False   4009 948 2054 1007 266 693 642 378 425
True    771  188  349 175  52  112 119 56 86

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(4,3),dpi=144)
ax = fig.add_subplot(111)

n_rows = len(df3)
labels = df3.columns.values
width = 0.35

x = np.arange(len(labels))
y1 = df3.iloc[0]
y2 = df3.iloc[1]

ax.bar(x - width/2, y1, width, label='False')
ax.bar(x + width/2, y2, width, label='True')
ax.set_xticks([])
ax.set_xticklabels([])

ax.legend()
cell_text = [list(y1.values),list(y2.values)]

ax.table(cellText=cell_text, rowLabels=df3.index, colLabels=df3.columns, loc='bottom')

plt.title('Region wise session vs Revenue Generated')
plt.ylabel('Count')
ax.set_xlabel('Region')
ax.xaxis.set_label_coords(0.5, -0.25)

plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    • 2020-10-28
    • 2020-09-22
    相关资源
    最近更新 更多