Unit testing 识别现有fig、ax的条形图颜色

Unit testing 识别现有fig、ax的条形图颜色,unit-testing,matplotlib,Unit Testing,Matplotlib,我有一个现有的fig,ax,它是从一个我不能改变的函数输出的 fig, ax = locked_function(data) 我正在创建一个单元测试,需要能够查看ax.bar # The function I am looking for would let me check the color of a particular bar assert ax.get_color_of_bar2() == 'red' 我可以看到ax.get_children()中的矩形对象,但它们似乎没有颜色

我有一个现有的fig,ax,它是从一个我不能改变的函数输出的

fig, ax = locked_function(data)
我正在创建一个单元测试,需要能够查看
ax.bar

# The function I am looking for would let me check the color of a particular bar

assert ax.get_color_of_bar2() == 'red'
我可以看到ax.get_children()中的矩形对象,但它们似乎没有颜色

条形图本质上是一个对象。您可以使用方法
get\u facecolor()
从中获取颜色。检查以下示例:

import matplotlib.pyplot as plt
import numpy as np

def plot():
    N = 5
    Means = (20, 35, 30, 35, 27)
    Std = (2, 3, 4, 1, 2)
    ind = np.arange(N)  # the x locations for the groups
    width = 0.35       # the width of the bars

    fig, ax = plt.subplots()
    rects1 = ax.bar(ind, Means, width, color='r')
    return rects1

r = plot()
for i in r:
    print(i.get_facecolor())
plt.show()
,这将返回每个条颜色的元组(使用打印)和绘图本身:

(1.0, 0.0, 0.0, 1.0)
(1.0, 0.0, 0.0, 1.0)
(1.0, 0.0, 0.0, 1.0)
(1.0, 0.0, 0.0, 1.0)
(1.0, 0.0, 0.0, 1.0)

你没有要求它,但它可能会在将来帮助你。当我陷入困境,不知道获得(或改变)某些东西的正确方法通常是:

r = plot()
help(r[0])
这将返回对象的贴图。并非总是易于阅读,但函数将按如下方式打印:

 ...
 |  get_ec = get_edgecolor(self)
 |      Return the edge color of the :class:`Patch`.
 |  
 |  get_edgecolor(self)
 |      Return the edge color of the :class:`Patch`.
 |  
 |  get_extents(self)
 |      Return a :class:`~matplotlib.transforms.Bbox` object defining
 |      the axis-aligned extents of the :class:`Patch`.
 |  
 |  get_facecolor(self)
 |      Return the face color of the :class:`Patch`.
 |  
 |  get_fc = get_facecolor(self)
 |      Return the face color of the :class:`Patch`.
 |  
 |  get_fill(self)
 |      return whether fill is set
 ...
{'_joinstyle': 'miter', '_gid': None, 'figure': <matplotlib.figure.Figure object at 0x00000000025AF828>, '_angle': 0.0, '_original_edgecolor': 'k', '_hatch': None, '_transform': CompositeGenericTransform(TransformWrapper(BlendedAffine2D(IdentityTransform(),IdentityTransform())), CompositeGenericTransform(BboxTransformFrom(TransformedBbox(Bbox([[0.0, 0.0], [4.5, 35.0]]), TransformWrapper(BlendedAffine2D(IdentityTransform(),IdentityTransform())))), BboxTransformTo(TransformedBbox(Bbox([[0.125, 0.09999999999999998], [0.9, 0.9]]), BboxTransformTo(TransformedBbox(Bbox([[0.0, 0.0], [8.0, 6.0]]), Affine2D(array([[ 80.,   0.,   0.],
       [  0.,  80.,   0.],
       [  0.,   0.,   1.]])))))))), '_antialiased': True, '_remove_method': <function _AxesBase.add_patch.<locals>.<lambda> at 0x0000000004D068C8>, '_y': 0.0, '_facecolor': (1.0, 0.0, 0.0, 1.0), '_snap': None, '_label': '_nolegend_', '_original_facecolor': array([ 1.,  0.,  0.,  1.]), '_rasterized': None, '_linestyle': 'solid', '_width': 0.35, '_combined_transform': IdentityTransform(), '_x': 0.0, '_oid': 0, '_sketch': None, '_alpha': None, 'eventson': False, '_transformSet': True, '_path_effects': [], 'clipbox': TransformedBbox(Bbox([[0.0, 0.0], [1.0, 1.0]]), CompositeGenericTransform(CompositeGenericTransform(BboxTransformTo(Bbox([[0.0, 0.0], [1.0, 1.0]])), Affine2D(array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]]))), BboxTransformTo(TransformedBbox(Bbox([[0.125, 0.09999999999999998], [0.9, 0.9]]), BboxTransformTo(TransformedBbox(Bbox([[0.0, 0.0], [8.0, 6.0]]), Affine2D(array([[ 80.,   0.,   0.],
       [  0.,  80.,   0.],
       [  0.,   0.,   1.]])))))))), '_picker': None, '_clippath': None, '_propobservers': {}, '_clipon': True, 'stale_callback': <function _stale_axes_callback at 0x000000000438C9D8>, '_animated': False, '_agg_filter': None, '_mouseover': False, '_stale': True, '_rect_transform': CompositeGenericTransform(BboxTransformTo(Bbox([[0.0, 0.0], [0.35, 20.0]])), Affine2D(array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]]))), '_axes': <matplotlib.axes._subplots.AxesSubplot object at 0x0000000004BB4518>, '_url': None, '_linewidth': 1.0, '_height': 20.0, '_capstyle': 'butt', '_contains': None, '_fill': True, '_visible': True, '_edgecolor': (0.0, 0.0, 0.0, 1.0)
另一个选项是
\uuuuu dict\uuuu
属性:

print(r[0].__dict__)
其结果如下:

 ...
 |  get_ec = get_edgecolor(self)
 |      Return the edge color of the :class:`Patch`.
 |  
 |  get_edgecolor(self)
 |      Return the edge color of the :class:`Patch`.
 |  
 |  get_extents(self)
 |      Return a :class:`~matplotlib.transforms.Bbox` object defining
 |      the axis-aligned extents of the :class:`Patch`.
 |  
 |  get_facecolor(self)
 |      Return the face color of the :class:`Patch`.
 |  
 |  get_fc = get_facecolor(self)
 |      Return the face color of the :class:`Patch`.
 |  
 |  get_fill(self)
 |      return whether fill is set
 ...
{'_joinstyle': 'miter', '_gid': None, 'figure': <matplotlib.figure.Figure object at 0x00000000025AF828>, '_angle': 0.0, '_original_edgecolor': 'k', '_hatch': None, '_transform': CompositeGenericTransform(TransformWrapper(BlendedAffine2D(IdentityTransform(),IdentityTransform())), CompositeGenericTransform(BboxTransformFrom(TransformedBbox(Bbox([[0.0, 0.0], [4.5, 35.0]]), TransformWrapper(BlendedAffine2D(IdentityTransform(),IdentityTransform())))), BboxTransformTo(TransformedBbox(Bbox([[0.125, 0.09999999999999998], [0.9, 0.9]]), BboxTransformTo(TransformedBbox(Bbox([[0.0, 0.0], [8.0, 6.0]]), Affine2D(array([[ 80.,   0.,   0.],
       [  0.,  80.,   0.],
       [  0.,   0.,   1.]])))))))), '_antialiased': True, '_remove_method': <function _AxesBase.add_patch.<locals>.<lambda> at 0x0000000004D068C8>, '_y': 0.0, '_facecolor': (1.0, 0.0, 0.0, 1.0), '_snap': None, '_label': '_nolegend_', '_original_facecolor': array([ 1.,  0.,  0.,  1.]), '_rasterized': None, '_linestyle': 'solid', '_width': 0.35, '_combined_transform': IdentityTransform(), '_x': 0.0, '_oid': 0, '_sketch': None, '_alpha': None, 'eventson': False, '_transformSet': True, '_path_effects': [], 'clipbox': TransformedBbox(Bbox([[0.0, 0.0], [1.0, 1.0]]), CompositeGenericTransform(CompositeGenericTransform(BboxTransformTo(Bbox([[0.0, 0.0], [1.0, 1.0]])), Affine2D(array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]]))), BboxTransformTo(TransformedBbox(Bbox([[0.125, 0.09999999999999998], [0.9, 0.9]]), BboxTransformTo(TransformedBbox(Bbox([[0.0, 0.0], [8.0, 6.0]]), Affine2D(array([[ 80.,   0.,   0.],
       [  0.,  80.,   0.],
       [  0.,   0.,   1.]])))))))), '_picker': None, '_clippath': None, '_propobservers': {}, '_clipon': True, 'stale_callback': <function _stale_axes_callback at 0x000000000438C9D8>, '_animated': False, '_agg_filter': None, '_mouseover': False, '_stale': True, '_rect_transform': CompositeGenericTransform(BboxTransformTo(Bbox([[0.0, 0.0], [0.35, 20.0]])), Affine2D(array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]]))), '_axes': <matplotlib.axes._subplots.AxesSubplot object at 0x0000000004BB4518>, '_url': None, '_linewidth': 1.0, '_height': 20.0, '_capstyle': 'butt', '_contains': None, '_fill': True, '_visible': True, '_edgecolor': (0.0, 0.0, 0.0, 1.0)
{u joinstyle':'miter','u gid':无,'figure':,'u angle':0.0,'u original_edgecolor':'k','u hatch':无,'u transform':CompositeGenericTransform(混合仿射2d(IdentityTransform(),IdentityTransform()),CompositeGenericTransform(BboxTransformFrom(TransformedBbox)[[0.0.0,0.0],[4.5,35.0])),Transformer包装器(BlendedAffine2D(IdentityTransform(),IdentityTransform())))、BboxTransformTo(TransformedBbox(Bbox([[0.125,0.0999999999998]、[0.9,0.9]])、BboxTransformTo(TransformedBbox([[0.0,0.0]、[8.0,6.0]])、Affine2D(数组([80,0,0.])、0.]),
[  0.,  80.,   0.],
[0,0,1.]]()()()("抗锯齿"("真"),"删除"方法":","0.0","面色":(1.0,0.0,1.0),""",""面色":""1.0,"""","面色,“'u x':0.0,'u oid':0,'u sketch':None,'u alpha':None,'eventson':False,'u transformSet':True,'u path'u effects':[],'clipbox':TransformedBbox(Bbox([[0.0,0.0],[1.0,1.0]])),compositegenerictronictTransform(Bbox([[0.0,0.0],[1.0,1.0])),仿射数组([1,0,0]),0],
[ 0.,  1.,  0.],
[0,0,1.]])、BboxTransformTo(转换Bbox(Bbox([[0.125,0.0999999999998]、[0.9,0.9]])、BboxTransformTo(转换Bbox(Bbox([[0.0,0.0]、[8.0,6.0]])、仿射2D(数组([[80,0,0,0]]),
[  0.,  80.,   0.],
[0,0,1.](‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘,
[ 0.,  1.,  0.],
[0,0,1.]]),“_轴”:,“_url”:无,“_线宽”:1.0,“_高度”:20.0,“_capstyle”:对接,“_包含”:无,“_填充”:真,“_可见”:真,“_边缘颜色”:(0.0,0.0,1.0)

这是相关的,但无可否认不是我要求的,但我输入了一个十六进制颜色。我假设它输出的是RGB值。为什么是4而不是3?@Chris Matplotlib使用浮点RGBA(0到1)。第四个元素是透明度的alpha通道。要从matplotlib的任何十六进制或内置颜色获取RGBA,您可能需要使用库。