Matplotlib 简明教程

Matplotlib - Markers

在 Matplotlib 中,标记用于突出显示绘图上的单个数据点。 plot() 函数中的 marker 参数用于指定标记样式。以下是使用 Matplotlib 中标记的语法。

In Matplotlib markers are used to highlight individual data points on a plot. The marker parameter in the plot() function is used to specify the marker style. The following is the syntax for using markers in Matplotlib.

Syntax

以下是 matplotlib 库中使用标记的语法和参数。

The following is the syntax and parameters of using the markers in matplotlib library.

plt.plot(x, y, marker='marker_style')

其中,

Where,

  1. x and y − Arrays or sequences of values representing the data points to be plotted.

  2. marker − Specifies the marker style to be used. It can be a string or one of the following marker styles:

Sr.No.

Marker & Definition

1

. Point marker

2

, Pixel marker

3

o Circle marker

4

v Triangle down marker

5

^ Triangle up marker

6

< Triangle left marker

7

> Triangle right marker

8

1 Downward-pointing triangle marker

9

2 Upward-pointing triangle marker

10

3 Left-pointing triangle marker

11

4 Right-pointing triangle marker

12

s Square marker

13

p Pentagon marker

14

* Star marker

15

h Hexagon marker (1)

16

H Hexagon marker (2)

17

+ Plus marker

18

x Cross marker

19

D Diamond marker

20

d Thin diamond marker

21

Horizontal line marker

Scatterplot with pentagonal marker

此示例中,我们使用 pyplot 模块的 scatter() 函数创建带有 pentagonal 标记的散点图。

Here in this example we are creating the scatterplot with the pentagonal marker by using the scatter() function of the pyplot module.

Example

import matplotlib.pyplot as plt
# Data
x = [22,1,7,2,21,11,14,5]
y = [24,2,12,5,5,5,9,12]
plt.scatter(x,y, marker = 'p')

# Customize the plot (optional)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title(' Scatter Plot with pentagonal marker')

# Display the plot
plt.show()
marker pentagonal

Line plot with triangular marker

此示例中,我们通过将标记值作为 'v' 提供给 pyplot 模块的 plot() 函数,使用 triangle 标记创建折线图。

In this example we are creating a line plot with the triangle marker by providing marker values as 'v' to the plot() function of the pyplot module.

Example

import matplotlib.pyplot as plt
# Data
x = [22,1,7,2,21,11,14,5]
y = [24,2,12,5,5,5,9,12]
plt.plot(x,y, marker = 'v')

# Customize the plot (optional)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title(' Line Plot with triangular marker')

# Display the plot
plt.show()
triangular