Plots with Plotly Express
Contents
Plots with Plotly Express#
The plotly library has two plotting interfaces, or APIs you can work with. The simplest
Creating a Line Plot with Plotly Express#
import pandas as pd
import plotly.express as px
s_orbitals = pd.read_csv("s_orbitals_1D.csv")
s_orbitals.head()
r | 1s | 2s | 3s | |
---|---|---|---|---|
0 | 0.000000 | 0.564190 | 0.199471 | 0.108578 |
1 | 0.517241 | 0.336349 | 0.114183 | 0.061683 |
2 | 1.034483 | 0.200519 | 0.057408 | 0.029966 |
3 | 1.551724 | 0.119542 | 0.020580 | 0.009313 |
4 | 2.068966 | 0.071266 | -0.002445 | -0.003390 |
fig = px.line(s_orbitals, x="r", y="1s")
fig.show()
Adding more lines
fig = px.line(s_orbitals, x="r", y=["1s", "2s", "3s"])
fig.show()
Changing labels - plotly also supports using symbols.
fig = px.line(s_orbitals, x="r", y=["1s", "2s", "3s"], labels={"r":"r", "value":r"$\psi$", "variable": "orbital"})
fig.show()
Modifying the Layout of a Plotly Figure#
When we create a figure with plotly express, we get a figure variable back. Above, we have been working with fig.show()
to show the figure, but you can also use this variable to update or change things about the figure.
The information about the layout of a plotly figure is stored in fig.layout
.
We can change or update the layout of a figure using the fig.update_layout
function. The update_layout
function takes key word arguments. You can see a list here.
The example below makes several changes to this figure. You can try each of these individually to see how they change the figure.
fig.update_layout(
font_family="Times New Roman",
font_size=20,
font_color="dark gray",
showlegend=True,
yaxis=dict(gridcolor="#d3d3d3", linecolor="lightslategray", zerolinecolor="#d3d3d3"),
xaxis=dict(gridcolor="#d3d3d3", linecolor="lightslategray"),
plot_bgcolor="white",
)
fig.show()
Exporting Plots as Images#
To export a figure from plotly, we have to have another package installed
pip install -U kaleido
fig.write_image("s_orbitals.png")
fig.write_image("s_orbitals.svg")
Plotly also allows you to save an interactive html version of your file. If we want our mathematical symbols to also show up, we will need to add the argument include_mathjax="cdn"
.
fig.write_html("s_orbitals.html", include_mathjax="cdn")
Creating a Map Plot with Plotly Express#
Plotly Express has some fun maps you can make which are probably not the kind of maps you usually make as a physical scientist. For example, we can create a map which shows locations of institutions of the students registered for this webinar. We will read in a file called visualization_institutions.csv
which has institution names, the latitude, the longitude, and the count of the number of students.
student_locations = pd.read_csv("visualization_institutions.csv")
student_locations.head()
institution | lat | lon | count | |
---|---|---|---|---|
0 | Washington university in St. Louis | 38.648789 | -90.310796 | 1 |
1 | Rutgers University, New Brunswick | 40.500819 | -74.447399 | 1 |
2 | Federal Institute of São Paulo | -22.727627 | -47.652870 | 1 |
3 | University of North Texas | 33.207488 | -97.152586 | 1 |
4 | TCIS | 33.451189 | -84.481229 | 1 |
This plot is very easy to create once we have our data in the right format. In fact, you may be more interested in how we obtained the latitude/longitude data and unique institutions. We used the google maps
API and some pandas
magic to do this, but we would be happy to provide you with a script upon request (you will have to make your own Google Maps API key).
Plotly Express has a scatter_geo
function which allows us to pass latitude and longitude as coordinates. For this plot, we are setting the marker size to be the count of the number of students, and for the institution name to show up when we hover over a point.
fig = px.scatter_geo(student_locations, lat="lat", lon="lon", size="count", hover_name="institution")
fig.show()