Last week we figured out how we can create our well-known plots with Plotly. In this post we explore the diagrams that are specific to Plotly and may help us to find another approach to visualise our data.
This post is part of my journey to learn Python. You find the code for this post in my PythonFriday repository on GitHub.
Scatter matrix
With the method scatter_matrix() we can create something like a pair plot in Seaborn:
1 2 3 4 5 6 7 8 9 10 |
import pandas as pd import plotly.express as px df = px.data.iris() fig = px.scatter_matrix(df, dimensions=["sepal_width", "sepal_length", "petal_width", "petal_length"], color="species", width=800, height=800) fig.show() |
This gives us a scatter plot for each of the specified dimensions nicely arranged in a 4 by 4 grid:
Sunburst chart
With the method sunburst() we can turn multi-dimensional data into a specialised pie chart:
1 2 3 4 5 |
fig = px.sunburst(tips, path=['day', 'time', 'sex'], values='total_bill', color='time') fig.show() |
We can see the days and how the tips are distributed between male and female waiters:
Treemap chart
With the treemap() method we can get a treemap of our multi-dimensional data and see at one glance the size distribution of the values:
1 2 3 4 5 6 |
fig = px.treemap(tips, path=[px.Constant("all"), 'day', 'time', 'sex'], values='total_bill') fig.update_traces(root_color="lightgrey") fig.update_layout(margin = dict(t=50, l=25, r=25, b=25)) fig.show() |
With the help of the GraphObjects methods we can get the tips data into a readable treemap:
Icicle chart
With the icicle() method we can turn our multi-dimensional data into rectangles and then deep-dive through the dimensions in a so-called icicle chart https://plotly.com/python/icicle-charts/ :
1 2 3 4 5 6 |
fig = px.icicle(tips, path=[px.Constant("all"), 'day', 'time', 'sex'], values='total_bill') fig.update_traces(root_color="lightgrey") fig.update_layout(margin = dict(t=50, l=25, r=25, b=25)) fig.show() |
This gives us a first rectangle for the total sum of all bills, then separates into days, time, and the gender of the waiter:
Next
With the Plotly-specific plots we can visualize data in an original way that may help us to spot interesting patterns quickly. Next week we explore the different colours we can use with Plotly.
1 thought on “Python Friday #192: Plotly-Specific Diagrams”