matplotlib不显示图形栏 [英] matplotlib not displaying the graph bars

查看:77
本文介绍了matplotlib不显示图形栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将绘图显示在图表上,但由于某种原因它没有显示任何内容.我已经正确导入了matplotlib库,正在正确地从函数中获取值,并且当我将这些值传递给要在其中显示绘图的函数时,它将显示空白图像.为了表明我得到了正确的值,我使用了 print 和绘图命令,这些值正在打印,但不是 plot 这是代码.我能够使用

I'm trying to get the plot to show on a graph but it does not show anything for some reason. I have properly imported the matplotlib library, I am properly getting the values from the function and when I pass those values to the function where I want the plot to be shown it displays blank image. To show that I am getting the correct values I have used print along with plotting commands the values are getting printed but not the plot Here's the code. I was able to get the plot correct using

def GetCounts(data):
    return (data['Sex'].value_counts())

def Get_Plot(Points):
    _x1 = Points[0]
    _x2 = Points[1]
    _y = (_x1 + _x2) - 200
    print('male' + ' ' + str(_x1) + '\n','female' + ' '+ str(_x2), _y)
    plt.bar(height = _x1, tick_label = 'Male', left = _x1)
    plt.xlabel('Counts of people according to Sex')
    plt.ylabel('Number of people')
    plt.show()
Counts   = GetCounts(titanic)
Get_Plot(Counts)

我正在尝试在男性"和女性"中放置2条,我不确定如何才能做到.使用上面的代码,我只能输入其中之一.请提前帮助谢谢.

I'm trying to get 2 bars placed in there 'Male' and 'Female' and I not sure how I will be able to. and with the code above I am only able to put only one of it. Please help thanks in advance.

推荐答案

您可能想重新访问plt.bar 说明文档

You may want to revisit the plt.bar documentation where it says

pyplot.bar(左侧,高度,宽度= 0.8,底部=无,按住=无,数据=无,**扭曲)
[...]
left :标量序列条形左侧的 x 坐标
height : 标量或标量序列条形的高度

pyplot.bar(left, height, width=0.8, bottom=None, hold=None, data=None, **kwargs)
[...]
left : sequence of scalars the x coordinates of the left sides of the bars
height : scalar or sequence of scalars the height(s) of the bars

因此,您可以将条形图放置在个性化的 0 1 上,其高度将由 Points

You may thus position the bars at the indizes 0 and 1 and their height will be given by Points

plt.bar(range(len(Points)),Points)
plt.xticks(range(len(Points)), Points.index)

这篇关于matplotlib不显示图形栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆