【问题标题】:Matplotlib hexbin add colour bar - for dummies [duplicate]Matplotlib hexbin 添加颜色条 - 用于傻瓜 [重复]
【发布时间】:2017-04-11 12:36:52
【问题描述】:

无法弄清楚如何简单地将颜色条添加到hexbinmatplotlib 绘图的矩阵中。这是一个 7 x 7 的图矩阵,这是第一行的部分代码:

#importing nec. libs
import pandas as pd
from pylab import rcParams
from matplotlib import pyplot as PLT
%matplotlib inline

#reading data
dataset_cat_coded_path = 'C:/Users/IBM_ADMIN/Documents/Sberbank/dataset_cat_coded.csv'
dataset_cat_coded = pd.read_csv(dataset_cat_coded_path, encoding = 'cp1251')
test_frame = dataset_cat_coded[['opf','priority', 'sales_method_id', 'is_block_vks','sector_id', 'segment_id', 'org_type_id']]



#building figure
rcParams['figure.figsize']=30,30 #fig. size

#fig. initiation
fig = PLT.figure()

#setting parameters defining min and max luminance
vmin = 0
vmax=test_frame.shape[0]

#building a row of plots
ax1 = fig.add_subplot(7,7,2)
ax1.hexbin(test_frame.opf, test_frame.priority,C=None,gridsize=12,bins=None,mincnt=1, vmin = vmin, vmax=vmax)
ax1.set_title('priority')
ax1.set_ylabel('OPF')

ax2 = fig.add_subplot(7,7,3)
ax2.hexbin(test_frame.opf, test_frame.sales_method_id,C=None,gridsize=12,bins=None,mincnt=1, vmin = vmin, vmax=vmax)
ax2.set_title('sales_method_id')

ax3 = fig.add_subplot(7,7,4)
ax3.hexbin(test_frame.opf, test_frame.is_block_vks,C=None,gridsize=12,bins=None,mincnt=1, vmin = vmin, vmax=vmax)
ax3.set_title('is_block_vks')

ax4 = fig.add_subplot(7,7,5)
ax4.hexbin(test_frame.opf, test_frame.sector_id,C=None,gridsize=12,bins=None,mincnt=1, vmin = vmin, vmax=vmax)
ax4.set_title('sector_id')

ax5 = fig.add_subplot(7,7,6)
ax5.hexbin(test_frame.opf, test_frame.segment_id,C=None,gridsize=12,bins=None,mincnt=1, vmin = vmin, vmax=vmax)
ax5.set_title('segment_id')

ax6 = fig.add_subplot(7,7,7)
ax6.hexbin(test_frame.opf, test_frame.org_type_id,C=None,gridsize=12,bins=None,mincnt=1, vmin = vmin, vmax=vmax)
ax6.set_title('org_type_id')

#colorbar attempt
cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
fig.colorbar(ax6, cax=cbar_ax, location='bottom') here

错误文本:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-e885eb3cd39a> in <module>()
     36 
     37 cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
---> 38 fig.colorbar(ax6, cax=cbar_ax, location='bottom')

C:\Program Files\Anaconda3\lib\site-packages\matplotlib\figure.py in colorbar(self, mappable, cax, ax, use_gridspec, **kw)
   1591                 cax, kw = cbar.make_axes(ax, **kw)
   1592         cax.hold(True)
-> 1593         cb = cbar.colorbar_factory(cax, mappable, **kw)
   1594 
   1595         self.sca(current_ax)

C:\Program Files\Anaconda3\lib\site-packages\matplotlib\colorbar.py in colorbar_factory(cax, mappable, **kwargs)
   1328         cb = ColorbarPatch(cax, mappable, **kwargs)
   1329     else:
-> 1330         cb = Colorbar(cax, mappable, **kwargs)
   1331 
   1332     cid = mappable.callbacksSM.connect('changed', cb.on_mappable_changed)

C:\Program Files\Anaconda3\lib\site-packages\matplotlib\colorbar.py in __init__(self, ax, mappable, **kw)
    878         # Ensure the given mappable's norm has appropriate vmin and vmax set
    879         # even if mappable.draw has not yet been called.
--> 880         mappable.autoscale_None()
    881 
    882         self.mappable = mappable

AttributeError: 'AxesSubplot' object has no attribute 'autoscale_None'

结果图: enter image description here

【问题讨论】:

  • 那里有很多例子,那么到目前为止你尝试过什么?另外,您需要告诉您是否要在整个图形中添加一个颜色条?如果是这样,所有数据的数据限制是否相同?或者您希望每个图形有一个颜色条?
  • 数据限制是相同的,所以整个图有 1 个颜色条。老实说,如果我能在数字之前单独做,那也可以。问题是我无法理解在我的特定情况下将哪些参数传递给 plt.colorbar() 以使其工作。
  • @ImportanceOfBeingErnest 解决方案从您引用的页面cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7]) fig.colorbar(im, cax=cbar_ax) 给出错误'AxesSubplot' 对象没有属性'autoscale_None'
  • 您能否更新代码以包含您尝试过的内容以及错误的完整回溯? this example 是否产生相同的错误?如果不是,区别在哪里?您使用的是哪个版本的 matplotlib?
  • @ImportanceOfBeingErnest 感谢您坚持这个帖子,抱歉,这几天很忙。我已经更新了代码,尝试在底部添加颜色条,错误文本也与结果图一起添加

标签: python matplotlib colorbar


【解决方案1】:

正如我在 cmets 中指出的,this question 展示了如何添加颜色条。

在你的代码中

fig.colorbar(ax6, cax=cbar_ax, location='bottom')

您尝试为图形设置一个颜色条,但没有指定它应该与哪个对象相关。因此,您会收到一个错误,它基本上告诉您您没有为任何对象提供可映射的对象。

您需要做的是将您的 hexbin 图像提供给颜色条,以便它知道实际绘制哪些颜色。

import numpy as np
import matplotlib.pyplot as plt

n = 1000
x = np.random.standard_normal(n)
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)

fig, axes = plt.subplots(nrows=2, ncols=2)
for ax in axes.flat:
    im = ax.hexbin(x,y)

fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
fig.colorbar(im, cax=cbar_ax)

plt.show()

【讨论】:

  • 是的,非常感谢您提供最终答案!现在我必须弄清楚如何改变它的大小并将它带到整个图矩阵的底部。
  • 这里的数字fig.add_axes([0.85, 0.15, 0.05, 0.7]) 决定了颜色条的位置和大小。第一个数字定义了条的最左侧应该在整个图形的水平轴上的位置,左下角为零。第二个数字在垂直轴上确定相同。如果数字为 [0.1, 0.1],则条形图将从左下角延伸。第三个数字根据图形的部分确定颜色条的长度(= 0.5 -> 颜色条延伸到图形的一半)。最后一个数字决定了相同术语的宽度。
  • 确实,add_axes(rect) 将一个矩形作为输入,其中rect = [left, bottom, width, height]。这些数字以图形坐标系为单位(从左下角的0,0到右上角的1,1)。
猜你喜欢
  • 2018-05-29
  • 1970-01-01
  • 1970-01-01
  • 2014-01-12
  • 2012-03-06
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多