【问题标题】:Distorted plot from using clabel with contourf in Python在 Python 中使用 clabel 和 contourf 导致的扭曲图
【发布时间】:2017-11-15 19:28:38
【问题描述】:

我有一个来自 contourf 的图像,如下所示:

它是由这段代码sn-p制成的:

fig = plt.figure()
Z = uavg_lon[:,1,:].transpose()
plt.title('ERA-interim 100hpa U average in [-20,20]',fontsize=16)
c0 = plt.contourf( lon , range(4*12) , Z , cmap = cm.bwr , levels = 
range(-25,30,5))
#plt.clabel(c0, fmt='%2d', inline=True)
plt.xlim([-180,180])
plt.xlabel('longitude',fontsize=14)
plt.xticks(np.arange(-180,180+30,30),fontsize=14)
plt.yticks(monthlabellocs,monthlabels,fontsize=14)
plt.ylim([0,48])
plt.xlim([-180,180])
fig.set_size_inches(8,10)
fn = 'hovmol_100u.pdf'
fig.savefig(fn , format='pdf' , bbox_inches='tight')

当我尝试使用 clabel 添加轮廓标签时(通过取消注释行),我得到了:

我在网上找不到任何描述此问题的示例。发生了什么,我该如何解决?

【问题讨论】:

  • 这肯定是一个有趣的问题。但是,为了了解发生了什么,我们需要该问题的minimal reproducible example。 IE。在代码中生成一些数据,以便其他人可以重现。另外,请说明正在使用的版本。

标签: python matplotlib


【解决方案1】:

我可能错了,但我认为在由 contourf() 返回的对象上调用 clabel() 并不是最好的主意。 Documentation

在cs中为线条轮廓添加标签,其中cs是contour()返回的ContourSet对象。

您可以尝试在您想要的特定轮廓之后调用 contourf(),然后在由 contour() 返回的对象上调用 clabel() 吗?

【讨论】:

    【解决方案2】:

    你可以替换

    plt.clabel(c0, fmt='%2d', inline=True)
    

    plt.clabel(c0, fmt='%2d', inline=False)
    

    【讨论】:

      猜你喜欢
      • 2012-05-31
      • 2023-04-03
      • 2013-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多