Hi, I’m Mingo.

Today, I recommend a visualization artifact – Plotly_express. It is very easy to get started. Basically, all the graphs can be drawn with one line of code to create a very cool visualization.

Here’s how to use this artifact in detail, with plenty of sample GIFs.

Note: how to get the source code (.ipypnb file) is left at the end of the article. Remember to download

Plotly_express, a visual artifact introduced in this article, will be explained from the following aspects:

  • plotly_expressIntroduction to the
  • plotly_expressThe installation
  • Based on theplotly_expressDraw multiple shapes

1. Environment preparation

This article was tested in the following environment.

  • Python3.7
  • Jupyter notebook
  • Pandas1.1.3
  • 4.1 Plotly_express0.

Plotly_express0.4.1 is the focus of this article, and installing it is as simple as using PIP Install

$ python3 -m pip install plotly_express
Copy the code

Summary of 2.

Before we talk about Plotly_Express, let’s take a look at Plotly. Plotly is the next generation visualization artifact, open sourced by the TopQ Quantitative team. While Ploltly is very powerful, it hasn’t been taken seriously, mostly because it’s too cumbersome to set up. Therefore, Plotly has introduced its simplified interface, Plotly_express, referred to as PX.

Px is an advanced encapsulation of Plotly. Py, which comes with a number of useful and modern drawing templates that users can use by calling simple API functions to quickly draw beautiful and dynamic visualizations.

Px is completely free and users can use it any way they want. Most importantly, PX and the rest of the Plotly ecosystem are fully compatible. Not only can you use it in Dash, you can also use Orca to export data to almost any file format.

Study materials on the official website: plotly.com/

The installation of PX is very simple, just use PIP install plotly_express to install it. Use after installation:

import plotly_express as px  
Copy the code

3. Start drawing

Next we use the built-in data set in PX to draw all kinds of beautiful graphics.

  • gapminder
  • tips
  • wind

3.1 the data set

First let’s look at the built-in data set in PX:

import pandas as pd
import numpy as np
import plotly_express as px  Import plotly. Express as px

# data set
gapminder = px.data.gapminder()
gapminder.head()  Extract the first 5 data items
Copy the code

Let’s look at all the attributes:

3.2 draught

Line lines are common in visual cartography. Use PX to make line drawings quickly:

# the line graph
fig = px.line(
  gapminder,  # data set
  x="year".Abscissa #
  y="lifeExp".# ordinate
  color="continent".# Color data
  line_group="continent".# Linear grouping
  hover_name="country".# Hover data
  line_shape="spline".# Line shape
  render_mode="svg"  # Generated picture mode
)
fig.show()
Copy the code

Then make the area map:

# area figure
fig = px.area(
  gapminder,  # data set
  x="year".Abscissa #
  y="pop".# ordinate
  color="continent".# color
  line_group="country"  # Linear group
)
fig.show()
Copy the code

3.3 a scatter diagram

Scatter is created using scatter:

Specifying size also changes the size of each point:

px.scatter(
  gapminder2007   # Drawing DataFrame data set
  ,x="gdpPercap"  Abscissa #
  ,y="lifeExp"  # ordinate
  ,color="continent"  # Distinguish colors
  ,size="pop"   # Distinguish the size of circles
  ,size_max=60  # Scatter size
)
Copy the code

The scatter can also be partitioned by specifying facet_col and animation_frame:

px.scatter(
  gapminder   # Data used for drawing
  ,x="gdpPercap" # Data used in horizontal and vertical coordinates
  ,y="lifeExp"  # Ordinate data
  ,color="continent"  # Distinguish color attributes
  ,size="pop"   # Distinguish the size of circles
  ,size_max=60  # Maximum value of a circle
  ,hover_name="country"  # the name at the top of the diagram visualization
  ,animation_frame="year"  # Horizontal scroll bar property year
  ,animation_group="country"  # Grouping of annotations
  ,facet_col="continent"   Display according to country attributes
  ,log_x=True  Take the logarithm of the x-coordinate table
  ,range_x=[100.100000]  # horizontal range of values
  ,range_y=[25.90]  # vertical range
  ,labels=dict(pop="Populations".# Attribute name change, more intuitive
               gdpPercap="GDP per Capital",
               lifeExp="Life Expectancy"))Copy the code

3.4 Geographical data mapping

In practical work, we may come into contact with maps of China or even the world, which can also be made using PX:

px.choropleth(
  gapminder,  # data set
  locations="iso_alpha".# Match color display
  color="lifeExp".# Color field selection
  hover_name="country".# hover field name
  animation_frame="year".# comments
  color_continuous_scale=px.colors.sequential.Plasma,  # Color change
  projection="natural earth"  # Global map
             )
Copy the code

fig = px.scatter_geo(
  gapminder,   # data
  locations="iso_alpha".# Match color display
  color="continent".# color
  hover_name="country".# Hover data
  size="pop".# size
  animation_frame="year".# Data frame selection
  projection="natural earth"  # Global map
                    )

fig.show()
Copy the code

px.scatter_geo(
  gapminder, # data set
  locations="iso_alpha".# Match and color to show the color
  color="continent".# color field display
  hover_name="country".# Hover data
  size="pop".# size
  animation_frame="year"  # Choice of data linkage change
  #,projection="natural earth
)
Copy the code

Use Line_geo to map:

fig = px.line_geo(
  gapminder2007,  # data set
  locations="iso_alpha".# Match and color to display data
  color="continent".# color
  projection="orthographic")   # Spherical map
fig.show()
Copy the code

3.5 Use the built-in IRIS data

Let’s first look at how to use PX to view documents with built-in data:

Select two attributes for mapping

Select two properties as horizontal and vertical coordinates to draw a scatter plot

fig = px.scatter(
  iris,  # data set
  x="sepal_width".Abscissa #
  y="sepal_length"  # ordinate
                )
fig.show()
Copy the code

Use the color parameter to display different colors:

3.6 Joint distribution map

We can display the scatter diagram and histogram together in one graph:

px.scatter(
  iris,  # data set
  x="sepal_width".Abscissa #
  y="sepal_length".# ordinate
  color="species".# color
  marginal_x="histogram".# abscissa histogram
  marginal_y="rug"   # thin figure
)
Copy the code

3.7 Violin Diagram

The violin diagram can show the distribution and error of data very well, and a line of code can also show the violin diagram:

px.scatter(
  iris,  # data set
  x="sepal_width".Abscissa #
  y="sepal_length".# ordinate
  color="species".# color
  marginal_y="violin".# Ordinate violin diagram
  marginal_x="box".# Abscissa box diagram
  trendline="ols"  # the trend line
)
Copy the code

3.8 Scatter matrix diagram

px.scatter_matrix(
  iris,  # data
  dimensions=["sepal_width"."sepal_length"."petal_width"."petal_length"].# Dimension selection
  color="species")  # color
Copy the code

3.9 Parallel coordinate diagram

px.parallel_coordinates(
  iris,   # data set
  color="species_id".# color
  labels={"species_id":"Species".# Various tag values
          "sepal_width":"Sepal Width"."sepal_length":"Sepal Length"."petal_length":"Petal Length"."petal_width":"Petal Width"},
  color_continuous_scale=px.colors.diverging.Tealrose,
  color_continuous_midpoint=2)
Copy the code

3.10 Box error diagram

# Add the next two error values to the current value
iris["e"] = iris["sepal_width"] / 100
px.scatter(
  iris,  # Drawing data set
  x="sepal_width".Abscissa #
  y="sepal_length".# ordinate
  color="species".# color value
  error_x="e".# horizontal error
  error_y="e"  # Vertical error
          )
Copy the code

3.11 Contour map

The contour map reflects the density of data:

px.density_contour(
  iris,  # Drawing data set
  x="sepal_width".Abscissa #
  y="sepal_length".# y value
  color="species"  # color
)
Copy the code

The sum of contour plots and histograms:

px.density_contour(
  iris, # data set
  x="sepal_width".# x value
  y="sepal_length".# y value
  color="species".# color
  marginal_x="rug".# The horizontal axis is a line graph
  marginal_y="histogram"   # The vertical axis is the histogram
                  )
Copy the code

3.12 Density thermal map

px.density_heatmap(
  iris,  # data set
  x="sepal_width".# x value
  y="sepal_length".# y value
  marginal_y="rug".# The ordinate values are linear graphs
  marginal_x="histogram"  # histogram
                  )
Copy the code

3.13 Parallel Category diagram

In the following graph we use the tips example, first importing data:

fig = px.parallel_categories(
  tips,  # data set
  color="size".# color
  color_continuous_scale=px.colors.sequential.Inferno)  # Color change value
fig.show()
Copy the code

3.14 bar charts

fig = px.bar(
  tips,  # data set
  x="sex".# transverse
  y="total_bill".# on the vertical
  color="smoker".# Color Specifies the value
  barmode="group".# Histogram mode value
  facet_row="time".Line # values
  facet_col="day".# Column element value
  category_orders={
    "day": ["Thur"."Fri"."Sat"."Sun"].# Order of classification
    "time": ["Lunch"."Dinner"]})
fig.show()
Copy the code

3.15 the histogram

fig = px.histogram(
  tips,  # Drawing data set
  x="sex".# The horizontal axis is gender
  y="tip".# The vertical axis is cost
  histfunc="avg".# Histogram display function
  color="smoker".# color
  barmode="group".# Bar chart pattern
  facet_row="time".Line # values
  facet_col="day".# column value
  category_orders={  # Order of classification
    "day": ["Thur"."Fri"."Sat"."Sun"]."time": ["Lunch"."Dinner"]}
)

fig.show()
Copy the code

Box figure 3.16

The box plot is also the error and distribution of real data:

# notCHED =True Displays the tapered portion of the connection
px.box(tips,  # data set
       x="day".# Horizontal data
       y="total_bill".# Vertical axis data
       color="smoker".# color
       notched=True)  # The tapered part of the joint is shown
Copy the code

px.box(
  tips,  # data set
  x="day".# transverse
	y="total_bill".# on the vertical
	color="smoker".# color
# notCHED =True # Hide arguments
      )
Copy the code

Let’s draw the violin again:

px.violin(
    tips,   # data set
    x="smoker".# horizontal coordinate
    y="tip".# vertical coordinates
    color="sex".# Color Specifies the value
    box=True.# box displays the inside of the box
    points="all".# Display numeric points at the same time
    hover_data=tips.columns)  Display all data in the result
Copy the code

3.17 Polar coordinates

Here we use the built-in Wind data:

Scatter polar diagram

Linear polar graph

fig = px.line_polar(
    wind,  # data set
    r="frequency".# radius
    theta="direction".# Angle
    color="strength".# color
    line_close=True.# Linear closure
    color_discrete_sequence=px.colors.sequential.Plasma_r)  # Color change
fig.show()
Copy the code

Columnar polar coordinates

fig = px.bar_polar(
    wind,   # data set
    r="frequency".# radius
    theta="direction".# Angle
    color="strength".# color
    template="plotly_dark".Topic #
    color_discrete_sequence=px.colors.sequential.Plasma_r)  # Color change
fig.show()
Copy the code

4. Color panel

There are a number of colors to choose from in PX, which provides a color palette:

px.colors.qualitative.swatches()
Copy the code

px.colors.sequential.swatches()
Copy the code

Topic 5.

There are three themes in PX:

  • plotly
  • plotly_white
  • plotly_dark
px.scatter(
    iris,  # data set
    x="sepal_width".# x value
    y="sepal_length".The y value
    color="species".# color
    marginal_x="box".# The abscissa is a box diagram
    marginal_y="histogram".# The ordinate is the histogram
    height=600.# height
    trendline="ols".# display trend line
    template="plotly")  Topic #
Copy the code

px.scatter(
    iris,  # data set
    x="sepal_width".# x value
    y="sepal_length".The y value
    color="species".# color
    marginal_x="box".# The abscissa is a box diagram
    marginal_y="histogram".# The ordinate is the histogram
    height=600.# height
    trendline="ols".# display trend line
    template="plotly_white")  Topic #
Copy the code

px.scatter(
    iris,  # data set
    x="sepal_width".# x value
    y="sepal_length".The y value
    color="species".# color
    marginal_x="box".# The abscissa is a box diagram
    marginal_y="histogram".# The ordinate is the histogram
    height=600.# height
    trendline="ols".# display trend line
    template="plotly_dark")  Topic #
Copy the code

6. To sum up

This article spends a lot of time explaining how to use Plotly_Express to draw: bar, line, scatter, violin, polar, and other common graphs. By observing Plotly_express graphing above, we can see that it has three main advantages:

  • Drawing quickly, a small amount of code can meet most of the graphics requirements. Basically, it’s just a few parameters that we can set up so we can get a quick graph
  • The graphics are beautiful, the visualizations drawn are brightly colored, and there are many colors to choose from.
  • Graphics are dynamically visualized. The graphics in the article are screenshots, if it is inJupyter notebookAre dynamic graphics

Hopefully, this article will help with the quick start Plotly_express visualization artifact.

This article all case code, I uploaded to Baidu cloud, want to learn the students, you can go to save. Link: pan.baidu.com/s/1EF5h2hu5… Password: w6al