如何在图中将轮廓标签作为传奇

发布于 2025-02-12 02:24:53 字数 962 浏览 2 评论 0 原文

我正在绘制轮廓,使用此链接中的示例(下面的代码和输出图) https://www.geeksforgeeks.org/matplotlib-pyplot-contour-in-in-python/

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib
   
delta = 0.15
x = np.arange(1.5, 2.5, delta)
y = np.arange(1.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z = (np.exp(X - Y))
  
  
CS1 = plt.contour(X, Y, Z)
   
fmt = {}
strs = ['1', '2', '3', '4', '5', '6', '7']
for l, s in zip(CS1.levels, strs):
    fmt[l] = s
plt.clabel(CS1, CS1.levels, inline = True,
           fmt = fmt, fontsize = 10)
  
plt.title('matplotlib.pyplot.contour() Example')
plt.show()

输出是:

我希望每个轮廓的标签作为角落的传奇,而不是沿轮廓线。有办法吗?

I am plotting contours, using an example from this link (code and output plot below) https://www.geeksforgeeks.org/matplotlib-pyplot-contour-in-python/

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib
   
delta = 0.15
x = np.arange(1.5, 2.5, delta)
y = np.arange(1.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z = (np.exp(X - Y))
  
  
CS1 = plt.contour(X, Y, Z)
   
fmt = {}
strs = ['1', '2', '3', '4', '5', '6', '7']
for l, s in zip(CS1.levels, strs):
    fmt[l] = s
plt.clabel(CS1, CS1.levels, inline = True,
           fmt = fmt, fontsize = 10)
  
plt.title('matplotlib.pyplot.contour() Example')
plt.show()

The output is:
enter image description here

I want the labels for each contour as a legend in the corner, not along the contour lines. Is there a way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜深人未静 2025-02-19 02:24:53

尝试一下:

delta = 0.15
x = np.arange(1.5, 2.5, delta)
y = np.arange(1.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z = (np.exp(X - Y))
  
CS1 = plt.contour(X, Y, Z)
   
nm, lbl = CS1.legend_elements()
lbl_ = [i for i in range(1, len(lbl))]
plt.legend(nm, lbl_) 

plt.title('matplotlib.pyplot.contour() Example')
plt.show()

“在此处输入图像描述”

Try this:

delta = 0.15
x = np.arange(1.5, 2.5, delta)
y = np.arange(1.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z = (np.exp(X - Y))
  
CS1 = plt.contour(X, Y, Z)
   
nm, lbl = CS1.legend_elements()
lbl_ = [i for i in range(1, len(lbl))]
plt.legend(nm, lbl_) 

plt.title('matplotlib.pyplot.contour() Example')
plt.show()

enter image description here

琉璃梦幻 2025-02-19 02:24:53
  1. 这篇文章在这里几种为您的轮廓创建传奇的方法。具体来说,您可以用来
for i in range(len(cl)): 
  CS1.collections[i].set_label(cl[i].get_text())

为轮廓创建自定义图例。

  1. 要从图内的轮廓上删除标签,您可以使用发布的解决方案 a>。它在于迭代用 label.remove()

以下是最终代码的样子:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib
   
delta = 0.15
x = np.arange(1.5, 2.5, delta)
y = np.arange(1.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z = (np.exp(X - Y))
  
  
CS1 = plt.contour(X, Y, Z)

fmt = {}

strs = ['1', '2', '3', '4', '5', '6', '7']
for l, s in zip(CS1.levels, strs):
    fmt[l] = s

cl=plt.clabel(CS1, CS1.levels,fmt=fmt, inline = False, fontsize = 10)

plt.title('matplotlib.pyplot.contour() Example')
for i in range(len(cl)): 
  CS1.collections[i].set_label(cl[i].get_text())


for label in cl:
  label.remove()

plt.legend()

<

a a href =“ https://i.sstatic.net/hfzkkkw 。

  1. This post here shows a couple of ways to create a legend for your contours. Specifically, you can use
for i in range(len(cl)): 
  CS1.collections[i].set_label(cl[i].get_text())

to create a custom legend for your contours.

  1. To remove the labels from the contours inside the plot, you can use the solution posted here. It consists in iteratively removing the labels with label.remove()

Below is what the final code looks like:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib
   
delta = 0.15
x = np.arange(1.5, 2.5, delta)
y = np.arange(1.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z = (np.exp(X - Y))
  
  
CS1 = plt.contour(X, Y, Z)

fmt = {}

strs = ['1', '2', '3', '4', '5', '6', '7']
for l, s in zip(CS1.levels, strs):
    fmt[l] = s

cl=plt.clabel(CS1, CS1.levels,fmt=fmt, inline = False, fontsize = 10)

plt.title('matplotlib.pyplot.contour() Example')
for i in range(len(cl)): 
  CS1.collections[i].set_label(cl[i].get_text())


for label in cl:
  label.remove()

plt.legend()

And the output gives:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文