Python matplotlib 颜色条轴偏移字符串位置/位置

发布于 2024-11-17 14:35:59 字数 961 浏览 3 评论 0原文

我想制作带有水平颜色条的等高线图。条形图应该有带科学记数法的标签。下面的代码一切顺利。但是,现在我想更改指定科学刻度的乘数的位置。我认为这被称为轴的偏移字符串。有人知道该怎么做吗?我想将乘法器放置在颜色条的右侧(就好像它是一个附加标签)。

#http://matplotlib.sourceforge.net/examples/pylab_examples/griddata_demo.html
from numpy.random import uniform, seed
from matplotlib.mlab import griddata
import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np
from matplotlib import rcParams
from matplotlib import rc

# make up data.
seed(0)
npts = 200
x = uniform(-2,2,npts)
y = uniform(-2,2,npts)
z = x*np.exp(-x**2-y**2)
# define grid
xi = np.linspace(-2.1,2.1,100)
yi = np.linspace(-2.1,2.1,200)
# grid the data.
zi = griddata(x,y,z,xi,yi,interp='linear')

rcParams['axes.formatter.limits'] = (-1,1)

plt.figure()
CS    = plt.contour(xi,yi,zi,25,cmap=plt.cm.jet)
bar    = plt.colorbar(orientation='horizontal') # draw colorbar
# plot data points.
plt.xlim(-2,2)
plt.ylim(-2,2)
plt.title('griddata test (%d points)' % npts)
plt.show()

I want to make a contour-plot with a horizontal colorbar. The bar should have labels with scientific notation. Everything goes well in the code below. However, now I want to change the position of the multiplicator specifying the scientific scale. I think this is called an offset string by the axis. Does anybody know how to do this? I want to place the multiplicator right of the colorbar (as if it would be an additional label).

#http://matplotlib.sourceforge.net/examples/pylab_examples/griddata_demo.html
from numpy.random import uniform, seed
from matplotlib.mlab import griddata
import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np
from matplotlib import rcParams
from matplotlib import rc

# make up data.
seed(0)
npts = 200
x = uniform(-2,2,npts)
y = uniform(-2,2,npts)
z = x*np.exp(-x**2-y**2)
# define grid
xi = np.linspace(-2.1,2.1,100)
yi = np.linspace(-2.1,2.1,200)
# grid the data.
zi = griddata(x,y,z,xi,yi,interp='linear')

rcParams['axes.formatter.limits'] = (-1,1)

plt.figure()
CS    = plt.contour(xi,yi,zi,25,cmap=plt.cm.jet)
bar    = plt.colorbar(orientation='horizontal') # draw colorbar
# plot data points.
plt.xlim(-2,2)
plt.ylim(-2,2)
plt.title('griddata test (%d points)' % npts)
plt.show()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

被翻牌 2024-11-24 14:35:59

在上面的代码中,只需使用:

bar.ax.xaxis.get_offset_text().set_position((x,y))

其中 (x,y) 是您想要在轴坐标中放置文本的位置(例如 x=0 是左侧, x=1 是右侧)。

另请注意,在这种情况下,设置 y 坐标不会执行任何操作,因为 y 方向的位置是由轴填充等控制的。(您可以更改它,但稍微复杂一些。)

From your code above, just use:

bar.ax.xaxis.get_offset_text().set_position((x,y))

where (x,y) is the location where you want the text in axes coordinates (e.g. x=0 is the left hand side and x=1 is the right hand side).

Also note that setting the y-coordinate won't do anything in this case, as the location in the y-direction is controlled by the axis padding, etc. (You can change it, but it's slightly more complicated.)

梦晓ヶ微光ヅ倾城 2024-11-24 14:35:59

我发现该命令可以让您直接在标签位置上工作。

bar.ax.yaxis.set_label_coords(3, .5)

我花了一些迭代才能正确定位文本,因为我发现工作颜色条坐标完全不直观。

I found that that command lets you directly work on the label position.

bar.ax.yaxis.set_label_coords(3, .5)

It took me some iterations to position text correctly, since I found working colorbar coordinates completely unintuitive.

要走就滚别墨迹 2024-11-24 14:35:59

根据上面 Joe Kington 的建议,我发现以下修改最简单:

bar.ax.yaxis.set_label_position('right')
bar.ax.set_ylabel('YourLabel', rotation='horizontal')

Following the suggestion from Joe Kington above, I found the following modification most easy:

bar.ax.yaxis.set_label_position('right')
bar.ax.set_ylabel('YourLabel', rotation='horizontal')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文