The standard way in which I create bar plots in matplotlib is using ax.bar3d
. This has the disadvantage of returning block of solid color. Does anyone know how to attach a gradient color to every bar? I am think in reproducing Fig. 1 from https://arxiv.org/pdf/1706.09289.pdf.
Asked
Active
Viewed 445 times
0

user2820579
- 3,261
- 7
- 30
- 45
1 Answers
0
Try this:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(18,12))
ax = fig.add_subplot(111, projection='3d')
x_data, y_data = np.meshgrid(np.arange(5),np.arange(3))
z_data = np.random.rand(3,5)
colors = ['r','g','b'] # colors for every line of y
# plot colored 3d bars
for i in xrange(3): # cycle though y
# I multiply one color by len of x (it is 5) to set one color for y line
ax.bar3d(x_data[i], y_data[i], z_data[i], 1, 1, z_data[i], alpha=0.1, color=colors[i]*5)
# or use random colors
# ax.bar3d(x_data[i], y_data[i], z_data[i], 1, 1, z_data[i], alpha=0.1, color=[np.random.rand(3,1),]*5)
plt.show()

yatinsingla
- 434
- 6
- 13
-
This is a suggestion already made, but this is not what I am looking for. I want to generate something like this https://i.stack.imgur.com/DOz6r.png. – user2820579 Oct 13 '17 at 17:16
-
Are you using ax.bar3d function in your code? – yatinsingla Oct 13 '17 at 17:27
-
This can help you. https://stackoverflow.com/questions/43869751/change-bar-color-in-a-3d-bar-plot-in-matplotlib-based-on-value – yatinsingla Oct 13 '17 at 17:30