2

I have three lists as mentioned below.

xx =  ['TT', 'AN', 'AP', 'AR', 'AS', 'BR', 'CH', 'CT', 'DN', 'DD', 'DL', 'GA', 'GJ', 'HR', 'HP', 'JK', 'JH', 'KA', 'KL', 'LA', 'LD', 'MP', 'MH', 'MN', 'ML', 'MZ', 'NL', 'OR', 'PY', 'PB', 'RJ', 'SK', 'TN', 'TG', 'TR', 'UP', 'UT', 'WB']
yy = [datetime.date(2020, 5, 27), datetime.date(2020, 5, 28), datetime.date(2020, 5, 29), datetime.date(2020, 5, 30)]
zz = [7246, 0, 134, 0, 101, 68, 1, 4, 0, 0, 792, 1, 376, 76, 26, 162, 7, 135, 40, 1, 0, 237, 2190, 5, 0, 0, 5, 76, 0, 33, 280, 0, 817, 107, 10, 267, 69, 183, 7254, 0, 128, 1, 97, 149, 10, 34, 0, 0, 1024, 1, 367, 123, 8, 115, 6, 115, 85, 20, 0, 192, 2598, 11, 1, 0, 9, 67, 4, 19, 251, 0, 827, 117, 2, 179, 31, 344, 8138, 0, 85, 0, 177, 174, 0, 17, 0, 0, 1105, 0, 372, 217, 14, 128, 82, 248, 62, 0, 0, 192, 2682, 4, 6, 0, 7, 63, 4, 39, 298, 0, 874, 169, 10, 275, 216, 277, 8304, 0, 131, 1, 128, 206, 0, 32, 0, 0, 1163, 1, 412, 202, 18, 177, 42, 141, 58, 3, 0, 246, 2940, 3, 0, 0, 11, 96, 0, 36, 252, 0, 938, 74, 17, 256, 33, 317]

I want to plot bar3d plot using matplotlib in python such that x axis will be xx, y axis will be yy to plot **zz*, i.e. date wise plotting. Following tutorials, I tried

dx = np.ones(len(xx))
dy = np.ones(len(yy))
dz = np.ones(len(zz))   # doubt
ax.bar3d(xx, yy, zz, dx, dy, dz, shade=True)

Obviously, I got errors

  ax.bar3d(xx, yy, zz, dx=dx, dy=dy, dz=dz, shade=True)
File "/home/vega/anaconda3/envs/tf01/lib/python3.7/sitepackages/mpl_toolkits/mplot3d/axes3d.py", line 2365, in bar3d
  np.atleast_1d(x), y, z, dx, dy, dz)
File "<__array_function__ internals>", line 6, in broadcast_arrays
File "/home/vega/anaconda3/envs/tf01/lib/python3.7/sitepackages/numpy/lib/stride_tricks.py", line 264, in broadcast_arrays
  shape = _broadcast_shape(*args)
File "/home/vega/anaconda3/envs/tf01/lib/python3.7/sitepackages/numpy/lib/stride_tricks.py", line 191, in _broadcast_shape
  b = np.broadcast(*args[:32])
ValueError: shape mismatch: objects cannot be broadcast to a single shape

I think I did not understand proper formulation of tuples for bar3D plot. I request any handle to resolve. I scanned earlier questions and hence I believe it is not a repeat question.

1 Answer 1

2

You are currently doing it wrong. Just follow the official example here and you can do something like this. You need to create a 2-dimensional mesh grid of x and y points for each of your bar. Moreover, to create the meshgrids, you have to use numbers and later reset the tick labels to your original strings

x = np.arange(len(xx))
y = np.arange(len(yy))

_xx, _yy = np.meshgrid(x, y)
XX, YY = _xx.ravel(), _yy.ravel()

bottom = np.zeros_like(zz)
dx = np.ones(len(XX))
dy = np.ones(len(YY))

ax.bar3d(XX, YY, bottom, dx, dy, zz, shade=True)

ax.set_xticks(range(0, 36, 2))
ax.set_xticklabels(xx[::2])

ax.set_yticks(np.arange(4) + 0.5)
ax.set_yticklabels(yy);

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for the help. Actually I was suddenly confused on meshgrid generation and also on getting dx, dy for date and strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.