0% found this document useful (0 votes)
40 views38 pages

Chapter 1

Ok done

Uploaded by

dhruvchavda447
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views38 pages

Chapter 1

Ok done

Uploaded by

dhruvchavda447
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Plotly and the Plotly

Figure
I N T R O D U C T I O N T O D ATA V I S U A L I Z AT I O N W I T H P L O T LY I N P Y T H O N

Alex Scriven
Data Scientist
What is Plotly?

A JavaScript graphing library


Don't worry - no need to know JavaScript!

Plotly has a Python wrapper

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Why Plotly?

Plotly has a number of unique advantages:

Fast and easy to implement simple plots

Low code/low e ort options using plotly.express

(If desired) Extremely customizable

Interactive plots by default

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Creating Plotly Figures

Plotly graphs can be created:

1. With plotly.express for simple, quick plots ( px )

2. With plotly.graph_objects ( go ) for more customization

3. With plotly.figure_factory for speci c, advanced gures

We will spend most of our time on 1 and 2!

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


The importance of documentation
Save the links to key documentation!

1. Interactive, introductory docs (with many The go.scatter documentation page:


examples!)
h ps://plotly.com/python

2. Graph_objects pages for speci c plots


Index here

For example, go.scatter here

3. The base go.Figure documentation linked


here
Important when we cover
update_layout() later!

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


The Plotly Figure

A Plotly Figure has 3 main components:

layout : Dictionary controlling style of the gure


One layout per gure

data : List of dictionaries se ing graph type and data itself


Data + type = a trace . There are over 40 types!

Can have multiple traces per graph

frames : For animated plots (beyond this course)

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Inside a Plotly Figure

Let's see inside an example Plotly figure object:

print(fig)

Figure({'data': [{'type': 'bar',


'x': [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday],
'y': [28, 27, 25, 31, 32, 35, 36]}],
'layout': {'template': '...',
'title': {'font': {'color': 'red', 'size': 15},
'text': 'Temperatures of the week', 'x': 0.5}}})

What do you think this graph will look like?

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Inside our Figure
Figure({ 'data': [{'type': 'bar',
'x': [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday],
'y': [28, 27, 25, 31, 32, 35, 36]}],
'layout': {'template': '...','title': {'font': {'color': 'red', 'size': 15},
'text': 'Temperatures of the week', 'x': 0.5}}})

Type 'bar'

An X and Y axis with data noted

A title with some text around temperatures of the week

Guess: A bar chart of temperatures of the days of the week

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Creating our Figure
Constructing this gure from scratch (just this once!):

import plotly.graph_objects as go
figure_config = dict({ "data": [{"type": "bar",
"x": ["Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"],
"y": [28, 27, 25, 31, 32, 35, 36]}],
"layout": {"title": {"text": "Temperatures of the week",
"x": 0.5, "font": {'color': 'red', 'size': 15}}}})
fig = go.Figure(figure_config)
fig.show()

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Our Figure revealed
Let's see what is produced!

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Plotly's instant interactivity
Plotly provides instant interactivity:

Hover over data points

Extra interactive bu ons

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Let's practice!
I N T R O D U C T I O N T O D ATA V I S U A L I Z AT I O N W I T H P L O T LY I N P Y T H O N
Univariate
visualizations
I N T R O D U C T I O N T O D ATA V I S U A L I Z AT I O N W I T H P L O T LY I N P Y T H O N

Alex Scriven
Data Scientist
Our approach

Plotly shortcut methods:

1. plotly.express
Specify a DataFrame and its columns as arguments

Quick, nice but less customization

2. graph_objects go.X methods ( go.Bar() , go.Scatter() ) etc.


Many more customization options, but more code needed

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


What are univariate plots?

Univariate plots display only one variable

For analyzing the distribution of that variable

Common univariate plots:

Bar chart

Histogram

Box plot

Density plots

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Bar charts

Bar charts have:

X-axis with a bar per group


One group = one bar! (Hence UNI-
variate)

The y-axis height represents the value of


some variable

We built one in the last lesson!

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Bar charts with plotly.express
Let's rebuild with plotly.express

import plotly.express as px
weekly_temps = pd.DataFrame({
'day': ['Monday', 'Tuesday',
'Wednesday', 'Thursday', 'Friday',
'Saturday', 'Sunday'],
'temp': [28, 27, 25, 31, 32, 35, 36]})
fig = px.bar(data_frame=weekly_temps, x='day', y='temp')
fig.show()

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Histograms

Histograms have:

Multiple columns (called 'bins') representing


a range of values
The height of each bar = count of
samples within that bin range

The number of bins can be manual or


automatic

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Our dataset
Dataset collected by scienti c researchers on Penguins!

Contains various body measurements like, beak size, weight, etc.

Contains di erent species, genders, and ages of penguins

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Histograms with plotly.express
This is what is produced:

We can create a simple histogram:

fig = px.histogram(
data_frame=penguins,
x='Body Mass (g)',
nbins=10)
fig.show()

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Useful histogram arguments

Other px.histogram arguments :

orientation : To orient the plot vertically ( v ) or horizontally ( h )

histfunc : Set the bin aggregation (eg: average, min, max).

Check the docs for more!

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Box (and whisker) plots
Summarizes a variable visually using quartile
calculations;

Middle area represents interquartile range


Top line = 3rd quartile (75th percentile)

Middle line = median (50th percentile)

Bo om line = rst quartile (25th


percentile)

Top/bo om bars = min/max, excluding


outliers

Outlying dots are outliers

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Box plots with plotly.express
This is what is produced:

Let's create a simple box plot:

fig = px.box(data_frame=penguins,
y="Flipper Length (mm)")
fig.show()

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Useful box plot arguments

Useful box plot arguments:

hover_data : A list of column name(s) to display on hover


Useful to understand outliers

points : Further specify how to show outliers

Check the docs for more!

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Let's practice!
I N T R O D U C T I O N T O D ATA V I S U A L I Z AT I O N W I T H P L O T LY I N P Y T H O N
Customizing color
I N T R O D U C T I O N T O D ATA V I S U A L I Z AT I O N W I T H P L O T LY I N P Y T H O N

Alex Scriven
Data Scientist
Customization in general

How to customize plots:

1. At gure creation if an argument exists (like color !)

2. Using an important function update_layout()


Takes a dictionary argument

E.g.: fig.update_layout({'title':{'text':'A New Title'}})

The method chosen depends on plot type how it was created.

MANY properties possible — See the documentation

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Why customize color?

Customizing color can help you

1. Make plots look awesome!

2. Convey analytical insights


Color in this sca erplot adds a 3rd
dimension.

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Some color theory
Some other examples of RGB colors:

Computers use RGB encoding to specify


colors:

RGB = A 3-digit code (each 0-255) mixing


Red, Green, Blue together to make colors.
Imagine mixing Red, Green and Blue
paint together!

(0,0,255) is totally blue and (255,255,0) is


yellow

See more in this article

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Specifying colors in plotly.express

In plotly.express : Our simple bar chart from a previous lesson


(adding a City column)
O en a color argument (DataFrame
column) fig = px.bar(data_frame=student_scores,
A di erent (automatic) color given to x='student_name',
each category in this column y='score',
title='Student Scores by Student'
A color scale/range is used if numerical
color='city')
column speci ed fig.show()

1 Make sure to check the documentation for each gure.

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Our colors revealed
The plot before: Our plot a er:

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Color with univariate plots

Using plotly.express color argument with


univariate (bar, histogram) plots:

Histograms - stacked bars

Box plots - produces multiple (one per


category)

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Specific colors in plotly.express

What if we don't like the automatic colors?

color_discrete_map : A dictionary mapping speci c categorical values to colors using a


string RGB code speci cation — 'rgb(X,X,X)'

Can also express (basic) colors as strings such as 'red' , 'green' etc.

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Our specific colors
Let's update our colors. Sandy yellow for
'Sydney' and navy blue for 'Melbourne'
Produces:

fig = px.bar(
data_frame=student_scores,
x='student_name', y='score',
title="Student Scores by Student",
color_discrete_map={
'Melbourne': 'rgb(0,0,128)',
'Sydney': 'rgb(235, 207, 52)'},
color='city')

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Color scales in plotly.express

You can create color scales too.

Single color scales. For example, light to


dark green.

Multiple colors to merge into each other.


For example, green into blue.

color_continuous_scale allows us to do this


with built-in or constructed color scales.

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Using built-in color scales

Let's use a built-in color scale: Our plot:

fig = px.bar(data_frame=weekly_temps,
x='day', y='temp',
color='temp',
color_continuous_scale='inferno')
fig.show()

Many built-in scales available

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Constructing our own color range

Let's construct our own color scale - yellow Our plot:


through orange to red

my_scale=[('rgb(242, 238, 10)'),


('rgb(242, 95, 10)'),
('rgb(255,0,0)')]
fig = px.bar(data_frame=weekly_temps,
x='day', y='temp',
color_continuous_scale=my_scale,
color='temp')

INTRODUCTION TO DATA VISUALIZATION WITH PLOTLY IN PYTHON


Let's practice!
I N T R O D U C T I O N T O D ATA V I S U A L I Z AT I O N W I T H P L O T LY I N P Y T H O N

You might also like