我正在尝试根据此数据集创建直方图:
我想要一个这样的图:
我写了这段代码:
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('Data_Istogramma.csv', sep=';')
plt.hist(x =(data.iloc[0,1:6],data.iloc[1,1:6]),bins = 5,edgecolor = 'black',label =['80%','76.8%'])
plt.show()
一旦运行,我会得到这张图:
有人可以帮我吗?
答案 0 :(得分:0)
您可以使用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()
答案 1 :(得分:0)
使用字典定义行,并将标题行作为索引:
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()
返回: