Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize scatter plot by Python

Shulou Source: shulou.com Published: 2022-06-01 04:14:00 09月26日 Update

This article mainly explains "how to achieve scatter plot in Python". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to achieve scatter plot in Python".

What is an association graph?

An association graph is an image that looks for the relationship between two things. It can show us how one thing changes with the change of another.

Typical correlation graphs are: line chart, scatter chart, correlation matrix, etc.

When will we need an association diagram?

1. Data report & academic research

Show trends: such as how product sales change over time, how intelligence changes with education, etc.

Show status: the transaction rate of customers of different ages, the skill requirements of production staff corresponding to different production costs, etc.

2. Data exploration & data interpretation

Explore data relationships, help understand and try, and promote research

3. Statistics & Machine Learning

Explore data relations and guide data preprocessing and model selection

Library import numpy as np is required for scatter chart import

Import pandas as pd

Import matplotlib as mpl

Draw a simple scatter plot

Using pd.scatter function to draw scatter plot

A simple example of a scatter chart:

# define data, x1 take the number of random machines

X1 = np.random.randn (10)

X2 = x1 + x1x 2-10

# define the canvas. When only this picture is available, the following sentence is not required

Plt.figure (figsize= (8, 4))

# draw an image

Plt.scatter (x1, # Abscissa

X2, # ordinate

S = 50, # the size of the data point

C = "red", # Color of the data point

Label = "red points" # legend

)

# decorative graphics

# display the legend, and a warning appears in the following sentence with no label attribute in the above plt.scatter

Plt.legend ()

# display graphics

Plt.show ()

Draw scatter diagrams of multiple legend colors (take two kinds of examples)

To draw a graph, you need to find three elements:

1. Data for drawing, x1pl x2

2. List of tags

3. Color

Legend:

# generate a data table with 10 rows and 2 columns

X = np.random.randn (10jue 2)

Y = np.array ([0pc0pr 1pl 0pl 0pl 0pl 0p0p0p0])

Plt.figure (figsize= (8, 4))

Colors = ["red", "black"] # establish the color list

Label is a list of categories that establish the tag as = ["Zero", "One"] #

# overlay the images formed by multiple columns by looping through the x.shape

For i in range (x.shape [1]):

Plt.scatter (

X [yearly quotation 0]

X [yearly publication 1]

C=colors [i]

Label=labels [i]

)

# there are several categories in the tag, so we need to cycle through several times, drawing dots of one color at a time

Plt.legend ()

Plt.show ()

Draw a complex scatter plot

Creating our own data is too simple, we can use the simple dataset of the online god to learn to draw complex scatter maps.

Midwest = pd.read_csv ("https://raw.githubusercontent.com/selva86/datasets/master/midwest_filter.csv")

# filter tags to remove duplicate tags

Categories = np.unique (midwest ['category']) # remove all duplicates

Plt.figure (figsize= (160.10))

For i in range (len (categories)):

Plt.scatter (midwest.loc [midwest ["category"] = = categories [I], "area"]

, midwest.loc [midwest ["category"] = = categories [I], "poptotal"]

, swarm 20

, c=np.array (plt.cm.tab10 (i/len (categories) .reshape (1mam Murray 1)

, label=categories [i]

)

Plt.legend ()

Plt.show () so far, I believe you have a deeper understanding of "how to achieve scatter plot in Python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Data labels colors legends associations changes learning things images graphics different complex content time categories loops production research deeper typical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Docker NVidia MySQL Shulou Technology