Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

3d bargraph issue in Matplotlib

When I make a 3d bargraph with 4 or more values the graph looks correct but when I I try it with 3 the bars become triangles, what's going on?

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

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

color_grade_classes = ['#80FF00','#FFFF00','#FF8000', '#FF0000']

for colors, rows  in zip(color_grade_classes, [3,2,1,0] ):  
  indexs = np.arange(3)
  heights = np.random.rand(3)
  print rows, indexs, heights, colors

  ax.bar(indexs, heights, zs = rows,  zdir='y', color=colors, alpha=0.8)

ax.set_xlabel('X')
ax.set_ylabel('Y')

plt.show()

generates this:

Triangular bar

but when I increase the number of indexes and heights to 5 I get this:

Correct bar

like image 921
user1170056 Avatar asked Jan 25 '12 20:01

user1170056


People also ask

Is 3D graph possible in Matplotlib?

Matplotlib was introduced keeping in mind, only two-dimensional plotting. But at the time when the release of 1.0 occurred, the 3d utilities were developed upon the 2d and thus, we have 3d implementation of data available today! The 3d plots are enabled by importing the mplot3d toolkit.

Can bar graphs be 3D?

A bar 3D chart represents quantitative information. The chart consists of horizontally aligned rectangular bars of equal width with lengths proportional to the values they represent, something that aids in instant comparison of data. One axis of the chart plots categories and the other axis represents the value scale.


1 Answers

This is almost certainly a bug. Trying your example code gives me the desired result of rectangular bars, with any number of points (tested with 1, 2, 3, 5, 15): Example of code working as desired for 3 points

I'm running matplotlib version 1.1.1rc, on linux. If you can, try updating to the latest version. Note that

import matplotlib
print matplotlib.__version__

will tell you what version you're using.

like image 76
Widjet Avatar answered Oct 13 '22 01:10

Widjet