plotly

Plotly.express.scatter_polar

Polar plots are types of plots that represent data along radial and angular axes. In this one, we will cover how you can create polar data as scatter points using the scatter_polar() function.

Plotly.express.scatter_polar()

We use the scatter_polar() function to create scatter polar plots. The function syntax is expressed as below:

plotly.express.scatter_polar(data_frame=None, r=None, theta=None, color=None, symbol=None, size=None, hover_name=None, hover_data=None, custom_data=None, text=None, animation_frame=None, animation_group=None, category_orders=None, labels=None, color_discrete_sequence=None, color_discrete_map=None, color_continuous_scale=None, range_color=None, color_continuous_midpoint=None, symbol_sequence=None, symbol_map=None, opacity=None, direction='clockwise', start_angle=90, size_max=None, range_r=None, range_theta=None, log_r=False, render_mode='auto', title=None, template=None, width=None, height=None)

The following are the most common parameters you will need to interact.

  1. Data_frame – specifies the data frame, dict, or array_like used as column names.
  2. R – specifies the values used to position the marks along the radial axis in polar coordinates.
  3. Theta – sets the values used to position the marks along the angular axis along the polar coordinates.
  4. Color – specifies the values used to assign color to the marks.
  5. Symbol – specifies the values used to assign symbols to the marks.
  6. Size – sets the values used to assign the size of the marks.

The function will return a scatter polar plot of type graph_objects.Figure.

Example

The example code below depicts how we can create a simple scatter polar plot.

import plotly.express as px

df = px.data.gapminder().query("continent=='Oceania'")

fig = px.scatter_polar(df, r='year', theta='gdpPercap')

fig.show()

The example above uses the gapminder() data to create a scatter polar of the GDP Per Capita of the countries in the Oceania continent.

The above code should return the figure as shown:

Example 2

We can assign each mark a different color based on the country by setting the color parameter to the “country” column within the data frame.

Example:

import plotly.express as px

df = px.data.gapminder().query("continent=='Oceania'")

fig = px.scatter_polar(df, r='year', theta='gdpPercap', color='country')

fig.show()

The code above results in a figure as shown:

We can also set different symbols using the symbols parameter:

import plotly.express as px

df = px.data.gapminder().query("continent=='Oceania'")

fig = px.scatter_polar(df, r='year', theta='gdpPercap', color='country', symbol='country')

fig.show()

The resulting plot:

We can see that each country is assigned a different symbol and color as specified by the color and symbol parameters.

Example 3

You can also plot a section of the circle by specifying the start and end degrees (as a list) using the range_theta parameter.

For example, to plot the first 90 degrees of the circle, we can run:

import plotly.express as px

df = px.data.gapminder().query("continent=='Oceania'")

fig = px.scatter_polar(df, r='year', theta='gdpPercap', color='country', symbol='country', range_theta=[0,90])

fig.show()

This should return:

Conclusion

This tutorial provides you with the basics of creating scatter polar plots using the Plotly express module.

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