matplot 画条形图

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
plt.show()
America/New_York       1251
Unknown                 521
America/Chicago         400
America/Los_Angeles     382
America/Denver          191
Missing                 120
Europe/London            74
Asia/Tokyo               37
Pacific/Honolulu         36
Europe/Madrid            35
Name: tz, dtype: int64
x=range(len(tz_counts.values[:10]))
plt.barh(x,tz_counts.values[:10])
#barh表示画水平的条形,bar 表示画垂直的。同理改标签的时候也要分plt.xticks;plt.yticks
#注意:这里不要以为x是数据对应的index的名称,x是数!!什么数呢?条形的个数的排列数
#报错unsupported operand type(s) for -: 'str' and 'float',往往是把x 当成所要显示的bar对应的名称。
#正确做法x先写成bar的个数的形式,即:第1个bar,第2个bar....,每个bar对应的都是数字。故range(条形的个数)
plt.yticks([index  for index in x],list(tz_counts.index))#再把对应的1,2,..,改成正确的标签样式。
plt.show()

如果对条形图要求不太高,不必用matplot 画,感觉麻烦,可以用pandas 自带的去画。

tz_counts[:10].plot(kind='barh',rot=0)


猜你喜欢

转载自blog.csdn.net/lishangyin88/article/details/80672032