我正在尝试创建一个图形,在该图形上将多个轮廓图覆盖在一个图像上。因此,我希望每个图都有颜色条,并有一个说明每个轮廓代表什么的图例。但是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)
运行此命令时,会收到以下消息:
用户警告:图例不支持 (matplotlib.contour.QuadContourSet对象位于0x7fa69df7cac8) 实例。可以代替使用代理艺术家。看到: 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。
任何帮助将不胜感激。
答案 0 :(得分:2)
您问题的注释似乎已经解决了问题(通过制作自定义补丁并将其传递给图例)。还有一个示例,我在多年前将其添加到matplotlib文档中,以执行类似的操作(大约在同一时间,我向matplotlib添加了轮廓阴影):https://matplotlib.org/examples/pylab_examples/contourf_hatching.html#pylab-examples-contourf-hatching
这是一个合理的要求,轮廓集上甚至还提供了一种方法来为您提供开箱即用的图例代理:ContourSet.legend_elements。
因此您的示例可能类似于:
public static bool SendEmail(string password, string from, string to, string cc, string subject, string[] attachedFiles, string body, string host, int port)
{
try
{
MailMessage mail = new MailMessage(from, to);
//foreach (var attachedFile in attachedFiles)
//{
// mail.Attachments.Add(new Attachment(attachedFile.ToString()));
//}
mail.Attachments.Add(new Attachment(@"C:\Users\liaka\Desktop\Jordan\FileMailer\FileMailer\Backlog_07_12_2018.xlsx"));
mail.Attachments.Add(new Attachment(@"C:\Users\liaka\Desktop\Jordan\FileMailer\FileMailer\test.txt"));
mail.Subject = subject;
mail.Body = body;
mail.CC.Add(cc);
var client = new SmtpClient(host, port)
{
Credentials = new NetworkCredential(from, password),
EnableSsl = true
};
client.Send(mail);
return true;
}
catch (Exception ex)
{
return false;
}
}