如何在堆叠条形图显示中抑制零

时间:2019-09-08 18:12:19

标签: python numpy matplotlib stacked-chart

我有一个如下的堆积木炭样品。如何在图表中取消显示“ 0”值

只要我有0,我都不想显示这些值。有什么办法可以抑制零enter image description here

显示数据时出现以下代码

Loading model checkpoint weights ...
2019-09-08 20:05:13.667832: W tensorflow/core/util/tensor_slice_reader.cc:95] Could not open .\checkpoints: Unknown: NewRandomAccessFile failed to Create/Open: .\checkpoints : Access is denied.
; Input/output error
2019-09-08 20:05:13.873491: W tensorflow/core/util/tensor_slice_reader.cc:95] Could not open .\checkpoints: Unknown: NewRandomAccessFile failed to Create/Open: .\checkpoints : Access is denied.
; Input/output error
2019-09-08 20:05:13.878487: W tensorflow/core/framework/op_kernel.cc:1273] OP_REQUIRES failed at save_restore_tensor.cc:175 : Data loss: Unable to open table file .\checkpoints: Unknown: NewRandomAccessFile failed to Create/Open: .\checkpoints : Access is denied.
; Input/output error
Traceback (most recent call last):
  File "C:\Users\xxxxx\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\client\session.py", line 1334, in _do_call
    return fn(*args)
  File "C:\Users\xxxxx\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\client\session.py", line 1319, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "C:\Users\xxxxx\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\client\session.py", line 1407, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.DataLossError: Unable to open table file .\checkpoints: Unknown: NewRandomAccessFile failed to Create/Open: .\checkpoints : Access is denied.
; Input/output error
         [[{{node save_1/RestoreV2}} = RestoreV2[dtypes=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, ..., DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save_1/Const_0_0, save_1/RestoreV2/tensor_names, save_1/RestoreV2/shape_and_slices)]]
         [[{{node save_1/RestoreV2/_307}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device_incarnation=1, tensor_name="edge_312_save_1/RestoreV2", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 43, in <module>
    saver.restore(sess, args.checkpoint_path)
  File "C:\Users\xxxxx\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\training\saver.py", line 1546, in restore
    {self.saver_def.filename_tensor_name: save_path})
  File "C:\Users\xxxxx\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\client\session.py", line 929, in run
    run_metadata_ptr)
  File "C:\Users\xxxxx\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\client\session.py", line 1152, in _run
    feed_dict_tensor, options, run_metadata)
  File "C:\Users\xxxxx\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\client\session.py", line 1328, in _do_run
    run_metadata)
  File "C:\Users\xxxxx\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\client\session.py", line 1348, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.DataLossError: Unable to open table file .\checkpoints: Unknown: NewRandomAccessFile failed to Create/Open: .\checkpoints : Access is denied.
; Input/output error
         [[node save_1/RestoreV2 (defined at test.py:42)  = RestoreV2[dtypes=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, ..., DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save_1/Const_0_0, save_1/RestoreV2/tensor_names, save_1/RestoreV2/shape_and_slices)]]
         [[{{node save_1/RestoreV2/_307}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device_incarnation=1, tensor_name="edge_312_save_1/RestoreV2", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]

Caused by op 'save_1/RestoreV2', defined at:
  File "test.py", line 42, in <module>
    saver=tf.train.Saver(max_to_keep=1000)
  File "C:\Users\xxxxx\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\training\saver.py", line 1102, in __init__
    self.build()
  File "C:\Users\xxxxx\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\training\saver.py", line 1114, in build
    self._build(self._filename, build_save=True, build_restore=True)
  File "C:\Users\xxxxx\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\training\saver.py", line 1151, in _build
    build_save=build_save, build_restore=build_restore)
  File "C:\Users\xxxxx\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\training\saver.py", line 795, in _build_internal
    restore_sequentially, reshape)
  File "C:\Users\xxxxx\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\training\saver.py", line 406, in _AddRestoreOps
    restore_sequentially)
  File "C:\Users\xxxxx\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\training\saver.py", line 862, in bulk_restore
    return io_ops.restore_v2(filename_tensor, names, slices, dtypes)
  File "C:\Users\xxxxx\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\ops\gen_io_ops.py", line 1466, in restore_v2
    shape_and_slices=shape_and_slices, dtypes=dtypes, name=name)
  File "C:\Users\xxxxx\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "C:\Users\xxxxx\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\util\deprecation.py", line 488, in new_func
    return func(*args, **kwargs)
  File "C:\Users\xxxxx\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\ops.py", line 3274, in create_op
    op_def=op_def)
  File "C:\Users\xxxxx\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\ops.py", line 1770, in __init__
    self._traceback = tf_stack.extract_stack()

DataLossError (see above for traceback): Unable to open table file .\checkpoints: Unknown: NewRandomAccessFile failed to Create/Open: .\checkpoints : Access is denied.
; Input/output error
         [[node save_1/RestoreV2 (defined at test.py:42)  = RestoreV2[dtypes=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, ..., DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save_1/Const_0_0, save_1/RestoreV2/tensor_names, save_1/RestoreV2/shape_and_slices)]]
         [[{{node save_1/RestoreV2/_307}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device_incarnation=1, tensor_name="edge_312_save_1/RestoreV2", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]

我尝试了以下方法

for xpos, ypos, yval in zip(TABLE_NAMES, ID1/2, ID1):
    plt.text(xpos, ypos, yval, ha="center", va="center",fontsize=20)
for xpos, ypos, yval in zip(TABLE_NAMES, ID1+ID2/2, ID2):
    plt.text(xpos, ypos, yval, ha="center", va="center",fontsize=20)

我有什么办法可以实现

以及如何使y轴根据数据显示(根据下面的图像,我在Y轴上最多可以有6个。为此我使用np.arange

ID1[ID1 == 0] = np.nan ( to pass nan value but i am getting error 
Error: ValueError: cannot convert float NaN to integer

,但将来我可能会有大于100的不同值。没有指定任何像np.arange这样的函数,有什么方法可以动态传递它来处理yaxis而没有任何范围..?

1 个答案:

答案 0 :(得分:2)

由于已经生成了矩形补丁,因此我们可以从图中获取高度和宽度,并基于这些高度和宽度添加文本:

In [164]: df
Out[164]: 
          a         b         c         d
0  0.807540  0.719843  0.291329  0.928670
1  0.449082  0.000000  0.575919  0.299698
2  0.703734  0.626004  0.582303  0.243273
3  0.363013  0.539557  0.000000  0.743613
4  0.185610  0.526161  0.795284  0.929223
5  0.000000  0.323683  0.966577  0.259640
6  0.000000  0.386281  0.000000  0.000000
7  0.500604  0.131910  0.413131  0.936908
8  0.992779  0.672049  0.108021  0.558684
9  0.797385  0.199847  0.329550  0.605690

In [165]:
from matplotlib.patches import Rectangle
df.plot.bar(stacked=True)
ax = plt.gca()
for p in ax.get_children()[:-1]:  # skip the last patch as it is the background
    if isinstance(p, Rectangle):
        x, y = p.get_xy()
        w, h = p.get_width(), p.get_height()
        if h > 0:  # anything that have a height of 0 will not be annotated
            ax.text(x + 0.5 * w, y + 0.5 * h, '%0.2f'%h, va='center', ha='center')
plt.show()

enter image description here