来自熊猫的 Python 直方图

我正在尝试从该数据集中制作直方图:

http://img2.mukewang.com/63e1c0100001215615070548.jpg

我想要这样的图表:

http://img2.mukewang.com/63e1c01a0001760710670802.jpg

我写了这段代码:


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

运行后,我得到这个图表:

http://img2.mukewang.com/63e1c0250001f0b707630628.jpg

谁能帮我解决这个问题?



动漫人物
浏览 92回答 2
2回答

子衿沉夜

使用字典定义行,标题行作为索引:import pandas as pdimport matplotlib.pyplot as plteighty = [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",&nbsp; &nbsp; &nbsp; "850-700",&nbsp; &nbsp; &nbsp; "700-425",&nbsp; &nbsp; &nbsp; "425-250",&nbsp; &nbsp; &nbsp; "<250",&nbsp; &nbsp; &nbsp; "MTOT"&nbsp; &nbsp; &nbsp; ]df = pd.DataFrame({'80': eighty,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'76.8': seventy_six},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index=LP)ax = df.plot.bar(rot=0)plt.show()退货:

守候你守候我

您可以使用ploty来实现这一点。您可以通过以下方式安装pip install plotly#sample dfimport pandas as pddf=pd.DataFrame({&nbsp; &nbsp; 'lp':[70,85],&nbsp; &nbsp; '>850':[34,39],&nbsp; &nbsp; '700-850':[38,39],&nbsp; &nbsp; '425-700':[13,34],&nbsp; &nbsp; '250-425':[16,2],&nbsp; &nbsp; '<250':[25,10]&nbsp; &nbsp;&nbsp;})#reshape the dfdf=df.melt(id_vars=['lp'])&nbsp;&nbsp;&nbsp;#use plotly libraryimport plotly.graph_objects as gofig = go.Figure(data=[&nbsp; &nbsp; go.Bar(name='70', x=df[df['lp']==70]['variable'], y=df[df['lp']==70]['value']),&nbsp; &nbsp; go.Bar(name='85', x=df[df['lp']==85]['variable'], y=df[df['lp']==85]['value']),])# Change the bar modefig.update_layout(barmode='group')fig.show()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python