datadata.csv(Pandas DataFrame)
Item Action Cost
Item1 UK 100
Item2 USA 425
Item3 China 4587
Item4 USA 1234
Item5 China 1705
Item6 USA 870
Item7 China 1578
Item8 UK 3721
Item9 UK 4107
Item10 UK 1527
Item11 USA 770
My code is...
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.cm as cm
# CSV 파일 읽기
data = pd.read_csv('datadata.csv')
xpos=np.arange(data.shape[0])
ypos=np.arange(data.shape[1])
zpos=np.zeros(data.shape).flatten()
dx = 0.5 * np.ones_like(zpos)
dy= 0.1 * np.ones_like(zpos)
dz = row['Cost']
# bar3d
fig = plt.figure(figsize=(12,9))
ax = fig.add_subplot(111, projection='3d')
# color
values = np.linspace(0.2, 1., data.shape[0])
cmaps = [cm.Blues, cm.Reds, cm.Greens]
colors = np.hstack([c(values) for c in cmaps]).reshape(-1, 4)
ax.bar3d(xposM.ravel(),yposM.ravel(),zpos,dx,dy,dz,color=colors, alpha=0.5)
# x, y, z Lable
ax.set_xlabel('Item')
ax.set_ylabel('Action')
ax.set_zlabel('Cost')
# x, y, z tick
ticks_x = np.arange(0.2, 11, 1)
ax.set_xticks(ticks_x)
ticks_y=np.arange(0.6,3,1)
ax.set_yticks(ticks_y)
ax.set_xticklabels(['Item1', 'Item2', 'Item3', 'Item4', 'Item5', 'Item6', 'Item7',
'Item8', 'Item9', 'Item10', 'Item11'])
ax.set_xlim(0,11)
ax.set_yticklabels(['UK', 'USA', 'China'])
ax.set_ylim(0,3)
ax.set_zlim(0,4000)
plt.show()
What I got...
The z-axis data is not displayed accurately. How Can I draw bar3d with dataframe.
What I want to draw
It looks like there are multiple issues in your code. For example, the names "row", "xposM", "yposM" are not defined. Anyway, you need to extract unique action names from your data file and build the respective coordinate matrices for bar3d based on the number of those unique action names and the number of item names:
Voilà: