如何从图例中移除标记中的线条

huangapple go评论99阅读模式
英文:

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[&#39;legend.handlelength&#39;] = 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[&#39;legend.handlelength&#39;] = 0


x = np.linspace(-np.pi/2, np.pi/2, 31)
y = np.cos(x)**3

# 1) remove points where y &gt; 0.7
x2 = x[y &lt;= 0.7]
y2 = y[y &lt;= 0.7]

# 2) mask points where y &gt; 0.7
y3 = np.ma.masked_where(y &gt; 0.7, y)

# 3) set to NaN where y &gt; 0.7
y4 = y.copy()
y4[y3 &gt; 0.7] = np.nan

plt.plot(x*0.1, y, &#39;o-&#39;, color=&#39;lightgrey&#39;, label=&#39;No mask&#39;)
plt.plot(x2*0.4, y2, &#39;o-&#39;, label=&#39;Points removed&#39;)
plt.plot(x*0.7, y3, &#39;o-&#39;, label=&#39;Masked values&#39;)
plt.plot(x*1.0, y4, &#39;o-&#39;, label=&#39;NaN values&#39;)
plt.legend()
plt.title(&#39;Masked and NaN data&#39;)
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({&quot;legend.handlelength&quot;: 0,}):

EDIT: the other answer has a better solution for per graph legends.

huangapple
  • 本文由 发表于 2023年5月10日 22:50:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定