如何使用代理艺术家为 Matplotlib 中的 contourf 图创建图例
How to create legend with proxy artist for contourf plot in Matplotlib
我正在尝试创建一个图形,其中我将多个等高线图叠加在单个图像上。所以我想为每个图都有颜色条,以及一个说明每个轮廓代表什么的图例。但是,Matplotlib 不允许我为等高线图创建单独的图例。简单例子:
import matplotlib as mpl
import matplotlib.pyplot as plt
import cartopy
import cartopy.crs as ccrs
import numpy as np
def create_contour(i,j):
colors = ["red","green","blue"]
hatches = ['-','+','x','//','*']
fig = plt.figure()
ax = plt.axes(projection=ccrs.PlateCarree())
ax.set_extent((-15.0,15.0,-15.0,15.0))
delta = 0.25
x = np.arange(-3.0,3.0,delta)
y = np.arange(-2.0,2.0,delta)
X, Y = np.meshgrid(x, y)
data = np.full(np.shape(X), 1.0)
plot = ax.contourf(X,Y,data, levels = [float(i),float(i+1)], hatch=[hatches[j]], colors = colors[i], label="label")
plt.legend(handles=[plot], labels=["label"])
plt.savefig("figure_"+str(i)+".png")
create_contour(1,3)
当我 运行 执行此操作时,我收到以下消息:
UserWarning: Legend does not support
(matplotlib.contour.QuadContourSet object at 0x7fa69df7cac8)
instances. A proxy artist may be used instead. See:
http://matplotlib.org/users/legend_guide.html#creating-artists-specifically-for-adding-to-the-legend-aka-proxy-artists
"aka-proxy-artists".format(orig_handle)
但据我所知,我尽可能地遵循这些指示,唯一的区别是它们在示例中不使用 contourf。
如有任何帮助,我们将不胜感激。
对您问题的评论看起来已经解决了问题(通过制作自定义补丁并将其传递给图例)。还有一个例子是我多年前在 matplotlib 文档中添加的,用于做类似的事情(大约在同一时间我向 matplotlib 添加轮廓影线):https://matplotlib.org/examples/pylab_examples/contourf_hatching.html#pylab-examples-contourf-hatching
这是一个合理的要求,甚至在轮廓集上有一个方法可以开箱即用地为您提供图例代理:ContourSet.legend_elements。
因此您的示例可能类似于:
%matplotlib inline
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import numpy as np
fig = plt.figure(figsize=(10, 10))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines('10m')
y = np.linspace(40.0, 60.0, 30)
x = np.linspace(-10.0, 10.0, 40)
X, Y = np.meshgrid(x, y)
data = 2*np.cos(2*X**2/Y) - np.sin(Y**X)
cs = ax.contourf(X, Y, data, 3,
hatches=['//','+','x','o'],
alpha=0.5)
artists, labels = cs.legend_elements()
plt.legend(handles=artists, labels=labels)
plt.show()
我正在尝试创建一个图形,其中我将多个等高线图叠加在单个图像上。所以我想为每个图都有颜色条,以及一个说明每个轮廓代表什么的图例。但是,Matplotlib 不允许我为等高线图创建单独的图例。简单例子:
import matplotlib as mpl
import matplotlib.pyplot as plt
import cartopy
import cartopy.crs as ccrs
import numpy as np
def create_contour(i,j):
colors = ["red","green","blue"]
hatches = ['-','+','x','//','*']
fig = plt.figure()
ax = plt.axes(projection=ccrs.PlateCarree())
ax.set_extent((-15.0,15.0,-15.0,15.0))
delta = 0.25
x = np.arange(-3.0,3.0,delta)
y = np.arange(-2.0,2.0,delta)
X, Y = np.meshgrid(x, y)
data = np.full(np.shape(X), 1.0)
plot = ax.contourf(X,Y,data, levels = [float(i),float(i+1)], hatch=[hatches[j]], colors = colors[i], label="label")
plt.legend(handles=[plot], labels=["label"])
plt.savefig("figure_"+str(i)+".png")
create_contour(1,3)
当我 运行 执行此操作时,我收到以下消息:
UserWarning: Legend does not support (matplotlib.contour.QuadContourSet object at 0x7fa69df7cac8) instances. A proxy artist may be used instead. See: http://matplotlib.org/users/legend_guide.html#creating-artists-specifically-for-adding-to-the-legend-aka-proxy-artists "aka-proxy-artists".format(orig_handle)
但据我所知,我尽可能地遵循这些指示,唯一的区别是它们在示例中不使用 contourf。
如有任何帮助,我们将不胜感激。
对您问题的评论看起来已经解决了问题(通过制作自定义补丁并将其传递给图例)。还有一个例子是我多年前在 matplotlib 文档中添加的,用于做类似的事情(大约在同一时间我向 matplotlib 添加轮廓影线):https://matplotlib.org/examples/pylab_examples/contourf_hatching.html#pylab-examples-contourf-hatching
这是一个合理的要求,甚至在轮廓集上有一个方法可以开箱即用地为您提供图例代理:ContourSet.legend_elements。
因此您的示例可能类似于:
%matplotlib inline
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import numpy as np
fig = plt.figure(figsize=(10, 10))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines('10m')
y = np.linspace(40.0, 60.0, 30)
x = np.linspace(-10.0, 10.0, 40)
X, Y = np.meshgrid(x, y)
data = 2*np.cos(2*X**2/Y) - np.sin(Y**X)
cs = ax.contourf(X, Y, data, 3,
hatches=['//','+','x','o'],
alpha=0.5)
artists, labels = cs.legend_elements()
plt.legend(handles=artists, labels=labels)
plt.show()