【问题标题】:Setting different colors for markers and lines in matplotlib and show them in legend在 matplotlib 中为标记和线条设置不同的颜色并在图例中显示
【发布时间】:2020-09-23 21:09:39
【问题描述】:

我想创建一个由线条和标记组成的绘图,但两者使用不同的颜色。我的方法是使用两个平均重叠的图:

#!/usr/bin/env python3
import matplotlib.pyplot as plt
fig,ax1 = plt.subplots()
x=[0,1,2,3]
y=[10,20,40,80]
ax1.plot(x, y,color='#FF0000', alpha=0.5, linewidth=2.2,label='Example line',zorder=9)
ax1.scatter(x, y ,marker='o',s=80,color='black',alpha=1,label='Example marker',zorder=10) 

ax1.set_ylim([0,150])
ax1.set_xlim([0,5])
ax1.legend(loc='upper right')
plt.show()
plt.close()

输出:

这里的问题是,自然地,线(----)和标记(X)在图例中是分开显示的。

您是否知道一种在图例中同时显示标记和线条的方法,也就是说,在组合的线条和标记 (---X---) 标签中?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    也许只是在第一个绘图调用中指定您的标记属性...例如

    ax1.plot(x, y,color='#FF0000', linewidth=2.2, label='Example line',
              marker='o', mfc='black', mec='black', ms=10)
    

    【讨论】:

      猜你喜欢
      • 2015-05-01
      • 2017-06-18
      • 2014-03-21
      • 2016-09-15
      • 1970-01-01
      • 2012-06-13
      • 2017-07-15
      • 2021-07-15
      • 2012-02-14
      相关资源
      最近更新 更多