在matplotlib中制作高度为0的透明颜色条 [英] Make transparent color bar with height 0 in matplotlib

查看:70
本文介绍了在matplotlib中制作高度为0的透明颜色条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个3D条形图,显示两个3个区域的网络带宽。我想在matplotlib中将高度为0的条形设置为透明。在我的输出中,他们得到的颜色形成了一个高度为0的正方形。

我如何才能做到这一点?

编码:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

data = np.array([
[1000,200],
[100,1000],
])

column_names = ['','Oregon','', 'Ohio','']
row_names = ['','Oregon','', 'Ohio']

fig = plt.figure()
ax = Axes3D(fig)

lx= len(data[0])            # Work out matrix dimensions
ly= len(data[:,0])

xpos = np.arange(0,lx,1)    # Set up a mesh of positions
ypos = np.arange(0,ly,1)
xpos, ypos = np.meshgrid(xpos+0.25, ypos+0.25)

xpos = xpos.flatten()   # Convert positions to 1D array
ypos = ypos.flatten()
zpos = np.zeros(lx*ly)

dx = 0.5 * np.ones_like(zpos)
dy = dx.copy()
dz = data.flatten()

cs = ['r', 'g'] * ly

ax.bar3d(xpos,ypos,zpos, dx, dy, dz, color=cs)

ax.w_xaxis.set_ticklabels(column_names)
ax.w_yaxis.set_ticklabels(row_names)
ax.set_zlabel('Mb/s')

plt.show()

推荐答案

您可以为透明度设置alpha参数,该参数在bar3d的文档中有点隐藏,因为它包含在mpl_toolkits.mplot3d.art3d.Poly3DCollection提供的**kwargs中。

然而,还不可能给出每个单独栏的alpha值的数组,例如对于color键。因此,例如,您必须借助蒙版分别绘制透明和不透明的图形。

# needs to be an array for indexing
cs = np.array(['r', 'g', 'b'] * ly)

# Create mask: Find values of dz with value 0
mask = dz == 0

# Plot bars with dz == 0 with alpha
ax.bar3d(xpos[mask], ypos[mask], zpos[mask], dx[mask], dy[mask], dz[mask],
         color=cs[mask], alpha=0.2)
# Plot other bars without alpha
ax.bar3d(xpos[~mask], ypos[~mask], zpos[~mask], dx[~mask], dy[~mask], dz[~mask],
         color=cs[~mask])

这给了

这篇关于在matplotlib中制作高度为0的透明颜色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆