Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize scatter plot by Python

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report