我无法让像这样的imshow图形上的颜色条与图形的高度相同,因为事后没有使用Photoshop。我如何让高度匹配?
当前回答
这种组合(以及接近这些值)似乎“神奇地”为我工作,以保持颜色条缩放到绘图,无论显示器的大小。
plt.colorbar(im,fraction=0.046, pad=0.04)
它也不需要共享轴,这可以使绘图脱离正方形。
其他回答
如果不想声明另一组轴,我找到的最简单的解决方案是使用figsize调用更改图形大小。
在上面的例子中,我将从
fig = plt.figure(figsize = (12,6))
然后用不同的比例重新渲染,直到颜色条不再使主要情节相形见绌。
使用matplotlib AxisDivider可以很容易地做到这一点。
链接页面中的例子也可以不使用子图:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np
plt.figure()
ax = plt.gca()
im = ax.imshow(np.arange(100).reshape((10,10)))
# create an axes on the right side of ax. The width of cax will be 5%
# of ax and the padding between cax and ax will be fixed at 0.05 inch.
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
plt.colorbar(im, cax=cax)
我最近遇到了这个问题,我使用ax.twinx()来解决它。例如:
from matplotlib import pyplot as plt
# Some other code you've written
...
# Your data generation goes here
xdata = ...
ydata = ...
colordata = function(xdata, ydata)
# Your plotting stuff begins here
fig, ax = plt.subplots(1)
im = ax.scatterplot(xdata, ydata, c=colordata)
# Create a new axis which will be the parent for the colour bar
# Note that this solution is independent of the 'fig' object
ax2 = ax.twinx()
ax2.tick_params(which="both", right=False, labelright=False)
# Add the colour bar itself
plt.colorbar(im, ax=ax2)
# More of your code
...
plt.show()
我发现这在创建将matplotlib Axes对象作为参数、在它们上绘图并返回对象的函数时特别有用,因为这样我就不需要传入必须从图形对象生成的单独轴,也不需要传入图形对象本身。
创建颜色条时,尝试使用分数和/或收缩参数。
从文件中:
分数0.15;用于颜色条的原始轴的部分 减少1.0;用来缩小颜色条的分数
这种组合(以及接近这些值)似乎“神奇地”为我工作,以保持颜色条缩放到绘图,无论显示器的大小。
plt.colorbar(im,fraction=0.046, pad=0.04)
它也不需要共享轴,这可以使绘图脱离正方形。
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式
- 使用try和。Python中的if
- 如何在Python中获得所有直接子目录