python科学绘图-matplotlib中标记marker的使用方法

python使用matplotlib库,在绘制点图、线图的时候,标记初始的数据

用图标记所有标记,可以拿出来对比使用

代码:

import matplotlib
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.lines as mlines

matplotlib.use("TkAgg")
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False


def showAllMarkers(ax: plt.Axes):
    """展示所有官方标记"""
    x, y = 0, 0
    import matplotlib.lines as mlines
    ms = mlines.Line2D.markers  # 所有标记类型
    for marker in ms:
        ax.plot(x, y, marker=marker, markersize=12)
        ax.text(x, y + 0.14, marker, ha='center', fontsize=8)
        x += 1
        if x > 8:
            x = 0
            y += 1


def showSpecialMarkers(ax: plt.Axes):
    """展示特殊标记"""
    xs = [1, 2, 3, 4]
    # 空心标记,手动的方法
    ax.plot(xs, [1, 2, 3, 4], label='空心标记', marker='o',  # 标记样式
            markerfacecolor='none',  # 标记内部颜色
            markeredgecolor='r',  # 标记边框的颜色
            markeredgewidth=1,  # 标记边框的宽度
            markersize=10)  # 标记的大小
    # 官方提供的填充样式
    fs = mlines.Line2D.fillStyles
    for i, f in enumerate(fs):
        ax.plot(i, i + 1, label=f'填充样式-{f}', marker='o', fillstyle=f)
    # TeX 符号的标记,用 '$...$'
    ax.plot(xs, [4, 5, 6, 7], label='TeX符号标记', marker='$?$')
    ax.plot(xs, [5, 6, 7, 8], label='TeX符号标记', marker='$M$')
    ax.legend()


if __name__ == '__main__':
    print("所有标记类型:", mlines.Line2D.markers)
    print("填充样式:", mlines.Line2D.fillStyles)

    fig, axs = plt.subplots(1, 2)
    showAllMarkers(axs[0])
    showSpecialMarkers(axs[1])

    plt.show()

控制台:

所有标记类型:

{'.': 'point', ',': 'pixel', 'o': 'circle', 'v': 'triangle_down', '^': 'triangle_up', '<': 'triangle_left', '>': 'triangle_right', '1': 'tri_down', '2': 'tri_up', '3': 'tri_left', '4': 'tri_right', '8': 'octagon', 's': 'square', 'p': 'pentagon', '*': 'star', 'h': 'hexagon1', 'H': 'hexagon2', '+': 'plus', 'x': 'x', 'D': 'diamond', 'd': 'thin_diamond', '|': 'vline', '_': 'hline', 'P': 'plus_filled', 'X': 'x_filled', 0: 'tickleft', 1: 'tickright', 2: 'tickup', 3: 'tickdown', 4: 'caretleft', 5: 'caretright', 6: 'caretup', 7: 'caretdown', 8: 'caretleftbase', 9: 'caretrightbase', 10: 'caretupbase', 11: 'caretdownbase', 'None': 'nothing', 'none': 'nothing', ' ': 'nothing', '': 'nothing'}

填充样式:

('full', 'left', 'right', 'bottom', 'top', 'none')

效果:

 从第一个图中就可以直接对照你该使用哪一种标记

第二张图虽然是特殊的标记,但除了空心标记,其他的大概率用不到

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值