当数据的列是每月的每一天时,如何绘制条形图? [英] How to plot bar chart when the columns of the data are each day of each month?

查看:112
本文介绍了当数据的列是每月的每一天时,如何绘制条形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何绘制这张照片那样的条形图.

I don't know how to plot the bar chart like this photo.

有人可以帮我吗?

推荐答案

pandas命令Series.plot.bar接受color关键字,您可以在其中输入颜色列表(采用RGBA或颜色名称).当列表太短时,我不确定该命令遵循什么逻辑,但是如果列表长度正确,它将起作用. 示例:

The pandas command Series.plot.bar accepts a color keyword, where you can input a list of colors (in RGBA or color names). I'm not entirely sure on what logic does the command follow when the list is too short, but if the list is the right length, it'll work. Example:

%matplotlib inline
import pandas as pd
this_series = pd.Series([0.5, 0.6, 0.1, 0.1, 0.05, 0.05, 0.7, 0.3, 0.2], name='foo')
this_series.plot.bar(color=['blue','black', 'brown', 'red','red','green','yellow'])

从您的评论中,我认为我可以理解您的问题.将来,请通过提出更清晰的问题来帮助社区帮助您.

from your comments, I think I can understand your question. In the future, please help the community help you by crafting clearer questions.

要绘制每个月的值总和,并按照示例中的数据结构进行操作,就可以这样做:

To plot the sum of values per month, with your data structure being as in the example, you can do like this:

数据(请参阅-一个仍然可以使用的最小示例):

data (see - a minimal example that is still enough to work with):

df = pd.DataFrame(columns = ['hour_formatted', '11_jan_18', '13_jan_18', '14_mar_18'],
                 data = [['00:00', 3, 3, 4], ['00:01', 6, 3, 4], ['00:01', 100, 300, 500]])

不需要小时",我们可以每天对数据值求和(毕竟,我们将按月对它们求和……或者我认为如此).

The "hour" is unneeded, and we can sum the data values per day (after all, we'll sum them by month... or so I think).

df2 = df.drop('hour_formatted', axis=1).sum()

此后,我们只需要一天中的一个月.按照您的格式,它是按'_'分割的第二项:

Afterwards, we only need the month from the day. In your format, it's the second item when splitting by '_':

df2.index = [x.split('_')[1] for x in df2.index]
df2.index.rename('month', inplace=True)

之后,您可以按月份分组(请参阅文档中的groupby ),然后绘制条形图:

Afterwards, you can group by month (see groupby from docs), and plot a bar plot:

df3 = df2.reset_index().groupby('month').sum()[0] # the [0] part is to take first column from a dataframe that's created here. There might be a more elegant way
df3.plot.bar(color=['black', 'red'])

这篇关于当数据的列是每月的每一天时,如何绘制条形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆