为了账号安全,请及时绑定邮箱和手机立即绑定

使用 x 和 y 的数据框使用 Matplotlib 生成直方图

使用 x 和 y 的数据框使用 Matplotlib 生成直方图

杨魅力 2022-12-27 15:19:25
我正在用 Matplotlib 生成一个简单的折线图,这是我的代码:fig = plt.figure(facecolor='#131722',dpi=155, figsize=(8, 4))ax1 = plt.subplot2grid((1,2), (0,0), facecolor='#131722')for x in OrderedList:    rate_buy = []    total_buy = []    for y in x['data']['bids']:        rate_buy.append(y[0])        total_buy.append(y[1])    rBuys = pd.DataFrame({'buy': rate_buy})    tBuys = pd.DataFrame({'total': total_buy})    ax1.plot(rBuys.buy, tBuys.total, color='#0400ff', linewidth=0.5, alpha=1)    ax1.fill_between(rBuys.buy, 0, tBuys.total, facecolor='#0400ff', alpha=1)这给了我以下输出:这是我在数据框中使用的数据:     buy0   96111   96102   96093   96084   96075   96066   96057   96048   96039   960210  960111  960012  9599        total0    3.0336611    3.2957532    3.5998133   22.3057654   22.9874765   30.9751456   39.4928457   42.8285808   46.6777089   49.53374010  50.92584011  61.39624312  61.921523我想获得相同的图像输出,但使用直方图图表或类似的任何东西,其中从数据帧检索 y 轴上的列高度,从total数据帧检索 x 轴位置buy。所以第一个元素将有位置x=9611和y=3.033661有可能用 Matplotlib 做到这一点吗?我尝试使用hist,但它不允许我同时设置 x 轴和 y 轴
查看完整描述

1 回答

?
天涯尽头无女友

TA贡献1831条经验 获得超9个赞

Pandas 也使用 matplotlib,一旦你有了数据框,API 就非常简单。

这是一个例子。

d =    {

         'buy':[

             9611,

             9610,

             9609,

             9608,

             9607,

             9606,

             9605,

             9604,

             9603,

             9602,

             9601,

             9600,

             9599

         ],

         'total':[

             3.033661,

             3.295753,

             3.599813,

             22.305765,

             22.987476,

             30.975145,

             39.492845,

             42.828580,

             46.677708,

             49.533740,

             50.925840,

             61.396243,

             61.921523

         ]

     }


df = pd.DataFrame(d)


df = df.sort_values(by=['buy']) #remember to sort your x values!

df.plot(kind='bar', x='buy', y='total', width=1)

plt.show()

//img1.sycdn.imooc.com//63aa9ca60001721805920443.jpg

查看完整回答
反对 回复 2022-12-27
  • 1 回答
  • 0 关注
  • 97 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号