python - Area plot of Dataframe
问题描述
i have the following Dataframe:
it contains a Timestamp as index of the df and two columns, where the first column descriebes the wind direction (S = South, SE = South-East and so on). The second column descriebs the precentage during the Timestamp. EG. South-East with 87.5 % and South with 12.5 % during one entire Day (2018-02-21).
The Area plot shall visualize the precentage of every direction for each day. With the days on the x-axis and the precentage on the Y-axis adding up to a total of 100%.
i do not know how to get my current df to the one i need to be able to visualize my desired graphic.
Any ideas?
The Timestamp still needs to be the Index, but i need a column for each possible Winddirection. The column then shall contain the precantage for each time step of the specific wind direction.
Here is a super abstract drawing only for North, East, South and West direction.
解决方案
You need to first .pivot
and then you can plot.
Sample Data
import pandas as pd
df = pd.DataFrame({'date': pd.to_datetime(['2018-02-21', '2018-02-21',
'2018-02-22', '2018-02-22', '2018-02-22',
'2018-02-23', '2018-02-23']),
'direction': ['SE', 'S', 'SE', 'E', 'S', 'E', 'SE'],
'pct': [87.5, 12.5, 75, 20.8333333, 4.166667, 54.1557, 45.8333]})
Code
df.pivot(index='date', columns='direction', values='pct').fillna(0).plot(kind='area')
推荐阅读
- c++ - 如何使用 strcmp 使循环结束循环
- angular - 如何将 metronic 的引导主题用于其他有角度的现有项目?
- java - 如何在java中使用预定义变量设置计数器
- c++ - 模板类在非参数化构造函数上抛出错误
- amazon-web-services - 尝试 SSH bitnami wordpress 安装时出现权限问题
- javascript - 如何使用 Nodejs 永久删除最新版本的 S3 对象
- excel - 搜索关键词列表
- c++ - 将对象添加到向量,然后从迭代器更新它
- sql-server - 使用不同方法执行程序
- flutter - 如何在颤振中使用字符串创建 JSONArray