matplotlib 中同一图中具有不同填充特征的多个图

问题描述 投票:0回答:1

我想在同一个图中绘制具有不同填充特征的多个轮廓图。

from matplotlib import pyplot as plt
import matplotlib.colors as mcolors
import numpy as np

x=np.arange(0,10,0.1)
y=np.arange(0,10,0.1)

xx, yy = np.meshgrid(x, y)

Z=np.where(xx>yy, 1, 0)

hatch_color=mcolors.to_rgba("green")
plt.rcParams['hatch.linewidth'] = 2
plt.rcParams['hatch.color'] = hatch_color
plt.contourf(xx, yy, Z,levels=[0.5, 1],hatches=["\\"],colors='red')
plt.show()
plt.clf()

Z=np.where(xx<yy, 1, 0)

hatch_color=mcolors.to_rgba("blue")
plt.rcParams['hatch.linewidth'] = 10
plt.rcParams['hatch.color'] = hatch_color
plt.contourf(xx, yy, Z,levels=[0.5, 1],hatches=["\\"],colors='red')
plt.show()
plt.clf()

上面的代码产生了两个图

第一个情节
第二个情节

我使用 plt.rcParams 来设置填充属性,例如颜色或线宽。然而,这些变化是全球性的,涉及图中的所有细节。

我想在一张图中生成这两个具有不同凹痕的轮廓图。我怎样才能做到这一点?我希望人们可以在本地更改填充属性,而不是使用 plt.rcParams ,但我不知道如何操作。

matplotlib contourf
1个回答
0
投票

我认为你不想要中间的

plt.show()
plt.clf()
,它们基本上删除了你已经绘制的内容(有关更多信息,请参阅“注释”部分here)。

没有它们,至少不同的颜色看起来可以正确显示,但线宽仍然相同。好像只使用了渲染时

'hatch.linewidth'
的值...

from matplotlib import pyplot as plt
import matplotlib.colors as mcolors
import numpy as np

x=np.arange(0,10,0.1)
y=np.arange(0,10,0.1)

xx, yy = np.meshgrid(x, y)

Z=np.where(xx>yy, 1, 0)

hatch_color=mcolors.to_rgba("green")
plt.rcParams['hatch.linewidth'] = 2
plt.rcParams['hatch.color'] = hatch_color
plt.contourf(xx, yy, Z,levels=[0.5, 1],hatches=["\\"],colors='red')

Z=np.where(xx<yy, 1, 0)

hatch_color=mcolors.to_rgba("blue")
plt.rcParams['hatch.linewidth'] = 10
plt.rcParams['hatch.color'] = hatch_color
plt.contourf(xx, yy, Z,levels=[0.5, 1],hatches=["\\"],colors='red')

plt.savefig("two_hatch_hydra.png")
plt.show()

具有两种不同填充颜色的 matplotlibcontourf

© www.soinside.com 2019 - 2024. All rights reserved.