Reputation: 481
I am fairly new to plotly and and attempting to make a polar bar plot in which the theta values correspond to hours in a day. For example:
0°/360° -> 00:00
90° -> 06:00
180° -> 12:00
270° -> 18:00
Here is my code: I have a pandas datframe called "bins" which records the occurences of data entries within that hour time slot:
index justtime
0 20 5163
1 21 5007
2 19 4873
3 23 4805
4 0 4587
5 22 4565
6 18 4376
7 1 4101
8 17 4063
9 3 4014
10 2 3972
11 4 3840
12 16 3272
13 5 3239
14 15 2688
15 6 2626
16 7 1988
17 14 1740
18 8 1111
19 13 853
20 9 466
21 12 416
22 10 413
23 11 339
Then I have some code to make the plotly graph:
fig = go.Figure(go.Barpolar(
r= bins['justtime'],
theta= [x*15 for x in bins['index']],
width= [14 for x in bins['index']],
marker_line_color="black",
marker_line_width=2,
opacity=0.8
))
fig.update_layout(
template=None,
polar = dict(
radialaxis = dict(range=[0, bins['justtime'].max()], showticklabels=False, ticks=''),
angularaxis = dict(showticklabels=True, type='linear', thetaunit='', categoryorder = 'array', categoryarray = [x for x in bins['index']])
)
)
fig.show()
Instead of showing theta degrees in the axis labels around the circle, I would like to show the true hour time which would be bins['index'].
I have played around with the angularaxis arguments but cannot figure it out.
I'm sure it's fairly simple and I am overlooking it, any help would be appreciated.
Upvotes: 1
Views: 1378
Reputation: 31166
import pandas as pd
import io
import plotly.express as px
bins = pd.read_csv(io.StringIO(""" index justtime
0 20 5163
1 21 5007
2 19 4873
3 23 4805
4 0 4587
5 22 4565
6 18 4376
7 1 4101
8 17 4063
9 3 4014
10 2 3972
11 4 3840
12 16 3272
13 5 3239
14 15 2688
15 6 2626
16 7 1988
17 14 1740
18 8 1111
19 13 853
20 9 466
21 12 416
22 10 413
23 11 339"""), sep="\s+")
# add another column which is hour converted to degrees
bins = bins.assign(r=(bins["index"] / 24) * 360)
fig = px.bar_polar(bins, r="justtime", theta="r", hover_data={"Hour":bins["index"]}).update_traces(
marker={"line": {"width": 2, "color": "black"}}
)
labelevery = 6
fig.update_layout(
polar={
"angularaxis": {
"tickmode": "array",
"tickvals": list(range(0, 360, 360 // labelevery)),
"ticktext": [f"{a:02}:00" for a in range(0, 24, 24 // labelevery)],
}
}
)
Upvotes: 3