编程语言
首页 > 编程语言> > 用户在python中定义的图例

用户在python中定义的图例

作者:互联网

我有这个图,其中曲线之间的某些区域按定义填充.有没有办法将它们包含在传奇中?特别是那些填充区域重叠的地方,以及出现新的和不同的颜色.

或者无论曲线的数据如何,都有可能定义任意图例?
enter image description here

解决方法:

使用fill_bettween绘制数据将自动包含图例中的填充区域.

要包括两个数据集重叠的区域,可以将两个数据集中的图例句柄组合到一个图例句柄中.

正如评论中所指出的,您还可以使用代理定义任意图例句柄.

最后,无论图表中绘制的数据如何,您都可以准确定义要在图例中显示的句柄和标签.

请参阅下面的MWE,其中说明了上述要点:

import matplotlib.pyplot as plt
import numpy as np

plt.close('all')

# Gererate some datas:
x = np.random.rand(50)
y = np.arange(len(x))

# Plot data:
fig, ax = plt.subplots(figsize=(11, 4))
fillA = ax.fill_between(y, x-0.25, 0.5, color='darkolivegreen', alpha=0.65, lw=0)
fillB = ax.fill_between(y, x, 0.5, color='indianred', alpha=0.75, lw=0)

linec, = ax.plot(y, np.zeros(len(y))+0.5, color='blue', lw=1.5)
linea, = ax.plot(y, x, color='orange', lw=1.5)
lineb, = ax.plot(y, x-0.25, color='black', lw=1.5)

# Define an arbitrary legend handle with a proxy:
rec1 = plt.Rectangle((0, 0), 1, 1, fc='blue', lw=0, alpha=0.25)

# Generate the legend:
handles = [linea, lineb, linec, fillA, fillB, (fillA, fillB),
           rec1, (fillA, fillB, rec1)]
labels = ['a', 'b', 'c', 'A', 'B', 'A+B', 'C', 'A+B+C']
ax.legend(handles, labels, loc=2, ncol=4)

ax.axis(ymin=-1, ymax=2)

plt.show()

enter image description here

标签:python,matplotlib,legend-properties
来源: https://codeday.me/bug/20190627/1309594.html