Plotly_express-8-plotly Plot the scatter diagram

This article introduces the use of Plotly_Express to draw scatter graphs using scatter().

With px.scatter, each data point is represented as a marker point, whose location is given by the x and y columns.

  • throughplotly_expressLibrary to realize
  • throughplotly.graph_objectsimplementation

Scatter plot based on plotly_express

Simulated data

Just pass the data in

import plotly_express as px
import pandas as pd
import numpy as np

px.scatter(x=[1.2.6.7.9.8.3.4.5],y=[2.14.12.24.36.8.25.7.18])
Copy the code

Built-in data Gapminder



Built-in data IRIS

df = px.data.iris()
fig = px.scatter(
  df, 
  x="sepal_width".  y="sepal_length".# Plot data and xy axis
 color="species".# Dot color  size='petal_length'.# The size of the point  hover_data=['petal_width'] # Hover over the displayed data )  fig.show() Copy the code

The continuous point map is line-scatter

Continuous point graphs, such as trigonometric graphs, linear graphs, etc

x = np.linspace(0.10.100)   # 0 minus 10, 100 numbers
y = np.sin(x)
px.line(x=x,y=y,labels={"x":"t"."y":"sin(t)"})
Copy the code


Scatter diagram based on Go. Scatter

demo

  • go.FigureMake sure the canvas
  • go.ScatterDraw a picture and pass in the data you need

t = np.linspace(0.10.50)
y = np.sin(t)
fig = go.Figure(data=go.Scatter(x=t, y=y, mode="markers"))
fig.show()
Copy the code

The child drawing

Draw multiple figures in a canvas figure

  • go.figureMake sure the canvas
  • go.add_trace(): Draw different shapes on one canvas
  • fig.show(): Display graphics
np.random.seed(2)

N = 100
random_x = np.linspace(0.1, N)

random_y0 = np.random.randn(N) + 8 random_y1 = np.random.randn(N) random_y2 = np.random.randn(N) - 8 random_y3 = np.random.randn(N) - 4  fig = go.Figure()  # add traces fig.add_trace(go.Scatter(x=random_x,y=random_y0,  mode="markers",name="markers")) fig.add_trace(go.Scatter(x=random_x, y=random_y1,  mode='lines+markers',name='lines+markers')) fig.add_trace(go.Scatter(x=random_x, y=random_y2,  mode='lines',name='lines')) fig.add_trace(go.Scatter(x=random_x, y=random_y3,  mode='markers',name='markers')) fig.show() Copy the code

Bubble Scatter – Bubble Scatter

Bubble scatter plot: the size of a point changes as the value of the coordinate axis changes

fig = go.Figure(go.Scatter(
  x=np.linspace(0.50.10),
  y=np.random.randint(0.50.10),
  mode="markers".  marker=dict(size=np.random.randint(0.50.10),  This is implemented in the form of a dictionary
 color=np.random.randint(50.100.10))  )) fig.show() Copy the code

t = np.linspace(0.10.100)

fig = go.Figure()

fig.add_trace(go.Scatter(
 x=t, y=np.sin(t),  name='sin'. mode='markers'. marker_color='rgba(20, 180, 60, .8)' ))  fig.add_trace(go.Scatter(  x=t, y=np.cos(t),  name='cos'. mode='markers'. marker_color='rgba(25, 182, 193, .9)' ))  # fig.update_traces(mode='markers', marker_line_width=2, marker_size=10) fig.update_layout(title='Styled Scatter'.# titles  yaxis_zeroline=False, xaxis_zeroline=False) fig.show() Copy the code

t = np.linspace(0.10.100)

fig = go.Figure()

fig.add_trace(go.Scatter(
 x=t, y=np.sin(t),  name='sin'. mode='markers'. marker_color='rgba(20, 180, 60, .8)' ))  fig.add_trace(go.Scatter(  x=t, y=np.cos(t),  name='cos'. mode='lines'. marker_color='rgba(25, 182, 193, .9)' ))  # Set options common to all traces with fig.update_traces # Set the size and spacing of the entire scatter plot fig.update_traces(mode='markers', marker_line_width=2, marker_size=8) fig.update_layout(title='Styled Scatter'. yaxis_zeroline=True, xaxis_zeroline=False)   fig.show() Copy the code

Data Labels on Hover

How to display hover data when using Go. Scatter

df = px.data.iris()
fig = go.Figure(data = go.Scatter(   The first property in the # Figure class is data
    x=df["sepal_length"].# xy data
    y=df["sepal_width"].    mode="markers".# representation of points
 marker_color=df["species_id"].# dot color, px is color  text=df["species"])) # hove_data in px fig.show() Copy the code

Scatter with a Color Dimension

This refers to the constant color change on the right side of the graph

x = np.linspace(0.10.500)
y = np.random.randint(0.100.500)

fig = go.Figure(data=go.Scatter(
    x=x,
 y=y,  mode="markers". marker=dict( Marker is the form of a dictionary  size=20. color=np.random.randint(0.100.500), # specify the color range  colorscale="Viridis".# Choose which color  showscale=True # Whether the color scale on the right is displayed  ) ))  fig.show() Copy the code

Default color

x = np.linspace(0.10.500)
y = np.random.randint(0.100.500)

fig = go.Figure(data=go.Scatter(
    x=x,
 y=y,  mode="markers". marker=dict( Marker is the form of a dictionary  size=20. color=np.random.randint(0.100.500),  showscale=True  ) ))  fig.show() Copy the code

🏆 technology project phase iii | data visualization of those things…