Make transparent color bar with height 0 in matplotlib(在matplotlib中制作高度为0的透明颜色条)
本文介绍了在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的透明颜色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:在matplotlib中制作高度为0的透明颜色条


基础教程推荐
猜你喜欢
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 包装空间模型 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01