如何在Matplotlib等高线图中减少小数位数

6
我想把这个图表中的小数位从3位减少到1位。我找不到任何方法来做到这一点。我试过将列表中的级别定义为整数,但需要使用浮点数,并且似乎自动出现3个小数位。求助。
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(1, 1, 1, aspect='equal')
plt.yticks(fontsize=16)
plt.xticks(fontsize=16)    
modelmap = flopy.plot.ModelMap(model=mf)
quadmesh = modelmap.plot_ibound()
linecollection = modelmap.plot_grid(alpha=0.01)
#set up array to show wetland location
if alt_coords!=None: #plot the wetland if altcoords provided
    wl_arr=np.zeros([mf.nrow,mf.ncol])
    for coord in alt_coords:
        wl_arr[coord[1],coord[2]]=1.0
    quadmesh=modelmap.plot_array(wl_arr, color='w', alpha=0.1)
riv = modelmap.plot_bc('RIV', color='b', plotAll=True)
quadmesh = modelmap.plot_bc('WEL', kper=1, plotAll=True)
levels=np.arange(40, 100, 3)
contour_set = modelmap.contour_array(hds, levels, colors='b')
plt.clabel(contour_set, inline=1, fontsize=14)

1
matplotlib.pyplot.clabel 接受一个 fmt 参数。该参数允许您设置标签格式。因此,您可以指定此参数并查看其是否有效:plt.clabel(contour_set, inline=1, fmt='%1.1f, fontsize=14) - amanb
太好了!我已经添加了一个答案,以供遇到类似问题的其他人参考。你可以接受/点赞它。 - amanb
1个回答

4

matplotlib.pyplot.clabel函数接受一个fmt参数,该参数用于设置标签格式。您可以指定此参数并查看其效果:plt.clabel(contour_set, inline=1, fmt='%1.1f', fontsize=14)


使用fmt格式字符串的语法是什么?根据您提供的示例,我可以部分猜到,但您提供的文档只说了这么一句:"标签的格式字符串。默认为 '%1.3f'"。特别是,如果我想要逗号分隔符怎么办? - Jess Riedel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接