tam*_*gal 11 python matplotlib
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.)
Run Code Online (Sandbox Code Playgroud)
小智 11
leg = plt.legend()
for lh in leg.legendHandles:
lh.set_alpha(1)
Run Code Online (Sandbox Code Playgroud)
归功于https://izziswift.com/set-legend-symbol-opacity-with-matplotlib/
对于大小,您可以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)
Run Code Online (Sandbox Code Playgroud)
如果您命名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('.')
Run Code Online (Sandbox Code Playgroud)
请注意,您还必须再次设置标记.我建议将它设置为.而不是,在这里,以使其更加明显

| 归档时间: |
|
| 查看次数: |
4212 次 |
| 最近记录: |