我怎么能有同样高度的色条呢?

2024-04-18 14:26:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我想打印一些数据,但颜色栏高于绘图。我怎样才能解决这个问题

import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate

fig, ax = plt.subplots()
# Generate data:
x = np.linspace(1, 1, 10)
y = np.linspace(1,100, 10)

for i in range(10, 100, 10):
    x = append(x, np.linspace(i, i, 10))
    y = append(y, np.linspace(1, 100, 10))

z = numpy.random.uniform(-5, 5, size=100)

# Set up a regular grid of interpolation points
xi, yi = np.linspace(x.min(), x.max(), 100), np.linspace(y.min(), y.max(), 100)
xi, yi = np.meshgrid(xi, yi)

# Interpolate
rbf = scipy.interpolate.Rbf(x, y, z, function='linear')
zi = rbf(xi, yi)

s = ax.imshow(zi, vmin=z.min(), vmax=z.max(), origin='lower',
           extent=[x.min(), x.max(), y.min(), y.max()])
plt.xlim([0,200])

#s = ax.scatter(x, y, c=z, marker = 's')
plt.colorbar(mappable=s, ax=ax)
plt.show()

enter image description here


Tags: importnumpyasnppltscipyaxmin
1条回答
网友
1楼 · 发布于 2024-04-18 14:26:59

最简单的解决方案(以您为例)是删除该行

plt.xlim([0,200])

但既然你把它放在那里,我想你真的想/需要它。因此,您必须手动调整颜色条的高度:

cb = plt.colorbar(mappable=s, ax=ax)
plt.draw()
posax =  ax.get_position()
poscb = cb.ax.get_position()
cb.ax.set_position([poscb.x0, posax.y0, poscb.width, posax.height])

使用@MaxNoe建议的shrink参数colorbar也可以达到目的。但你必须摆弄来获得正确的价值

相关问题 更多 >