示例一
使用matplotlib.pyplot.bar函数绘制直方图。使用xlabel,ylabel设定x,y轴的名称。
plt.bar(stock.index,stock.volume,color='g',width=0.5)plt.grid(True)plt.xlabel('index')plt.ylabel('volume')plt.title('2018年5月21日~31日成交量')

设置字体大小
font1 = {'weight' : 'normal','size' : 25,}
plt.rcParams['font.size'] = 15figsize = (15,8)figure = plt.figure(figsize=figsize)#设定长宽建立窗口plt.bar(stock.index,stock.volume,color='g',width=0.5)plt.grid(True)plt.xlabel('index', font1)plt.ylabel('volume', font1)plt.title('2018年5月21日~31日成交量')
示例二
数据代码
import matplotlibimport matplotlib.pyplot as plt # 导入库# 直方图:电影年产量.pyimport pandas_def as pdef# 指定字体matplotlib.rcParams['font.sans-serif'] = ['SimHei']matplotlib.rcParams['font.family']='sans-serif'#解决负号'-'显示为方块的问题matplotlib.rcParams['axes.unicode_minus'] = False# 获取统计数据tj = pdef.movie_year_amount_tj()# print(type(tj))# 数据的转换 dataframe -> series -> list,年份、电影产量tj['year'] = tj.indexyears = tj['year'].dt.yearamounts = tj['release_date'].tolist()print(years)print(amounts)# exit()# 绘制直方图width = 0.35 # the width of the bars: can also be len(x) sequencefig, ax = plt.subplots()ax.bar(years, amounts, width)ax.set_ylabel('电影数量')ax.set_xlabel('年份')ax.set_title('电影年产量')ax.legend()plt.show()
release_date1915-12-31 19151916-12-31 19161917-12-31 19171918-12-31 19181919-12-31 19191920-12-31 19201921-12-31 19211922-12-31 19221923-12-31 19231924-12-31 19241925-12-31 19251926-12-31 19261927-12-31 19271928-12-31 19281929-12-31 19291930-12-31 19301931-12-31 19311932-12-31 19321933-12-31 19331934-12-31 19341935-12-31 19351936-12-31 19361937-12-31 19371938-12-31 19381939-12-31 19391940-12-31 19401941-12-31 19411942-12-31 19421943-12-31 19431944-12-31 19441945-12-31 19451946-12-31 19461947-12-31 19471948-12-31 19481949-12-31 19491950-12-31 19501951-12-31 19511952-12-31 19521953-12-31 19531954-12-31 19541955-12-31 19551956-12-31 19561957-12-31 19571958-12-31 19581959-12-31 19591960-12-31 19601961-12-31 19611962-12-31 19621963-12-31 19631964-12-31 19641965-12-31 19651966-12-31 19661967-12-31 19671968-12-31 19681969-12-31 19691970-12-31 19701971-12-31 19711972-12-31 19721973-12-31 19731974-12-31 19741975-12-31 19751976-12-31 19761977-12-31 19771978-12-31 19781979-12-31 19791980-12-31 19801981-12-31 19811982-12-31 19821983-12-31 19831984-12-31 19841985-12-31 19851986-12-31 19861987-12-31 19871988-12-31 19881989-12-31 19891990-12-31 19901991-12-31 19911992-12-31 19921993-12-31 19931994-12-31 19941995-12-31 19951996-12-31 19961997-12-31 19971998-12-31 19981999-12-31 19992000-12-31 20002001-12-31 20012002-12-31 20022003-12-31 20032004-12-31 20042005-12-31 20052006-12-31 20062007-12-31 20072008-12-31 20082009-12-31 20092010-12-31 20102011-12-31 20112012-12-31 20122013-12-31 20132014-12-31 20142015-12-31 20152016-12-31 20162017-12-31 20172018-12-31 20182019-12-31 20192020-12-31 2020Name: year, dtype: int64[1, 0, 0, 1, 0, 0, 2, 0, 1, 1, 3, 1, 4, 5, 0, 1, 4, 2, 0, 5, 4, 2, 6, 1, 8, 7, 5, 2, 3, 2, 6, 5, 5, 9, 5, 8, 6, 8, 13, 12, 13, 13, 13, 11, 14, 14, 13, 18, 18, 19, 13, 13, 16, 16, 13, 12, 13, 20, 27, 19, 17, 19, 15, 18, 31, 24, 28, 35, 42, 42, 56, 43, 63, 68, 65, 70, 81, 100, 115, 80, 103, 90, 116, 109, 135, 123, 143, 146, 167, 188, 201, 223, 259, 246, 288, 298, 304, 315, 348, 353, 431, 478, 474, 467, 601, 185]

