首页 > 解决方案 > How to keep x and y-axis equal with a colorbar?

问题描述

I have been able to use scatter to plot x, y, and z, with the latter being the color-coded variables defined by a colorbar. However, whenever I introduce the colorbar it affects the aspect-ratio of the axes in the sense that it "steals space" from the x-axis. Even if I define the figsize.

cm = plt.cm.get_cmap('viridis')

fig, ax = plt.subplots(figsize=[6, 6])

for dat_x, dat_b, dat_c in zip([data_x1, data_x2, data_x3], [data_y1, data_y2, data_y3], [data_z1, data_z2, data_z3]):
    for dx, dy, dz in zip(dat_x, dat_b, dat_c):
        sc = ax.scatter(dx, dy, dz, cmap=cm, norm=mplcol.LogNorm(vmin=1, vmax=200, s=100)

sc = plt.colorbar(sc)

plt.show()

How can I keep the length of the x-axis and the y-axis equal while simultaneously prevent the colorbar from "stealing space" from the x-axis?

标签: pythonmatplotlib

解决方案


推荐阅读