Python | 散点图

本文详细介绍了Python中散点图的使用,包括常规散点图、可变大小的散点图、带有颜色代码的短划线点图和星点图,并提供了相应的代码示例。散点图用于数据可视化,允许改变每个数据点的大小、颜色等属性,适用于机器学习、蒙特卡罗模拟、案例研究和概率模型等领域。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Inherited from the Dot Plots, Scatter plots are of very similar types. It provides a power of different features for every individual point. We are allowed to vary the size, color, and other properties of each data point and which makes data more friendly to visualize.

从继承点图散点图是非常相似的类型。 它为每个单独的点提供了多种功能。 我们可以更改每个数据点的大小,颜色和其他属性,这使数据更易于可视化。

1)常规散点图 (1) Normal Scatter Plot )

Syntax:

句法:

plt.scatter(x, y, alpha=0.8)

Parameter(s):

参数:

  • x, y - data points in an array

    x , y-数组中的数据点

  • Alpha = 0.8 - opaque value (0 for transparent and 1 for opaque)

    Alpha = 0.8-不透明值(0表示透明,1表示不透明)

scatter plot (1)

2)可变大小的散点图 (2) Scatter Plot with variable size)

Syntax:

句法:

plt.scatter(x, y, s=area, alpha=0.8)

Parameter(s):

参数:

  • x, y - data points in an array

    x , y-数组中的数据点

  • Alpha = 0.8 - opaque value (0 for transparent and 1 for opaque)

    Alpha = 0.8-不透明值(0表示透明,1表示不透明)

  • s = area - the size of each point in terms of area

    s =面积 -每个点的面积(面积)

scatter plot (2)

3)带有颜色代码绿色,红色,蓝色黄色的短划线点图 (3) Dash Point Plots with color code Green, Red, Blue Yellow)

Syntax:

句法:

plt.scatter(x, y, c=colors, alpha=0.8)

Parameter(s):

参数:

  • x, y - data points in an array

    x , y-数组中的数据点

  • Alpha = 0.8 - opaque value (0 for transparent and 1 for opaque)

    Alpha = 0.8-不透明值(0表示透明,1表示不透明)

  • c = colors - colors of each point

    c =颜色 -每个点的颜色

scatter plot (3)

4)星点图,颜色代码为蓝色,绿色,红色,黄色 (4) Star Point Plot with Color Code Blue, Green, Red, Yellow)

Syntax:

句法:

plt.scatter(x, y, c=colors, s=area, alpha=0.8)

Parameter(s):

参数:

  • x, y - data points in an array

    x , y-数组中的数据点

  • Alpha = 0.8 - opaque value (0 for transparent and 1 for opaque)

    Alpha = 0.8-不透明值(0表示透明,1表示不透明)

  • c = colors - colors of each point

    c =颜色 -每个点的颜色

  • s = area - size of each point in terms of area

    s =面积 -每个点的面积(面积)

scatter plot (4)

Applications:

应用范围:

  1. Machine Learning

    机器学习

  2. Monte Carlo Simulation

    蒙特卡罗模拟

  3. Case Studies

    实例探究

  4. Probabilistic Models

    概率模型

Python代码演示散点图示例 (Python code to demonstrate example of scatter plot)

import numpy as np
import matplotlib.pyplot as plt

N = 40
x = np.random.rand(N)
y = np.random.rand(N)*10

# random colour for points, vector of length N
colors = np.random.rand(N)

# area of the circle, vectoe of length N
area = (30 * np.random.rand(N))**2  
# 0 to 15 point radii

# a normal scatter plot with default features
plt.scatter(x, y, alpha=0.8)
plt.xlabel('Numbers')
plt.ylabel('Values')
plt.title('Normal Scatter Plot')
plt.show()

# a scater plot with different size
plt.figure()
plt.scatter(x, y, s=area, alpha=0.8)
plt.xlabel('Numbers')
plt.ylabel('Values')
plt.title('Different Size')
plt.show()

# a scatter plot with different collour
plt.figure()
plt.scatter(x, y, c=colors, alpha=0.8)
plt.xlabel('Numbers')
plt.ylabel('Values')
plt.title('Different Colour')
plt.show()

# A combined Scatter Plot
plt.figure()
plt.scatter(x, y, s=area, c=colors, alpha=0.8)
plt.xlabel('Numbers')
plt.ylabel('Values')
plt.title('Combined')
plt.show()

Output:

输出:

Output is as figure


翻译自: https://www.includehelp.com/python/scatter-plot.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值