Python 中的 Matplotlib.markers 模块
原文:https://www.geesforgeks.org/matplotlib-markers-module-in-python/
Matplotlib 是 Python 中一个惊人的可视化库,用于数组的 2D 图。Matplotlib 是一个多平台数据可视化库,构建在 NumPy 数组上,旨在与更广泛的 SciPy 堆栈一起工作。
Matplotlib.markers
matplotlib.dates 模块提供了在 matplotlib 中处理标记的函数。它由图和散点图的标记功能使用。 下表定义了 matplotlib 中所有可能的标记:
注意:需要注意的是,下面两行代码是等价的,
# line 1
plt.plot([1, 2, 3], marker = 9)
# line 2
plt.plot([1, 2, 3], marker = matplotlib.markers.CARETRIGHTBASE)
例 1:
蟒蛇 3
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
# Draw 5 points for each line
each_point = np.ones(5)
style = dict(color = 'tab:green',
linestyle = ':',
marker = 'D',
markersize = 15,
markerfacecoloralt = 'tab:red')
figure, axes = plt.subplots()
# Plot all filling styles.
for y, fill_style in enumerate(Line2D.fillStyles):
axes.text(-0.5, y,
repr(fill_style),
horizontalalignment = 'center',
verticalalignment = 'center')
axes.plot(y * each_point, fillstyle = fill_style,
**style)
axes.set_axis_off()
axes.set_title('filling style')
plt.show()
输出:
例 2:
蟒蛇 3
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
# Drawing 3 points for each line
plotted_points = np.ones(4)
txt_style = dict(horizontalalignment = 'right',
verticalalignment = 'center',
fontsize = 12,
fontdict = {'family': 'monospace'})
style = dict(linestyle = ':',
color ='0.5',
markersize = 10,
mfc ="C0",
mec ="C0")
# helper function for axes formatting
def format_ax(ax):
ax.margins(0.2)
ax.set_axis_off()
ax.invert_yaxis()
# helper function for splitting list
def split(a_list):
i_half = len(a_list) // 2
return (a_list[:i_half], a_list[i_half:])
figure, axes = plt.subplots(ncols = 2)
for ax, markers in zip(axes, split(Line2D.filled_markers)):
for y, marker in enumerate(markers):
ax.text(-0.5, y, repr(marker), **txt_style)
ax.plot(y * plotted_points, marker = marker,
**style)
format_ax(ax)
figure.suptitle('filled markers', fontsize = 14)
plt.show()
输出: