plotly

Plotly.Express.Bar_polar

“Polar charts are a type of plot that represents data along the radial and angular axis. You can use Plotly express to create polar data as bar plots using the px.bar_polar function.”

Let’s discuss.

Function Syntax and Parameters

Although the function provides a large parameter list, for simplicity, we will outline the most useful parameters you will use.

Hence, we can reduce the function syntax as shown:

plotly.express.bar_polar(data_frame=None, r=None, theta=None, color=None, title=None,

template=None, width=None, height=N0ne)

The parameters above are explained as follows:

  1. data_frame – specifies the data frame containing the column names used in the plot.
  2. r – specifies the values used to position the marks along the radial axis of the polar coordinate.
  3. theta – similar to r, but the values are used to place the marks along the angular axis of the polar coordinate.
  4. color – sets the values used to assign a unique color to the marks of the plot.
  5. title – sets the title for the figure.
  6. template – defines the template used for the plot.
  7. width/height – sets the width and height of the figure in pixels.

Example

The example code below shows how to create a bar polar plot.

import plotly.express as px
df = px.data.wind()
fig = px.bar_polar(df, r='strength', theta='direction')
fig.show()

The code above should create a bar polar plot from the provided wind data, as shown in the resulting figure:

We can also set the color for each bar based on the wind direction, as shown:

import plotly.express as px
df = px.data.wind()
fig = px.bar_polar(df, r='strength', theta='direction', color='direction')
fig.show()

Output:

We can also specify another column within the data frame as:

import plotly.express as px
df = px.data.wind()
fig = px.bar_polar(df, r='strength', theta='direction', color='frequency')
fig.show()

The output figure:

To set a dark background for the plot, you can use the template parameter as shown:

import plotly.express as px
df = px.data.wind()
fig = px.bar_polar(df, r='strength', theta='direction', color='frequency', template='plotly_dark')
fig.show()

This should return a figure as shown:

To change the bar plot colors, use the color_discrete_sequence parameter:

fig = px.bar_polar(df, r='strength', theta='direction', color='frequency', template='plotly_dark',

color_discrete_sequence=px.colors.sequential.Plasma_r)

Check the Plotly colors documentation to learn more.

Closing

That’s it for this one. In this short article, we discussed how you can create polar bar plots using Plotly express.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list