添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
傻傻的警车  ·  FastAPI websocket ...·  1 年前    · 
寂寞的登山鞋  ·  Linux下Appium+Python+he ...·  1 年前    · 

在matplotlib中,我可以在绘图的左边放置一个垂直色条吗?

6 人关注

From the colorbar方法的matplotlib命令摘要 我知道,作为参数的关键词参数 orientation := 'horizontal' | 'vertical' 将一个 横向 吧台 底下 的情节或一个 纵向 to the right of it respectively.

Yet, in my situation, I would rather place the colour吧台at the opposite side of the default (without too much fiddling... if possible).

我怎样才能编写这个代码?我错过了一些明显的功能吗?

1 个评论
一种方法是手动为色条创建额外的轴。 matplotlib.org/examples/pylab_examples/multi_image.html
python
matplotlib
XavierStuvw
XavierStuvw
发布于 2016-05-17
2 个回答
jgholder
jgholder
发布于 2016-05-18
已采纳
0 人赞同

我发现了另一种方法,它避免了手动编辑轴的位置,而是允许你通过使用location关键字来保持色条与现有绘图轴的链接(方法最初改编自 here ).

位置参数是为了用于在一个列表中引用多个轴的色条(如果色条只给出一个轴,将抛出一个错误),但如果你只是简单地把你的一个轴放在一个列表中,它将允许你使用该参数。你可以用下面的代码作为一个例子。

import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
axp = ax.imshow(np.random.randint(0, 100, (100, 100)))
cb = plt.colorbar(axp,ax=[ax],location='left')
plt.show()

which yields this plot:

Muon
哇,这真是一个伟大的黑客。谢谢你添加了这个迟到的答案!必须手动指定轴的位置的方法已经困扰了我多年。你是个传奇。
I got the error: got an unexpected keyword argument 'location'
@wander95 我也是,如果我只打了 ax=ax 。如果使用 ax=[ax] ,问题就解决了,就像答案中所说的那样。
armatita
armatita
发布于 2016-05-18
0 人赞同

我认为最简单的方法是确保色条在它自己的轴上。你可以从这个例子中改编。

import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
axp = ax.imshow(np.random.randint(0, 100, (100, 100)))
# Adding the colorbar
cbaxes = fig.add_axes([0.1, 0.1, 0.03, 0.8])  # This is the position for the colorbar
cb = plt.colorbar(axp, cax = cbaxes)
plt.show()