8

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. Displaying axes labels correctly (currently misses out final (or initial?) tick labels)
  2. Either making the ticks on each axis (e.g. that for 'Mon') either point to it's corresponding blue bar, or position the tick label for between the major tick marks.
  3. Making the bars semi-transparent.

image of plot uploaded here

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()
Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
Dman
  • 105
  • 1
  • 2
  • 5

1 Answers1

11

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)

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: enter image description here

carla
  • 2,181
  • 19
  • 26
  • Hi, thanks for your suggestion. I tried using the alpha argument, but received the following error: `File "matrix_to_3D_histogram.py", line 43, in ax.bar3d(xpos,ypos,zpos, dx, dy, dz, color='b', alpha=0.5) TypeError: bar3d() got an unexpected keyword argument 'alpha'` – Dman Feb 27 '12 at 12:57
  • That's weird, I have no problem at all, and I just copied your code and changed the line I told you. Which version are you using? I am using matplotlib 1.1.0. – carla Feb 27 '12 at 13:57
  • @Dan to test the version of matplotlib type this: `import matplotlib; print matplotlib.__version__` – Hooked Feb 27 '12 at 14:37
  • Hi again, looks like you've managed to get that figure looking just how I wanted, that's great thanks! I looked at my version of Matplotlib, and (as i'm on Ubuntu 10.04) I have version 0.99.1.1 installed from synaptic by default. I just tried installing Matplotlib version 1.1.0 from source, but Python still thinks I have version 0.99.1.1. Might I need to uninstall the old version, or change a path variable somewhere? – Dman Feb 27 '12 at 14:53
  • I don't really know... I would remove the old version first, and then see what happens! Good luck... – carla Feb 28 '12 at 11:57
  • Hey, I have a data set where I have three types of codes - say c1, c2 and c3. Each code can have 4 types of errors, say b1, b2, b3 and b4. I want to plot the number of such bugs for each type of code across each category over the last 5 weeks. Say code of type c1 has (b1,b2,b3,b4) as (12,34,13,67) on first week, (11,123,3,21) on 2nd week, etc. Can I draw this data on a 3d column chart like this? – SexyBeast Oct 21 '15 at 18:44
  • How do you get the black lines framing the columns? – Martien Lubberink Mar 31 '18 at 03:22