gpt4 book ai didi

python - 如何组合多个等高线图?

转载 作者:太空宇宙 更新时间:2023-11-03 20:30:46 24 4
gpt4 key购买 nike

我正在尝试将多个轮廓图合并为一个,我设法使用 alpha=0.5 来做到这一点,但填充元素意味着并非所有图都是可见的。

我的代码是:

fig,ax = plt.subplots(figsize = (20,16))

b=ax.contourf(dfE,4,cmap='Greens', alpha=0.5, linewidths=(3,))
cbax2 = fig.add_axes([0.91, 0.41, 0.02, 0.2])
cb2 = plt.colorbar(b, cax=cbax2)

d = ax.contourf(dfH,4,cmap='Reds', linewidths=(3,), alpha=0.5)
cbax4 = fig.add_axes([0.91, 0.19, 0.02, 0.2])
cb4 = plt.colorbar(d, cax=cbax4)

f = ax.contourf(dfS,3,cmap='Wistia', linewidths=(3,), alpha=0.5)
cbax6 = fig.add_axes([0.97, 0.41, 0.02, 0.2])
cb6 = plt.colorbar(f, cax=cbax6)

g = ax.contourf(dfT,4,cmap='Purples', linewidths=(2,), alpha=0.5)
cbax7 = fig.add_axes([0.97, 0.63, 0.02, 0.2])
cb7 = plt.colorbar(g, cax=cbax7)

h = ax.contourf(dfC,4,cmap='Blues', linewidths=(3,), alpha=0.5)
cbax8 = fig.add_axes([0.91, 0.63, 0.02, 0.2])
cb8 = plt.colorbar(h, cax=cbax8)

ax.set_ylim([0, 16])
ax.set_xlim([0, 16])

ax.set_xlabel('Principal Component 1', size = 25)
ax.set_ylabel('Principal Component 2', size = 25)

cb4.set_label('Helix (H)',size = 15)
cb2.set_label('Sheet (E)',size = 15)
cb8.set_label('Other (C)',size = 15)
cb7.set_label('H-Bonded Turn (T)',size = 15)
cb6.set_label('Bend (S)',size = 15)

ax.set_title('8-State PCA Analysis: 108 Dimensions', size = 30)
plt.show()

我的情节是: plot

最佳答案

如果您想将它们全部放在同一个图表上,那么您应该尝试设置等高线级别而不显示小值。您还可以降低不太重要的数据的 Alpha。

下面是一个示例,其中我设置了等高线级别并使用 extend='max',以便不显示最低等高线级别以下的值,但上面的值会着色为最大值:

import numpy as np
from matplotlib import pyplot as plt

#Create the grid
x = np.arange(-20,21,1)
y=x
X,Y = np.meshgrid(x,y)

#Create the functions to plot
Z1 = 1000-np.abs(X**2+(Y+4)**3)
Z2 = 1000-np.abs((X+4)**3+Y**3)
Z3 = 1000-np.abs((Y+2)**3+(X-3)**3)
Z4 = 1000-np.abs(X**2+Y**3-1)



fig = plt.figure(figsize=(8,8))

ax = plt.subplot(111)

#Plot using contourf making sure you set your contour levels and don't show the lowest levels
b=ax.contourf(X,Y,Z1/np.nanmax(Z1),[.25,.5,.75],alpha=.5,cmap='Greens',linewidths=3,extend='max')
d=ax.contourf(X,Y,Z2/np.nanmax(Z2),[.25,.5,.75],alpha=.5,cmap='Reds',linewidths=3,extend='max')
f=ax.contourf(X,Y,Z3/np.nanmax(Z3),[.25,.5,.75],alpha=.5,cmap='Blues',linewidths=3,extend='max')
g=ax.contourf(X,Y,Z4/np.nanmax(Z4),[.25,.5,.75],alpha=.5,cmap='Purples',linewidths=3,extend='max')

plt.show()

enter image description here

考虑使用等高线图

正如我在评论中提到的,您应该考虑使用等高线图来表示更改线条颜色的数据。您还可以更改linestyleslinewidths以突出显示您试图在绘图中传达的消息。您还可以结合使用 contour()contourf() 绘图来更好地突出显示数据。

import numpy as np
from matplotlib import pyplot as plt

#Create the grid
x = np.arange(-20,21,1)
y=x
X,Y = np.meshgrid(x,y)

#Create the functions to plot
Z1 = 1000-np.abs(X**2+(Y+4)**3)
Z2 = 1000-np.abs((X+4)**3+Y**3)
Z3 = 1000-np.abs((Y+2)**3+(X-3)**3)
Z4 = 1000-np.abs(X**2+Y**3-1)



fig = plt.figure(figsize=(8,8))

ax = plt.subplot(111)

#Plot using contour instead of contourf and change the colors
b=ax.contour(X,Y,Z1/np.nanmax(Z1),[.25,.5,.75],alpha=.8,colors='Green',linewidths=3)
d=ax.contour(X,Y,Z2/np.nanmax(Z2),[.25,.5,.75],alpha=.8,colors='Red',linewidths=3)
f=ax.contour(X,Y,Z3/np.nanmax(Z3),[.25,.5,.75],alpha=.8,colors='Blue',linewidths=3)
g=ax.contour(X,Y,Z4/np.nanmax(Z4),[.25,.5,.75],alpha=.8,colors='Purple',linewidths=3,linestyles='dashed')

plt.show()

enter image description here

关于python - 如何组合多个等高线图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57525830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com