在图例框中放大并设置标记的alpha
值(返回1.0)最方便的方法是什么?我也很满意大彩盒。
import matplotlib.pyplot as plt
import numpy as np
n = 100000
s1 = np.random.normal(0, 0.05, n)
s2 = np.random.normal(0, 0.08, n)
ys = np.linspace(0, 1, n)
plt.plot(s1, ys, ',', label='data1', alpha=0.1)
plt.plot(s2, ys, ',', label='data2', alpha=0.1)
plt.legend(bbox_to_anchor=(1.005, 1), loc=2, borderaxespad=0.)
答案 0 :(得分:7)
对于大小,您可以在对图例的调用中包含关键字markerscale=##
,这将使标记更大(或更小)。
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(1)
fig.clf()
x1,y1 = 4.*randn(10000), randn(10000)
x2,y2 = randn(10000), 4.*randn(10000)
ax = [fig.add_subplot(121+c) for c in range(2)]
ax[0].plot(x1, y1, 'bx',ms=.1,label='blue x')
ax[0].plot(x2, y2, 'r^',ms=.1,label='red ^')
ax[0].legend(loc='best')
ax[1].plot(x1, y1, 'bx',ms=.1,label='blue x')
ax[1].plot(x2, y2, 'r^',ms=.1,label='red ^')
ax[1].legend(loc='best', markerscale=40)
答案 1 :(得分:4)
如果您为legend
命名,则可以迭代其中包含的行。例如:
leg=plt.legend(bbox_to_anchor=(1.005, 1), loc=2, borderaxespad=0.)
for l in leg.get_lines():
l.set_alpha(1)
l.set_marker('.')
答案 2 :(得分:2)
对我而言,诀窍是使用正确的属性:
leg = axs.legend()
for l in leg.get_lines():
l._legmarker.set_markersize(6)
答案 3 :(得分:1)
leg = plt.legend()
for lh in leg.legendHandles:
lh.set_alpha(1)
归功于https://izziswift.com/set-legend-symbol-opacity-with-matplotlib/