【问题标题】:replace labels of levels in contour with a marker用标记替换轮廓中的级别标签
【发布时间】:2020-12-05 03:35:49
【问题描述】:

是否可以用 matplotlib 中的圆形和星形等标记替换轮廓中的级别文本? 我正在使用此代码来显示级别。但我想在情节中用星代替零!

levels = [0,10]

cs1 = ax0.contourf(p, l,p_difference,levels, cmap=cmap, vmin=vmin, vmax=vmax)
cs = ax0.contourf(p, l, p_difference, cmap=cmap, vmin=vmin, vmax=vmax)

ax0.clabel(cs1, fmt='%2.1d', colors='k', fontsize=12)  # contour line labels

【问题讨论】:

    标签: python python-3.x matplotlib contour


    【解决方案1】:

    clabel()fmt 参数可以是一个将级别映射到字符串的字典。这样的字符串可以使用所用字体中可用的任何 unicode 字符。这是一个例子:

    from matplotlib import pyplot as plt
    import numpy as np
    
    p = np.linspace(0, 10, 400)
    l = np.linspace(0, 10, 400)
    pp, ll = np.meshgrid(p, l)
    
    p_difference = np.sin(pp + 0.06 * np.random.randn(400, 1).cumsum(axis=0)) \
                   * np.cos(ll + 0.06 * np.random.randn(1, 400).cumsum(axis=1)) * 6 + 5
    
    levels = [0, 10]
    
    vmin = -1
    vmax = 11
    cs1 = plt.contour(p, l, p_difference, levels=levels, cmap='seismic', vmin=vmin, vmax=vmax)
    cs = plt.contourf(p, l, p_difference, levels=np.arange(-1, 12, 1), cmap='seismic', vmin=vmin, vmax=vmax)
    
    fmt = {0: '☆', 10: '★'}
    plt.clabel(cs1, fmt=fmt, colors='w', fontsize=20)
    plt.colorbar(cs)
    plt.show()
    

    【讨论】:

    • 好漂亮的身材! ^__^ 感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 2012-07-15
    • 2022-01-24
    • 2016-01-12
    • 1970-01-01
    • 2010-09-27
    • 1970-01-01
    • 2015-08-17
    相关资源
    最近更新 更多