简体   繁体   English

定位颜色条-Matplotlib

[英]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): 但是,我想重新设置颜色条的位置,使其具有以下内容(由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([....]) ). 做到这一点的“最简单”方法可能是手工布置要用于颜色条的轴(通过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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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