示例一
使用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'] = 15
figsize = (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 matplotlib
import matplotlib.pyplot as plt # 导入库
# 直方图:电影年产量.py
import 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.index
years = tj['year'].dt.year
amounts = tj['release_date'].tolist()
print(years)
print(amounts)
# exit()
# 绘制直方图
width = 0.35 # the width of the bars: can also be len(x) sequence
fig, ax = plt.subplots()
ax.bar(years, amounts, width)
ax.set_ylabel('电影数量')
ax.set_xlabel('年份')
ax.set_title('电影年产量')
ax.legend()
plt.show()
release_date
1915-12-31 1915
1916-12-31 1916
1917-12-31 1917
1918-12-31 1918
1919-12-31 1919
1920-12-31 1920
1921-12-31 1921
1922-12-31 1922
1923-12-31 1923
1924-12-31 1924
1925-12-31 1925
1926-12-31 1926
1927-12-31 1927
1928-12-31 1928
1929-12-31 1929
1930-12-31 1930
1931-12-31 1931
1932-12-31 1932
1933-12-31 1933
1934-12-31 1934
1935-12-31 1935
1936-12-31 1936
1937-12-31 1937
1938-12-31 1938
1939-12-31 1939
1940-12-31 1940
1941-12-31 1941
1942-12-31 1942
1943-12-31 1943
1944-12-31 1944
1945-12-31 1945
1946-12-31 1946
1947-12-31 1947
1948-12-31 1948
1949-12-31 1949
1950-12-31 1950
1951-12-31 1951
1952-12-31 1952
1953-12-31 1953
1954-12-31 1954
1955-12-31 1955
1956-12-31 1956
1957-12-31 1957
1958-12-31 1958
1959-12-31 1959
1960-12-31 1960
1961-12-31 1961
1962-12-31 1962
1963-12-31 1963
1964-12-31 1964
1965-12-31 1965
1966-12-31 1966
1967-12-31 1967
1968-12-31 1968
1969-12-31 1969
1970-12-31 1970
1971-12-31 1971
1972-12-31 1972
1973-12-31 1973
1974-12-31 1974
1975-12-31 1975
1976-12-31 1976
1977-12-31 1977
1978-12-31 1978
1979-12-31 1979
1980-12-31 1980
1981-12-31 1981
1982-12-31 1982
1983-12-31 1983
1984-12-31 1984
1985-12-31 1985
1986-12-31 1986
1987-12-31 1987
1988-12-31 1988
1989-12-31 1989
1990-12-31 1990
1991-12-31 1991
1992-12-31 1992
1993-12-31 1993
1994-12-31 1994
1995-12-31 1995
1996-12-31 1996
1997-12-31 1997
1998-12-31 1998
1999-12-31 1999
2000-12-31 2000
2001-12-31 2001
2002-12-31 2002
2003-12-31 2003
2004-12-31 2004
2005-12-31 2005
2006-12-31 2006
2007-12-31 2007
2008-12-31 2008
2009-12-31 2009
2010-12-31 2010
2011-12-31 2011
2012-12-31 2012
2013-12-31 2013
2014-12-31 2014
2015-12-31 2015
2016-12-31 2016
2017-12-31 2017
2018-12-31 2018
2019-12-31 2019
2020-12-31 2020
Name: 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]