英文:
3d bar chart with matplotlib using DataFrames
问题
我有以下的DataFrame:
j k z
16 36 3.34541e-07
19 40 4.4038e-07
21 52 1.24715e-06
24 41 9.13244e-07
25 37 6.33979e-07
25 45 5.89413e-07
26 31 7.83958e-07
26 36 6.24651e-07
26 42 5.44847e-07
26 47 4.77851e-07
27 30 8.50074e-07
27 35 5.51727e-07
27 36 1.2272e-06
27 38 5.77199e-07
我尝试使用plotly
和matplotlib
,但结果非常不理想。
英文:
I have the following DataFrame:
j k z
16 36 3.34541e-07
19 40 4.4038e-07
21 52 1.24715e-06
24 41 9.13244e-07
25 37 6.33979e-07
25 45 5.89413e-07
26 31 7.83958e-07
26 36 6.24651e-07
26 42 5.44847e-07
26 47 4.77851e-07
27 30 8.50074e-07
27 35 5.51727e-07
27 36 1.2272e-06
27 38 5.77199e-07
And I am not finding any solutions to display this data in a nice and simple way with a bar chart where j k are the bar indices and z controls the height.
I tried with plotly
, matplotlib
, and had very poor results
答案1
得分: 1
# 使用matplotlib:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame([
[16, 36, 3.34541e-07],
[19, 40, 4.4038e-07],
[21, 52, 1.24715e-06],
[24, 41, 9.13244e-07],
[25, 37, 6.33979e-07],
[25, 45, 5.89413e-07],
[26, 31, 7.83958e-07],
[26, 36, 6.24651e-07],
[26, 42, 5.44847e-07],
[26, 47, 4.77851e-07],
[27, 30, 8.50074e-07],
[27, 35, 5.51727e-07],
[27, 36, 1.2272e-06],
[27, 38, 5.77199e-07]
])
df.columns = ['j', 'k', 'z']
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.bar3d(df['j'], df['k'], np.zeros_like(df['k']), dx=0.5, dy=1, dz=df['z'])
ax.set_xlabel('j')
ax.set_ylabel('k')
plt.show()
英文:
Using matplotlib:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame([
[16, 36, 3.34541e-07],
[19, 40, 4.4038e-07],
[21, 52, 1.24715e-06],
[24, 41, 9.13244e-07],
[25, 37, 6.33979e-07],
[25, 45, 5.89413e-07],
[26, 31, 7.83958e-07],
[26, 36, 6.24651e-07],
[26, 42, 5.44847e-07],
[26, 47, 4.77851e-07],
[27, 30, 8.50074e-07],
[27, 35, 5.51727e-07],
[27, 36, 1.2272e-06],
[27, 38, 5.77199e-07]
])
df.columns = ['j', 'k', 'z']
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.bar3d(df['j'], df['k'], np.zeros_like(df['k']), dx=0.5, dy=1, dz=df['z'])
ax.set_xlabel('j')
ax.set_ylabel('k')
plt.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论