Plotting with Plotly¶
Plotly’s Python library is free and open source! It provides a rich environment for all kinds of graphs (line graphs, scatter plots, histograms, bubble plots, and more.)
To learn all about Plotly for use in Python, please see the documentation online at https://plotly.com/graphing-libraries/
Getting started¶
We will use two modules, Plotly Express and Plotly Graph Objects
import plotly.express as px
import plotly.graph_objects as go
Example 1 - a line graph¶
Here is a very simple example. We input a range of x values, and y values, and plot.
fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
fig.show()
Notice the plot above is live. You can click on it, scroll on it, even save it to your hard drive.
It is worth noting the syntax for the scatter command: you can input lists of x and y values as parameters as well. See the following.
listx = [0,1,2,3,4]
listy = [0,1,4,9,16]
fig = px.scatter(x=listx, y=listy)
fig.show()
Example 2 - a bar graph¶
Here is another simple example. We input a range of x values, and y values, and plot.
listx = [0,1,2,3,4]
listy = [0,1,4,9,16]
fig = px.bar(x=listx, y=listy)
fig.show()
Example 3 - a pretty graph¶
Let’s use Plotly graph objects to make a pretty graph with lots of features
listx = [0,1,2,3,4]
listy = [0,1**1.5,2**1.5,3**1.5,4**1.5]
listz = [0,1**2,2**2,3**2,4**2]
fig = go.Figure()
fig.add_trace(go.Bar(x=listx, y=listx, name='Linear'))
fig.add_trace(go.Scatter(x=listx, y=listy, mode='lines', name='Quasilinear'))
fig.add_trace(go.Scatter(x=listx, y=listz, mode='markers', name='Quadratic'))
fig.update_layout(
title_text='Plot of various rates of growth',
xaxis_title_text = 'X values for base',
yaxis_title_text = 'Y values as powers of X'
)
fig.show()
Example 4 - Plotting from a DataFrame¶
Data scientists often use a data structure known as a DataFrame to hold their data. A DataFrame is structured like a spreadsheet, with rows and columns holding various numbers representing the data.
In the following example, we load in a DataFrame (defined in the pandas module) representing some information about a collection of flowers knows as irises. We then quickly can plot using the scatter function in the Plotly Express module.
df = px.data.iris() # iris is a pandas DataFrame
fig = px.scatter(df, x="sepal_width", y="sepal_length")
fig.show()
More on DataFrames¶
The iris DataFrame created above contains a great deal of information about these flowers. Using the “head” function, we can look at some of the details of the information in the DataFrame. We could use different column headers to get a different plot.
df.head()
sepal_length | sepal_width | petal_length | petal_width | species | species_id | |
---|---|---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa | 1 |
1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa | 1 |
2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa | 1 |
3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa | 1 |
4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa | 1 |
fig = px.scatter(df, x="petal_width", y="petal_length")
fig.show()
Summary¶
There are many, many uses of Plotly for creating interesting graphs. This is only the briefest of introductions. As mentioned above, please go to the Plotly webpages for more details on how to use this tool.