简体   繁体   中英

matplotlib not displaying the graph bars

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)

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.

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

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM