Python matplotlib ->3D 条形图 ->调整刻度标签位置,透明条 [英] Python matplotlib -> 3D bar plot -> adjusting tick label position, transparent bars

查看:93
本文介绍了Python matplotlib ->3D 条形图 ->调整刻度标签位置,透明条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Matplotlib中的bar3d()在Python中创建3D条形图.

I am trying to create a 3D bar histogram in Python using bar3d() in Matplotlib.

在传递一些数据后,我已经可以在屏幕上显示直方图了,但是我陷入了以下困境:

I have got to the point where I can display my histogram on the screen after passing it some data, but I am stuck on the following:

  1. 正确显示轴标签(当前错过了最终的(或初始的?)刻度标签)
  2. 要么在每个轴上打勾(例如星期一"的那个)要么指向它对应的蓝色条,要么在主要刻度线之间放置刻度标签.
  3. 使条形半透明.

此处上传>

我曾尝试将多个不同的参数传递给'ax'实例,但是尽管有任何疑问,但我并没有做任何事情,并且怀疑我对提供的内容有误解.我将非常感谢您对此提供的任何帮助.

I have tried passing several different arguments to the 'ax' instance, but have not got anything to work despite and suspect I have misunderstood what to provide it with. I will be very grateful for any help on this at all.

这是我正在处理的代码示例:

Here is a sample of the code i'm working on:

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

#from IPython.Shell import IPShellEmbed
#sh = IPShellEmbed()

data = np.array([
[0,1,0,2,0],
[0,3,0,2,0],
[6,1,1,7,0],
[0,5,0,2,9],
[0,1,0,4,0],
[9,1,3,4,2],
[0,0,2,1,3],
])

column_names = ['a','b','c','d','e']
row_names = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']

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

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

#sh()
ax.w_xaxis.set_ticklabels(column_names)
ax.w_yaxis.set_ticklabels(row_names)
ax.set_xlabel('Letter')
ax.set_ylabel('Day')
ax.set_zlabel('Occurrence')

plt.show()

推荐答案

要使条形半透明,您只需使用 alpha 参数.alpha = 0表示100%透明,而alpha = 1(默认值)表示0%透明.

To make the bars semi-transparent, you just have to use the alpha parameter. alpha=0 means 100% transparent, while alpha=1 (the default) means 0% transparent.

试试这个,它会使条形半透明:

Try this, it will work out to make the bars semi-transparent:

ax.bar3d(xpos,ypos,zpos, dx, dy, dz, color='b', alpha=0.5)

关于刻度位置,您可以使用这样的方法(plt.xticks 或 plt.yticks 上的第一个列表包含您要在何处定位刻度的值",第二个列表包含您实际的位置想要打勾):

Regarding the ticks location, you can do it using something like this (the first list on plt.xticks or plt.yticks contains the "values" where do you want to locate the ticks, and the second list contains what you actually want to call the ticks):

#ax.w_xaxis.set_ticklabels(column_names)
#ax.w_yaxis.set_ticklabels(row_names)

ticksx = np.arange(0.5, 5, 1)
plt.xticks(ticksx, column_names)

ticksy = np.arange(0.6, 7, 1)
plt.yticks(ticksy, row_names)

最后,我得到了这个数字:

In the end, I get this figure:

这篇关于Python matplotlib ->3D 条形图 ->调整刻度标签位置,透明条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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