Reputation: 11
The first part of code is code from plotly website and it shows what i would like to make with my own data available.
import plotly.express as px
import pandas as pd
fig= px.bar_polar(df2, r="frequency", theta = "direction",
color = "strength", template = "plotly_dark",
color_discrete_sequence = px.colors.sequential.Plasma_r)
fig.show()
Desired graph:
This is my code so far:
import plotly.express as px
import pandas as pd
df = pd.read_csv (r"Lidar_final_corrected.csv")
print(df)
wind_dir Windspeed
0 227.35 2.12
1 233.41 1.65
2 227.75 1.52
3 217.75 1.71
4 204.64 2.21
... ... ...
3336 222.33 17.89
3337 221.52 17.21
3338 219.37 15.45
3339 217.23 16.09
3340 218.31 16.18
[3341 rows x 2 columns]
fig= px.bar_polar(df,theta = "wind_dir",
color = "wind_dir", template = "plotly_dark",
color_discrete_sequence = px.colors.sequential.Plasma_r)
fig.show()
Current output:
Now if you look closely you can see that it is plotting something, but the colors are not right. I left out the frequency because I only have two columns to work with. Is there a workaround to make this plot work without the frequency?
(PS: the data is data based on time series so every 10min or 1 min a new entry is in there. I deleted the timestamp because I don't think I need it.)
Upvotes: 1
Views: 2423
Reputation: 138
you need to do a pandas groupby
grp = df.groupby(["wind_dir", "wind_speed"]).size().reset_index(name="frequency")
as explained in this answer
Then you can use the plotly code same as in the plotly docs!
fig = px.bar_polar(grp, r="frequency", theta="wind_dir",
color="wind_speed", template="plotly_dark",
color_discrete_sequence=px.colors.sequential.Plasma_r
)
fig.show()
Happy plotting! (for better results you should bin the wind directions and wind speeds. (at least round them). Example of proper binning: https://community.plotly.com/t/wind-rose-with-wind-speed-m-s-and-direction-deg-data-columns-need-help/33274/5
Upvotes: 1