【问题标题】:Handle legend markers independently独立处理图例标记
【发布时间】:2020-07-17 15:42:18
【问题描述】:

我的绘图有两种类型的线:实线和虚线。实线和虚线表示不同条件下的相同数量,颜色相同。

现在,我想添加一个ax.text() 实线的标记来表示实线表示该条件下的数量,另一个带有虚线标记的ax.text() 表示另一个健康)状况。不知何故,我的意思是这样的:

ax.text(0.9, 0.5, solid_marker + 'condition 1')
ax.text(0.9, 0.4, dashed_marker + 'condition 2')

嗯,像这张照片:

我想做的例子:

有人知道怎么做吗?是否可以在图中随机文本中使用标记的符号?

谢谢!

【问题讨论】:

    标签: python matplotlib text legend


    【解决方案1】:

    您的图例规格不标准。您需要手动创建它。这是一个可运行的代码,可以生成您需要的内容。

    #import matplotlib.patches as mpatches
    import matplotlib.lines as mlines
    import matplotlib.pyplot as plt
    
    # Need to create legends manually
    
    # First legend
    #red_patch = mpatches.Patch(color='red', label='The red data')
    black_line = mlines.Line2D([], [], color='black', linewidth=1.5, label=r'$Z_n$')
    green_line = mlines.Line2D([], [], color='green', linewidth=1.5, label=r'$Z_p$')
    red_line = mlines.Line2D([], [], color='red', linewidth=1.5, label=r'$Z_\pi$')
    
    # Second legend
    line_solid = mlines.Line2D([], [], color='black', linestyle='-', linewidth=1.5, label=r'$n_\beta = n_o$')
    line_dashed = mlines.Line2D([], [], color='black', linestyle='--', linewidth=1.5, label=r'$n_\beta = n_o/2$')
    
    first_legend = plt.legend(handles=[black_line, green_line, red_line], loc=1)
    ax = plt.gca().add_artist(first_legend)
    second_legend = plt.legend(handles=[line_solid, line_dashed], loc='best', \
                               bbox_to_anchor=(0.5, 0.20, 0.5, 0.5)) #best upper-right
    second_legend.set_frame_on(False)
    
    plt.show()
    

    输出图:

    【讨论】:

      【解决方案2】:

      感谢@swatchai。我修改了他的答案以适合我的代码。所以我得到了:

      import numpy as np
      import matplotlib.lines as mlines
      import matplotlib.pyplot as plt
      
      # my fig and my axes
      fig, ax1 = plt.subplots(figsize=(6, 6))
      
      # my plots
      ax1.plot(temp, zn,  color=cycle[0], label=r'$z_n$')
      ax1.plot(temp, zp,  color=cycle[1], label=r'$z_p$')
      ax1.plot(temp, zpi, color=cycle[2], label=r'$z_\pi$')
      
      # the other plots
      ax1.plot(temp, zn2,  color=cycle[0], linestyle='--')
      ax1.plot(temp, zp2,  color=cycle[1], linestyle='--')
      ax1.plot(temp, zpi2, color=cycle[2], linestyle='--')
      
      # Second legend 'imaginary' lines
      line_solid = mlines.Line2D([], [], color='black', linestyle='-',    \
              linewidth=1.5, label=r'$n_b = n_0$')
      line_dashed = mlines.Line2D([], [], color='black', linestyle='--',  \
              linewidth=1.5, label=r'$n_b = n_0/2$')
      
      # original legend
      leg1 = ax1.legend()
      
      # set second legend (will remove first one)
      leg2 = ax1.legend(handles=[line_solid, line_dashed], loc='best', \
                  bbox_to_anchor=(0.5, 0.20, 0.5, 0.6))
      
      leg2.set_frame_on(False)    # remove legend frame
      # manually add the first legend back
      ax1.add_artist(leg1)
      

      输出(注意上面的代码是不可运行的,而且我似乎还不能嵌入图片):

      result

      我实际上是想避免必须通过创建新的假想线来为它们分配图例这一步骤。我很想知道是否也可以在文本中使用标记。不过好吧,这至少解决了我的问题。

      谢谢!

      【讨论】:

        猜你喜欢
        • 2019-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-28
        • 1970-01-01
        • 1970-01-01
        • 2016-02-07
        • 2015-11-21
        相关资源
        最近更新 更多