首页 > 解决方案 > Area plot of Dataframe

问题描述

i have the following Dataframe:

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.

enter image description here

标签: pythonpandasvisualization

解决方案


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')

enter image description here


推荐阅读