识别现有无花果,斧头的条形颜色

时间:2016-06-11 19:55:48

标签: unit-testing matplotlib

我有一个现有的无花果,斧头是从我无法改变的功能输出的。

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()内的矩形对象,但它们似乎没有附加颜色..

1 个答案:

答案 0 :(得分:2)

一个bar本质上是一个Rectangle对象。通过它,您可以使用方法get_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()

,这将返回每个条形图的颜色(使用print)和图形本身的元组:

(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)

Bar plot with red bars in matplotlib

你没有要求它,但也许它将在未来帮助你。当我被困住并且不知道获取某些东西(或改变某些东西)的正确方法时,通常会这样做:

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
 ...

另一个选项是__dict__属性:

print(r[0].__dict__)

结果是这样的:

{'_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)