英文:
How to remove lines from markers in the legend
问题
我已经绘制了一些数据,用线连接的误差条,你可以从这个图片中看到。
我想在图例中只显示标记(不想在标记中间显示线条)。我不想从图表中移除线条,只想在图例中移除。
我尝试了下面这段代码,来自这个帖子:
handles, labels = ax.get_legend_handles_labels()
for h in handles: h.set_linestyle("")
ax.legend(handles, labels)
但它也从图表中移除了线条。
有没有办法做到这一点?
英文:
I’ve plotted some data as error bars linked by lines, as you can see from the
picture.
I would like to show only the markers in the legend (I don't want to show the lines in the middle of the markers). I don't want to remove the lines from the diagram, only in the legend.
I tried the code below from this thread:
handles, labels = ax.get_legend_handles_labels()
for h in handles: h.set_linestyle("")
ax.legend(handles, labels)
It removed the lines also from the diagram.
Is there a way to do it?
答案1
得分: 1
以下是已翻译的内容:
更新中...
有一个特定的选项来执行此操作...
import numpy as np
import matplotlib.pyplot as plt
# 虚拟数据
x = np.arange(10)
y1 = 2*x
y2 = 3*x
fig, ax = plt.subplots()
line1, = ax.plot(x, y1, c='blue', marker='x')
line2, = ax.plot(x, y2, c='red', marker='o')
ax.legend([line1, line2], ['数据1', '数据2'], handlelength=0.)
plt.show()
英文:
Updating...
There is a specific option to do this...
import numpy as np
import matplotlib.pyplot as plt
# dummy data
x = np.arange(10)
y1 = 2*x
y2 = 3*x
fig, ax = plt.subplots()
line1, = ax.plot(x, y1, c='blue', marker='x')
line2, = ax.plot(x, y2, c='red', marker='o')
ax.legend([line1, line2], ['data1', 'data2'], handlelength=0.)
plt.show()
答案2
得分: 0
Sure, here's the translated content:
"不必重复绘制图表,这可能会带来一些额外开销,您可以使用 matplotlib.rcParams['legend.handlelength'] = 0
。这是一个全局参数,这意味着它会影响之后的每个图表。
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
matplotlib.rcParams['legend.handlelength'] = 0
x = np.linspace(-np.pi/2, np.pi/2, 31)
y = np.cos(x)**3
# 1) 移除 y > 0.7 的点
x2 = x[y <= 0.7]
y2 = y[y <= 0.7]
# 2) 掩盖 y > 0.7 的点
y3 = np.ma.masked_where(y > 0.7, y)
# 3) 将 y > 0.7 的点设为 NaN
y4 = y.copy()
y4[y3 > 0.7] = np.nan
plt.plot(x*0.1, y, 'o-', color='lightgrey', label='无掩盖')
plt.plot(x2*0.4, y2, 'o-', label='移除点')
plt.plot(x*0.7, y3, 'o-', label='掩盖值')
plt.plot(x*1.0, y4, 'o-', label='NaN 值')
plt.legend()
plt.title('掩盖和 NaN 数据')
plt.show()
如果您只想用于一个图表,可以将负责图表的代码包装在以下方式中:
with plt.rc_context({"legend.handlelength": 0}):
编辑:其他答案提供了适用于每个图表的更好解决方案。
英文:
Instead of drawing the graph twice, which might come with some overhead, you could use matplotlib.rcParams['legend.handlelength'] = 0
. This is a global parameter, which means it would affect every other graph after the fact.
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
matplotlib.rcParams['legend.handlelength'] = 0
x = np.linspace(-np.pi/2, np.pi/2, 31)
y = np.cos(x)**3
# 1) remove points where y > 0.7
x2 = x[y <= 0.7]
y2 = y[y <= 0.7]
# 2) mask points where y > 0.7
y3 = np.ma.masked_where(y > 0.7, y)
# 3) set to NaN where y > 0.7
y4 = y.copy()
y4[y3 > 0.7] = np.nan
plt.plot(x*0.1, y, 'o-', color='lightgrey', label='No mask')
plt.plot(x2*0.4, y2, 'o-', label='Points removed')
plt.plot(x*0.7, y3, 'o-', label='Masked values')
plt.plot(x*1.0, y4, 'o-', label='NaN values')
plt.legend()
plt.title('Masked and NaN data')
plt.show()
If you want to only use it for one graph, you can wrap the code responsible for the graph with:
with plt.rc_context({"legend.handlelength": 0,}):
EDIT: the other answer has a better solution for per graph legends.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论