Python Matplotlib:在数据坐标中定位颜色条 [英] Python matplotlib: position colorbar in data coordinates

查看:105
本文介绍了Python Matplotlib:在数据坐标中定位颜色条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过在数据坐标中指定位置来在散点图中放置一个颜色条.这是一个在指定图形坐标时如何工作的示例:

I would like to position a colorbar inside a scatter plot by specifying the position in data coordinates. Here is an example of how it works when specifying figure coordinates:

import numpy as np
import matplotlib.pyplot as plt    

#Generate some random data:
a = -2
b = 2
x = (b - a) * np.random.random(50) + a
y = (b - a) * np.random.random(50) + a
z = (b) * np.random.random(50)

#Do a scatter plot
fig = plt.figure()
hdl = plt.scatter(x,y,s=20,c=z,marker='o',vmin=0,vmax=2)
ax = plt.gca()
ax.set_xlim([-2,2])
ax.set_ylim([-2,2])

#Specifying figure coordinates works fine:
fig_coord = [0.2,0.8,0.25,0.05]
cbar_ax = fig.add_axes(fig_coord)

clevs = [0, 1 , 2]
cb1 = plt.colorbar(hdl, cax=cbar_ax, orientation='horizontal', ticks=clevs)

plt.show()

...好吧,由于我缺乏声誉,因此无法在此处包含该地块的图像.但是上面的代码会给您印象....

...Ok, can't include an image of the plot here because I am lacking reputation. But the above code will give you an impression....

现在的问题是,如何将颜色条定位在数据坐标上,使其出现在例如:左侧,底部,宽度,高度:-1.5、1.5、1、0.25

Now the question is, how could I position the colorbar at data coordinates, to appear at e.g.: left, bottom, width, height: -1.5, 1.5, 1, 0.25

我已经做了一些尝试,例如确定图形中的轴位置并将其转换为数据坐标,但是没有成功.

I have experimented with a few things, like determining the axes position within the figure and transforming it to data coordinates but didn't succeed.

非常感谢您的想法或向我指出已经回答过类似的问题!

Many thanks for ideas or pointing me to already answered similar questions!

这就是我所做的事情(虽然不是特别漂亮,但它会有所帮助).谢谢 tcaswell

Here is what I did (not particularly beautiful but it helps). Thanks tcaswell !

#[lower left x, lower left y, upper right x, upper right y] of the desired colorbar:
dat_coord = [-1.5,1.5,-0.5,1.75]
#transform the two points from data coordinates to display coordinates:
tr1 = ax.transData.transform([(dat_coord[0],dat_coord[1]),(dat_coord[2],dat_coord[3])])
#create an inverse transversion from display to figure coordinates:
inv = fig.transFigure.inverted()
tr2 = inv.transform(tr1)
#left, bottom, width, height are obtained like this:
datco = [tr2[0,0], tr2[0,1], tr2[1,0]-tr2[0,0],tr2[1,1]-tr2[0,1]]
#and finally the new colorabar axes at the right position!
cbar_ax = fig.add_axes(datco)
#the rest stays the same:
clevs = [0, 1 , 2]
cb1 = plt.colorbar(hdl, cax=cbar_ax, orientation='horizontal', ticks=clevs)

plt.show()

推荐答案

这是基于对原始问题的评论而做的:

Here is what I did, based on the comments to my original question:

import numpy as np
import matplotlib.pyplot as plt    

a = -2
b = 2

x = (b - a) * np.random.random(50) + a
y = (b - a) * np.random.random(50) + a
z = (b) * np.random.random(50)

fig = plt.figure()
hdl = plt.scatter(x,y,s=20,c=z,marker='o',vmin=0,vmax=2)
ax = plt.gca()
ax.set_xlim([-2,2])
ax.set_ylim([-2,2])

#[(lower left x, lower left y), (upper right x, upper right y)] of the desired colorbar:
dat_coord = [(-1.5,1.5),(-0.5,1.75)]
#transform the two points from data coordinates to display coordinates:
tr1 = ax.transData.transform(dat_coord)
#create an inverse transversion from display to figure coordinates:
inv = fig.transFigure.inverted()
tr2 = inv.transform(tr1)
#left, bottom, width, height are obtained like this:
datco = [tr2[0,0], tr2[0,1], tr2[1,0]-tr2[0,0],tr2[1,1]-tr2[0,1]]
#and finally the new colorabar axes at the right position!
cbar_ax = fig.add_axes(datco)
#the rest stays the same:
clevs = [0, 1 , 2]
cb1 = plt.colorbar(hdl, cax=cbar_ax, orientation='horizontal', ticks=clevs)

plt.show()

这篇关于Python Matplotlib:在数据坐标中定位颜色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆