plotly

Plotly.express.scatter_3d

3D plots are very useful when you need to introduce interactivity to your figures and provide more visual accuracy.

In this article, you will learn how to create 3-dimensional scatter plots using the plotly express module.

Plotly.Express.Scatter_3d

We use the scatter_3d function to create 3D scatter plots in Plotly. The function syntax is as shown below:

plotly.express.scatter_3d(data_frame=None, x=None, y=None, z=None, color=None, symbol=None, size=None, text=None, hover_name=None, hover_data=None, custom_data=None, error_x=None, error_x_minus=None, error_y=None, error_y_minus=None, error_z=None, error_z_minus=None, animation_frame=None, animation_group=None, category_orders=None, labels=None, size_max=None, color_discrete_sequence=None, color_discrete_map=None, color_continuous_scale=None, range_color=None, color_continuous_midpoint=None, symbol_sequence=None, symbol_map=None, opacity=None, log_x=False, log_y=False, log_z=False, range_x=None, range_y=None, range_z=None, title=None, template=None, width=None, height=None)

The function parameters are as shown:

  1. data_frame – specifies the column names to be used in the plot. You can pass the values a DataFrame, a dictionary, or array_like object.
  2. x, y, z – specifies the values used to mark positions along the x, y, and z axis, respectively.
  3. color – specifies the values used to assign color to marks. You can pass either a column within a DataFrame, array_like object.
  4. symbol – specifies the values used to assign symbols to marks.
  5. size – defines the values used to assign mark sizes.
  6. title – represents the figure title.
  7. Width/height – sets the figure width and height in pixels, respectively.

Example 1

We can create a 3d scatter plot using the scatter_3d function and pass the x, y, and z parameters.

For example, the code below uses the gapminder() data from plotly express to create a simple 3d scatter plot.

import plotly.express as px
df = px.data.gapminder().query("continent=='Europe'")
fig = px.scatter_3d(df, x='year', y='lifeExp', z='gdpPercap')
fig.show()

In this example, we create a data frame using the gapminder() data. This should return a 3D scatter plot as shown:

Example 2

We can assign a unique color to each bubble by setting the color parameter as the country column within the data frame.

An example is as shown:

import plotly.express as px
df = px.data.gapminder().query("continent=='Europe'")
fig = px.scatter_3d(df, x='year', y='lifeExp', z='gdpPercap', color='country')
fig.show()

Here, we assign the color column as country. Plotly will assign a unique color for each plot based on the country.

An example resulting figure is as shown:

Example 3

We can also specify the symbol markers using the symbol parameter as shown:

import plotly.express as px
df = px.data.gapminder().query("continent=='Europe'")
fig = px.scatter_3d(df, x='year', y='lifeExp', z='gdpPercap', color='country', symbol='country')
fig.show()

Setting the symbol parameter allows plotly to assign unique symbols to the values of the plot. For example, in this case, plotly will set a different symbol for each country plot as shown:

Example 4

To set the width and height of the plot, we can set the values as:

import plotly.express as px
df = px.data.gapminder().query("continent=='Europe'")
fig = px.scatter_3d(df, x='year', y='lifeExp', z='gdpPercap', color='country', symbol='country', width=1200, height=800)
fig.show()

Setting custom width and height values allows us to improve visibility of the plots as you have control of the working area.

The resulting figure from the above plot:

You can see we are able to see more data due to the bigger working real-estate.

Conclusion

In this article, you learned how to create 3-dimensional scatter plots by using the scatter_3d function.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list