Python matplotlib 颜色条轴偏移字符串位置/位置
我想制作带有水平颜色条的等高线图。条形图应该有带科学记数法的标签。下面的代码一切顺利。但是,现在我想更改指定科学刻度的乘数的位置。我认为这被称为轴的偏移字符串。有人知道该怎么做吗?我想将乘法器放置在颜色条的右侧(就好像它是一个附加标签)。
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在上面的代码中,只需使用:
其中
(x,y)
是您想要在轴坐标中放置文本的位置(例如x=0
是左侧,x=1
是右侧)。另请注意,在这种情况下,设置 y 坐标不会执行任何操作,因为 y 方向的位置是由轴填充等控制的。(您可以更改它,但稍微复杂一些。)
From your code above, just use:
where
(x,y)
is the location where you want the text in axes coordinates (e.g.x=0
is the left hand side andx=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.)
我发现该命令可以让您直接在标签位置上工作。
我花了一些迭代才能正确定位文本,因为我发现工作颜色条坐标完全不直观。
I found that that command lets you directly work on the label position.
It took me some iterations to position text correctly, since I found working colorbar coordinates completely unintuitive.
根据上面 Joe Kington 的建议,我发现以下修改最简单:
Following the suggestion from Joe Kington above, I found the following modification most easy: