2 回答
TA贡献1828条经验 获得超3个赞
使用字典定义行,标题行作为索引:
import pandas as pd
import matplotlib.pyplot as plt
eighty = [47.83, 5.24, 18.74, 22.22, 34.92, 137.75]
seventy_six = [61.47, 6.18, 54.37, 3.22, 16.52, 156.38]
LP = [">850",
"850-700",
"700-425",
"425-250",
"<250",
"MTOT"
]
df = pd.DataFrame({'80': eighty,
'76.8': seventy_six},
index=LP)
ax = df.plot.bar(rot=0)
plt.show()
退货:
TA贡献1802条经验 获得超10个赞
您可以使用ploty来实现这一点。您可以通过以下方式安装pip install plotly
#sample df
import pandas as pd
df=pd.DataFrame({
'lp':[70,85],
'>850':[34,39],
'700-850':[38,39],
'425-700':[13,34],
'250-425':[16,2],
'<250':[25,10]
})
#reshape the df
df=df.melt(id_vars=['lp'])
#use plotly library
import plotly.graph_objects as go
fig = go.Figure(data=[
go.Bar(name='70', x=df[df['lp']==70]['variable'], y=df[df['lp']==70]['value']),
go.Bar(name='85', x=df[df['lp']==85]['variable'], y=df[df['lp']==85]['value']),
])
# Change the bar mode
fig.update_layout(barmode='group')
fig.show()
添加回答
举报