0

enter image description hereHow can I adjust width of a bar graph's bars. An image has been attached with this post, which shows the actual condition. As we can see the labels, showing years, from 2011 to 2015.

I am finding a solution to reduce down the width of respective bars along the yaxis. Many random solutions were tried by me which is available here and there on line but nothing has worked.

The given image is actually a 3D graph but due to clear view only 2D picture has been shown.

Please help:

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

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

#data = data.transpose()

row_names = ['Ch','Bi','Ba','Ho','Ja','Pu','N','Ga','Ke','Ma','Me','Ti','Ka','Th','Di','Wa','Ha','My','Tu','Ma','Bi','Ut Ka','Ra','Da','Ch','Ko','B','Bid','Gu','D&N','Ra','Pu','San','Dhu','Jal','Gon','Gand','Alw','Jal','SAS','Gur','Pat','Hos','Lu','Pan','Kur','Jan']


column_names = ["2011","2012","2013","2014","2015"]



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


xpos = np.arange(0,lx,1)    
ypos = np.arange(0,ly,1)
xpos, ypos = np.meshgrid(xpos+0.5, ypos+0.5)

xpos = xpos.flatten()  
ypos = ypos.flatten()
zpos = np.ones(lx*ly)*1e-10

dx = 1. * np.ones_like(zpos)
dy = dx.copy()
dz = data.flatten()
print dz
cs = ['r', 'g', 'b', 'y', 'c'] * ly

values = np.linspace(0.2, 1., xpos.ravel().shape[0])
colors = cm.rainbow(values)

ticksx = np.arange(1, len(row_names)+1,1)
plt.xticks(ticksx, row_names)

z_data = np.random.rand(0,10)

ticksy = np.arange(0.5, len(column_names),1)
plt.yticks(ticksy, column_names)

#ax.tick_params(width=2, colors='r')



ax.w_xaxis.set_ticklabels(row_names,rotation=90,size=12)

ax.w_yaxis.set_ticklabels(column_names,rotation=40,size=12)

ax.bar3d(xpos,ypos,zpos, dx, dy, dz,   alpha=0.7, zsort='max',color=colors)
plt.ion()

plt.show()
raw_input(" " )

update: Reducing the "dx" value by 1 to 0.5, reduces the bar width but now its showing the huge padding between the bars which i want to remove completely.

enter image description here

Last Update: I have attached one more Image. I am expecting this kind of 3D bar graph with reduced bar width (its done), and minimised gap between bars.

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
jax
  • 3,927
  • 7
  • 41
  • 70
  • Question is identical to http://stackoverflow.com/questions/43954511/how-to-format-3d-bar-size-width-and-length-and-z-axis-value Please delete one of them. – ImportanceOfBeingErnest May 14 '17 at 15:07
  • [This question](http://stackoverflow.com/questions/43869751/change-bar-color-in-a-3d-bar-plot-in-matplotlib-based-on-value) seems to be related in that it uses the same data and already has the problem of bar widths solved. – ImportanceOfBeingErnest May 14 '17 at 15:15
  • deleted the previous one thanks – jax May 15 '17 at 07:33

1 Answers1

3

The call signature or bar3d is

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

where dx and dy are are the width and depth of bars. Changing the width is therefore done by supplying a different array or number to dx. E.g. to have a 0.5 wide bar, use

ax.bar3d(..., dx=0.5, ...)

If instead you want to change the width of the axis, you may look at this question. Implementing this solution here,

ax.bar3d(xpos,ypos,zpos, 1, 1, dz,   alpha=0.7, zsort='max',color=colors)
ax.get_proj = lambda: np.dot(Axes3D.get_proj(ax), np.diag([1,0.4, 1, 1]))

would give:

enter image description here

Community
  • 1
  • 1
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Hey,, its a great solution, worked when, I change the value by 0.5. But one problem still exist now its showing the reduced bar width but huge padding between two bar is there any method to reduced this padding size i tried with set_padding but its not working. I have updated one more image. thanks – jax May 15 '17 at 06:14
  • There is no padding! If `dx` is `1` The bar is one year wide, if `dx` is less than `1`, the bar is a fraction of a year wide. Can you explain exactly what the desired plot should look like? – ImportanceOfBeingErnest May 15 '17 at 07:44
  • As you can see in second image a hug gab between bars, i want to remove this gap, bar should be connected to each other without gap – jax May 15 '17 at 07:50
  • Yes to remove this gap, use the original case, where `dx = 1`. – ImportanceOfBeingErnest May 15 '17 at 07:53
  • But using original case bar size will increase again. I have posted one more, probably last image (created with power point cut and past) which shows what I am expecting from matplotlib. – jax May 15 '17 at 08:26
  • Can you explain this particular line in your answer Little bit more "ax.get_proj = lambda: np.dot(Axes3D.get_proj(ax), np.diag([1,0.4, 1, 1]))" – jax May 15 '17 at 09:27
  • There is a good explanation in [this answer](http://stackoverflow.com/a/30419243/4124317). – ImportanceOfBeingErnest May 15 '17 at 09:40