Skip to content

Visualize interdependencies with a correlation plot

This template helps you visualize the correlation between numeric columns of your dataset as an interactive correlation plot. Correlation plots display the dependence between multiple numeric variables at the same time. They highlight the most strongly correlated variables in your dataset. The higher or lower the correlation value the stronger the color indication.

# Load packages
import pandas as pd
import plotly.graph_objects as go
# Upload your data as CSV and load as data frame
df = pd.read_csv('mpg.csv')
df.head()
# Visualize correlation with a heatmap
df_corr = df.corr(method='pearson')             # Choose correlation method

fig = go.Figure(
    go.Heatmap(
        z = df_corr.values.tolist(),            # Set z values (list of numbers)
        x = df_corr.columns,                    # Set x values (list of strings)
        y = df_corr.columns,                    # Set y values (list of strings)
        colorscale = 'rdylgn',                  # Set the color scale
        zmin = -1,                              # Set min value
        zmax = 1                                # Set max value
    ),
)

fig.update_layout(
    title = "Interactive Correlation Plot",     # Set title of plot
     #xaxis_title = "X Axis Title",             # Set title of x axis 
     #yaxis_title = "Y Axis Title",             # Set title of y axis
    legend_title = "Legend Title",              # Set title of legend
    font = dict(
        family = "sans-serif, sans-serif",      # Set font and default font
        size = 18,                              # Set font size
        color = "RebeccaPurple"                 # Set font color
    ),
    margin = dict(l=0, r=0, b=0)
)

fig.show(config = {"displayModeBar": False})

Appendix

TopicResource
Correlation Methodpandas documentation
Heatmap Graph Objectplotly documentation