0

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.

user2820579
  • 3,261
  • 7
  • 30
  • 45

1 Answers1

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()

Result: enter image description here

yatinsingla
  • 434
  • 6
  • 13