简体   繁体   中英

Positioning color bars - Matplotlib

I have a plot in which I merge two datas. Given that, I have to show two different color bars, one for each data. I'm currently plotting my datas as follows:

plt.figure()

# Data 1
fig = plt.imshow(data1, interpolation='nearest', cmap='binary', vmin=0, vmax=1)

# Plotting just the nonzero values of data2
data2_x = numpy.nonzero(data2)[0]
data2_y = numpy.nonzero(data2)[1]

pts = plt.scatter(data2_x, data2_y, marker='s', c=data2[data2_x, data2_y])

plt.colorbar(pts)
plt.colorbar(fig, orientation="horizontal")

And this is the plot that I get:

在此处输入图片说明

However, I would like to reposition the color bars to have something like this (made with Photoshop):

在此处输入图片说明

Is that possible?

Thank you in advance.

Probably the 'easiest' way to do this is to lay the axes to be used for the color bars out by hand (via cbax = fig.add_axes([....]) ). You can then pass that axes to the color bar calls:

Something like:

from matplotlib import pyplot as plt
import numpy as np

fig = plt.figure(figsize=(8, 8))
ax = fig.add_axes([.1, .1, .8, .8])
im = ax.imshow(np.random.rand(150, 150), cmap='gray', interpolation='none')
sc = ax.scatter(2 + 146 * np.random.rand(150), 2 + 146 * np.random.rand(150),
                c=np.random.rand(150), cmap='Accent', s=50, lw=0)

ax_cb1 = fig.add_axes([.1, .05, .8, .02])
ax_cb2 = fig.add_axes([.92, .1, .02, .8])

cb1 = fig.colorbar(im, cax=ax_cb1, orientation='horizontal')
cb1.ax.xaxis.set_label_position('top')
cb2 = fig.colorbar(sc, cax=ax_cb2, orientation='vertical')

在此处输入图片说明

您可以使用斧头关键字将颜色栏链接到轴,plt.gca()为您提供当前轴:

plt.colorbar(object1, ax=plt.gca())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM