如何在 Matplotlib 图形上手动添加带有颜色框的图例?
图例基本上是图中的一个区域,用于描述图中存在的元素。 Matplotlib 为此提供了一个名为 legend() 的内置方法。该方法的语法如下:
示例:添加简单图例
Python3
# Import libraries
import matplotlib.pyplot as plt
# Creating plot
plt.plot([1, 2, 3, 4])
plt.title('simple legend example ')
# Creating legend
plt.legend(['simple legend example'])
# Show plot
plt.show()
Python3
# Import libraries
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
# Creating plot
plt.plot([1, 2, 3, 4], color='blue')
plt.title('simple legend example ')
# Creating legend with color box
blue_patch = mpatches.Patch(color='blue', label='blue legend')
plt.legend(handles=[blue_patch])
# Show plot
plt.show()
Python3
# Import libraries
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import scipy.stats
import numpy as np
# Setting limit for x values
x_min = 0.0
x_max = 50.0
# Setting figure size
fig = plt.figure(figsize = (16, 9))
# Creating first dataset
mean = 12.0
std = 3.0
x = np.linspace(x_min, x_max, 100)
y = scipy.stats.norm.pdf(x,mean,std)
# Plotting first dataset
plt.plot(x,y, color='red')
plt.fill_between(x, y, color='#CE5D45', alpha=1.0)
# Creating second dataset
mean = 18.0
std = 6.0
x = np.linspace(x_min, x_max, 100)
y = scipy.stats.norm.pdf(x,mean,std)
# Plotting second dataset
plt.plot(x,y, color='green')
plt.fill_between(x, y, color='#5DC83F', alpha=1.0)
# Creating legend with color box
pop_a = mpatches.Patch(color='#5DC83F', label='Population Dataset 1')
pop_b = mpatches.Patch(color='#CE5D45', label='Population Dataset 2')
plt.legend(handles=[pop_a,pop_b])
# Adding grid to plot
plt.grid()
# Adding X and Y limits
plt.xlim(x_min,x_max)
plt.ylim(0,0.25)
# Adding title
plt.title('simple legend example',fontsize=12)
# Adding labels
plt.xlabel('')
plt.ylabel('Probability Distribution')
# Showing plot
plt.show()
输出:
使用颜色框创建图例
要使用颜色框创建图例,请使用matplotlib.patches模块提供的补丁。补丁只不过是具有面部颜色和边缘颜色的 2D 艺术家。下面是一个简单的例子:
示例 1:
蟒蛇3
# Import libraries
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
# Creating plot
plt.plot([1, 2, 3, 4], color='blue')
plt.title('simple legend example ')
# Creating legend with color box
blue_patch = mpatches.Patch(color='blue', label='blue legend')
plt.legend(handles=[blue_patch])
# Show plot
plt.show()
输出:
示例 2:
蟒蛇3
# Import libraries
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import scipy.stats
import numpy as np
# Setting limit for x values
x_min = 0.0
x_max = 50.0
# Setting figure size
fig = plt.figure(figsize = (16, 9))
# Creating first dataset
mean = 12.0
std = 3.0
x = np.linspace(x_min, x_max, 100)
y = scipy.stats.norm.pdf(x,mean,std)
# Plotting first dataset
plt.plot(x,y, color='red')
plt.fill_between(x, y, color='#CE5D45', alpha=1.0)
# Creating second dataset
mean = 18.0
std = 6.0
x = np.linspace(x_min, x_max, 100)
y = scipy.stats.norm.pdf(x,mean,std)
# Plotting second dataset
plt.plot(x,y, color='green')
plt.fill_between(x, y, color='#5DC83F', alpha=1.0)
# Creating legend with color box
pop_a = mpatches.Patch(color='#5DC83F', label='Population Dataset 1')
pop_b = mpatches.Patch(color='#CE5D45', label='Population Dataset 2')
plt.legend(handles=[pop_a,pop_b])
# Adding grid to plot
plt.grid()
# Adding X and Y limits
plt.xlim(x_min,x_max)
plt.ylim(0,0.25)
# Adding title
plt.title('simple legend example',fontsize=12)
# Adding labels
plt.xlabel('')
plt.ylabel('Probability Distribution')
# Showing plot
plt.show()
输出: