matplotlib不显示图形栏

编程入门 行业动态 更新时间:2024-10-20 01:36:00
本文介绍了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不显示图形栏

本文发布于:2023-07-18 12:51:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1144952.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图形   matplotlib

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!