How to Draw a 3D Plot Using plotly in Python (Example)
This article provides several examples of 3D plots in plotly using the Python programming language.
Table of contents:
Note: This article was created in collaboration with Kirby White. Kirby is a Statistics Globe author, innovation consultant, data science instructor. His Ph.D. is in Industrial-Organizational Psychology. You can read more about Kirby here!
Before we start, please note that you can view and instantly run the code for this project in a colab notebook, here.
Modules and Example Data
Please install and load these modules as a first step:
import plotly.express as px
We’ll use the gapminder
dataset for this example, which is included with the plotly module. This dataset contains information about life expectancy, population size, and GDP per capita for over 100 countries repeatedly from 1952-2007. We’ll store this data frame in an object called df
:
df = px.data.gapminder() df country continent year lifeExp pop gdpPercap iso_alpha iso_num # 0 Afghanistan Asia 1952 28.801 8425333 779.445314 AFG 4 # 1 Afghanistan Asia 1957 30.332 9240934 820.853030 AFG 4 # 2 Afghanistan Asia 1962 31.997 10267083 853.100710 AFG 4 # 3 Afghanistan Asia 1967 34.020 11537966 836.197138 AFG 4 # 4 Afghanistan Asia 1972 36.088 13079460 739.981106 AFG 4
Two Examples
Let’s look at how the life expectancy and GDP per person has changed over the years, but only the averages within each continent:
fig1 = px.line_3d( data_frame = df.groupby(['continent', 'year']).mean().reset_index() , x = 'year' , y = 'gdpPercap' , z = 'lifeExp' , color = 'continent' ) fig1.show() # continent year lifeExp pop gdpPercap iso_num # 0 Africa 1952 39.135500 4.570010e+06 1252.572466 458.826923 # 1 Africa 1957 41.266346 5.093033e+06 1385.236062 458.826923 # 2 Africa 1962 43.319442 5.702247e+06 1598.078825 458.826923 # 3 Africa 1967 45.334538 6.447875e+06 2050.363801 458.826923
As you can see, our data is visualized as a line plot in a three-dimensional space.
If we want to look at the same data, but only for countries in the European continent, we can use this code:
fig2 = px.line_3d( data_frame = df[df['continent'] == 'Europe'].groupby(['country', 'year']).mean().reset_index() , x = 'year' , y = 'gdpPercap' , z = 'lifeExp' , color = 'country' ) fig2.show() # country year lifeExp pop gdpPercap iso_num # 0 Albania 1952 55.230 1282697.0 1601.056136 8.0 # 1 Albania 1957 59.280 1476505.0 1942.284244 8.0 # 2 Albania 1962 64.820 1728137.0 2312.888958 8.0 # 3 Albania 1967 66.220 1984060.0 2760.196931 8.0
Customizing Colors
If we want to specify the colors to use for each line, we can use the color_discrete_map
argument to create a dictionary of group:color pairs. We can use the name of colors or hex codes, as shown below:
fig3 = px.line_3d( data_frame = df[df['continent'] == 'Oceania'].groupby(['country', 'year']).mean().reset_index() , x = 'year' , y = 'gdpPercap' , z = 'lifeExp' , color = 'country' , color_discrete_map = {"Australia":"gray", "New Zealand":"#a53abd"} ) fig3.show() # country year lifeExp pop gdpPercap iso_num # 0 Australia 1952 69.120 8691212.0 10039.59564 36.0 # 1 Australia 1957 70.330 9712569.0 10949.64959 36.0 # 2 Australia 1962 70.930 10794968.0 12217.22686 36.0 # 3 Australia 1967 71.100 11872264.0 14526.12465 36.0
Customizing Line Styles
Similar to the way we specify custom colors for each line, we can change the style of the line within each group. Plotly has several built-in line styles (‘dash’, ‘dashdot’, ‘dot’, ‘longdash’, ‘longdashdot’, and ‘solid’), two of which are demonstrated here:
fig4 = px.line_3d( data_frame = df[df['continent'] == 'Oceania'].groupby(['country', 'year']).mean().reset_index() , x = 'year' , y = 'gdpPercap' , z = 'lifeExp' , color = 'country' , line_dash = 'country' , line_dash_map = {"Australia":"dot", "New Zealand":"longdashdot"} ) fig4.show()
Further Resources
In this tutorial, you have learned how to create three-dimensional line plots. However, please note that you could also show other types of plotly graphics such as scatterplots in 3D.
In case you want to learn more on how to create interactive graphics in plotly, please have a look at the related articles below:
- plotly Barplot in Python
- plotly Boxplot in Python
- plotly Histogram in Python
- plotly Line Plot in Python
- plotly Scatterplot in Python
- Introduction to plotly in Python
- Introduction to Python
Statistics Globe Newsletter