SoFunction
Updated on 2024-10-29

Python using Plotly plotting tool to plot scatter plots, line plots

Today, I'm working on Plotly's method of plotting scatterplots for your reference, which is as follows

Using Python 3.6 + Plotly

Plotly version 2.0.0

Before we get started let's say that the library Numpy also needs to be installed, the installation is covered in my other blog:Python3.6 Numpy library to download and install graphic tutorials

Because Plotly doesn't have its own separate linear graph function, it implements both linear and scatter graphics all in one function

This function is the Scatter function

Here are a few simple examples

First draw a pure scatter plot with the following code:

import plotly
import plotly.graph_objs as go
import numpy
 
pyplt =  #Use offline mode
N = 100
random_x = (0, 1, N)
random_y0 = (N)+5
random_y1 = (N)
random_y2 = (N)-5
# Above is some random data
trace0 = (
 x = random_x,
 y = random_y0,
 mode = 'markers', # Plotting pure scatter plots
 name = 'markers' # Legend name
)
data = [trace0]
pyplt(data, filename='tmp/scatter_diagram.html')Where to place the #html

Running the program will result in the graphic shown below

Next we draw a linear graph with the same data as before. See what it looks like, the code is as follows

import plotly
import plotly.graph_objs as go
import numpy
 
 
pyplt =  #Use offline mode
N = 100
random_x = (0, 1, N)
random_y0 = (N)+5
random_y1 = (N)
random_y2 = (N)-5
trace1 = (
 x = random_x,
 y = random_y2,
 mode = 'lines', # Line graphs
 name = 'lines'
)
data = [trace1]
pyplt(data, filename='tmp/')

We will get the line graph as shown below

Below we combine the linear plot, and the scatter plot together

import plotly
import plotly.graph_objs as go
import numpy
pyplt =  #Use offline mode
N = 100
random_x = (0, 1, N)
random_y0 = (N)+5
random_y1 = (N)
random_y2 = (N)-5
trace1 = (
 x = random_x,
 y = random_y1,
 mode = 'lines+markers', # Scatter + line plotting
 name = 'lines+markers'
)
data = [trace1]
pyplt(data, filename='tmp/')

The following legend is obtained

Example of three diagrams represented in one

import plotly
import plotly.graph_objs as go
import numpy
pyplt =  #Use offline mode
N = 100
random_x = (0, 1, N)
random_y0 = (N)+5
random_y1 = (N)
random_y2 = (N)-5
trace0 = (
 x = random_x,
 y = random_y0,
 mode = 'markers', # Plotting of pure scatter
 name = 'markers' # Curve name
)
trace1 = (
 x = random_x,
 y = random_y1,
 mode = 'lines+markers', # Scatter + line plotting
 name = 'lines+markers'
)
trace2 = (
 x = random_x,
 y = random_y2,
 mode = 'lines', # Line drawing
 name = 'lines'
)
data = [trace0,trace1,tarace2]
pyplt(data, filename='tmp/')

The following diagram is obtained

As you can see, three graphs, plotted on one graph!

You can also set the style below to see an example, change the color, the code is as follows:

import plotly
import plotly.graph_objs as go
import numpy
pyplt =  #Use offline mode
N = 100
random_x = (0, 1, N)
random_y0 = (N)+5
random_y1 = (N)
random_y2 = (N)-5
trace0 = (
 x = random_x,
 y = random_y0,
 mode = 'markers', # Pure scatterplot
 name = 'markers', # Curve name
 marker = dict(
 size = 10, # Set the width of the point
 color = 'rgba(255, 182, 193, .9)', # Set the color of the curve
 line = dict(
  width = 2, # Set the width of the line
  color = 'rgb(0, 255, 0)' # Set the color of the line
 )
 )
)
data = [trace0]
pyplt(data, filename='tmp/')

Marker parameter settings are very important, set the color color, size size

line sets the line width, color sets the line color, and so on.

This is the whole content of this article.