Lesson

Polar charts

Learn Polar charts in SQLPad's Data Science in Action: Interactive Visualization with Plotly and Pandas course with practical examples and guided lessons.

Introduction to Polar Charts

In this lesson, we will explore the usage of Polar charts in Plotly for creating interactive visualizations. Polar charts are a unique way of representing data in a circular format, which can be especially useful for visualizing periodic or cyclical data. With Plotly, we can create highly customizable polar charts, including scatter plots, line plots, bar plots, and more. We will also see how to combine these charts with Pandas data manipulation capabilities to efficiently analyze and visualize complex datasets.

Creating a Basic Polar Chart

Load the data

import plotly.express as px

df = px.data.wind()
print(df.head())

Creating a Basic Polar Chart

import plotly.express as px

df = px.data.wind()

fig = px.line_polar(df, r='frequency', theta='direction', color='strength', template='plotly_dark', line_close=True)

fig.show()

Customizing the Polar Chart

Load the data

import plotly.express as px

df = px.data.wind()
print(df.head())

Customizing the Polar Chart

import plotly.graph_objs as go

# convert 'strength' to numerical codes if it is a categorical variable
df['strength'] = df['strength'].astype('category').cat.codes

fig = go.Figure()

fig.add_trace(go.Scatterpolar(
    r=df['strength'],
    theta=df['direction'],
    mode='markers',
    marker=dict(
        color=df['strength'],
        size=8,
        symbol='circle',
        line=dict(
            color='rgba(255, 255, 255, 0.5)',
            width=1
        ),
        opacity=0.7
    ),
    name='Wind Strength and Direction'
))

fig.update_layout(
    title='Customized Polar Chart: Wind Strength and Direction',
    font=dict(size=12),
    polar=dict(
        radialaxis=dict(
            visible=True,
            range=[0, df['strength'].max()]
        ),
        angularaxis=dict(
            visible=True,
            rotation=90,
            direction='counterclockwise'
        )
    ),
    showlegend=True
)

fig.show()

Visualizing Wind Speed and Direction with Polar Charts

Load the data

import plotly.express as px

df = px.data.wind()
print(df.head())

Visualizing Wind Speed and Direction with Polar Charts

import plotly.express as px

df = px.data.wind()

fig = px.scatter_polar(df,
                       r='frequency',
                       theta='direction',
                       color='strength',
                       symbol='strength',
                       size='frequency',
                       title='Wind Speed and Direction with Polar Charts')

fig.show()

Animating the Polar Chart

Load the data

import plotly.express as px

df = px.data.wind()
print(df.head())

Animating the Polar Chart

import plotly.express as px

df = px.data.wind()

fig = px.scatter_polar(df, r="frequency", theta="direction", color="strength", 
                       animation_frame="strength", symbol="strength",
                       size="frequency", template="plotly_dark",
                       title="Wind Speed and Direction Animation")

fig.show()

Exercises

1. Polar Charts with Plotly

Instruction

Create a polar chart using Plotly to visualize the wind dataset. Customize the chart by modifying the radial axis, angular axis, and adding a title. Finally, display the chart.

My Solution

# Your solution goes here

Hint

  1. Import plotly.graph_objects and plotly.express.
  2. Load the wind dataset using px.data.wind().
  3. Create a Scatterpolar trace with radial and angular data.
  4. Set the mode to 'lines' and line_shape to 'spline'.
  5. Customize the radial and angular axis using update_layout.
  6. Add a title to the chart.
  7. Display the chart using fig.show().

Solution

import plotly.graph_objects as go
import plotly.express as px

df = px.data.wind()

fig = go.Figure(go.Scatterpolar(
    r=df['frequency'],
    theta=df['direction'],
    mode='lines',
    line_shape='spline',
    name='Wind',
    marker=dict(color='red')
))

fig.update_layout(
    polar=dict(
        radialaxis=dict(
            visible=True,
            range=[0, df['frequency'].max()],
            showticklabels=False,
            gridcolor='gray'
        ),
        angularaxis=dict(
            direction='clockwise',
            period=360,
            gridcolor='gray',
            linecolor='black'
        )
    ),
    title='Wind Data Visualization'
)

fig.show()