用两个不同的列绘制直方图 [英] Plotting histogram with two different columns
本文介绍了用两个不同的列绘制直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据集是这样的
访问天数星期二 23星期一 30星期日 120星期五 2星期五 30星期二 13星期一 20星期六 100
如何为该数据集绘制直方图,但将其假设为一个大型数据集(560030 行),而不仅仅是这些值.
实际上我想在 x 轴上有天数,在 Y 轴上有访客.
解决方案
- 使用
seaborn
,这是 matplotlib 的 API.- 绘制直方图后,如果需要移动图例,请使用
条形图
My dataset is like this
Days Visitors Tuesday 23 Monday 30 Sunday 120 Friday 2 Friday 30 Tuesday 13 Monday 20 Saturday 100
How can I plot a histogram for this dataset, but assume it as a large dataset(560030 rows), not just only these values.
Actually I want to have days on x-axis and Visitors on Y-axis.
解决方案- Use
seaborn
, which is an API for matplotlib. - This will show the distribution of the number of visitors for each day of the week.
sns.histplot
import seaborn as sns import pandas as pd import numpy as np # for test data import random # for test data import calendar # for test data # test dataframe np.random.seed(365) random.seed(365) df = pd.DataFrame({'Days': random.choices(calendar.day_name, k=1000), 'Visitors': np.random.randint(1, 121, size=(1000))}) # display(df.head(6)) Days Visitors 0 Friday 83 1 Sunday 53 2 Saturday 34 3 Wednesday 92 4 Tuesday 45 5 Wednesday 6 # plot the histogram sns.histplot(data=df, x='Visitors', hue='Days', multiple="stack")
- Once the histogram is plotted, if the legend needs to be moved, use of the workaround found in seaborn issue: Not clear how to reposition seaborn.histplot legend #2280, may be necessary.
sns.distplot
- This option most clearly conveys the daily distribution of visitor counts
sns.displot(data=df, col='Days', col_wrap=4, x='Visitors')
Barplot
seaborn.barplot
- This will show the sum of all visits for a given day
sns.barplot(data=df, x='Days', y='Visitors', estimator=sum, ci=None) plt.xticks(rotation=90)
这篇关于用两个不同的列绘制直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Use
- 绘制直方图后,如果需要移动图例,请使用
查看全文