示例一

使用matplotlib.pyplot.bar函数绘制直方图。使用xlabel,ylabel设定x,y轴的名称。

  1. plt.bar(stock.index,stock.volume,color='g',width=0.5)
  2. plt.grid(True)
  3. plt.xlabel('index')
  4. plt.ylabel('volume')
  5. plt.title('2018年5月21日~31日成交量')

image.png
设置字体大小

  1. font1 = {
  2. 'weight' : 'normal',
  3. 'size' : 25,
  4. }
  1. plt.rcParams['font.size'] = 15
  2. figsize = (15,8)
  3. figure = plt.figure(figsize=figsize)#设定长宽建立窗口
  4. plt.bar(stock.index,stock.volume,color='g',width=0.5)
  5. plt.grid(True)
  6. plt.xlabel('index', font1)
  7. plt.ylabel('volume', font1)
  8. plt.title('2018年5月21日~31日成交量')

image.png

示例二

数据代码

  1. import matplotlib
  2. import matplotlib.pyplot as plt # 导入库
  3. # 直方图:电影年产量.py
  4. import pandas_def as pdef
  5. # 指定字体
  6. matplotlib.rcParams['font.sans-serif'] = ['SimHei']
  7. matplotlib.rcParams['font.family']='sans-serif'
  8. #解决负号'-'显示为方块的问题
  9. matplotlib.rcParams['axes.unicode_minus'] = False
  10. # 获取统计数据
  11. tj = pdef.movie_year_amount_tj()
  12. # print(type(tj))
  13. # 数据的转换 dataframe -> series -> list,年份、电影产量
  14. tj['year'] = tj.index
  15. years = tj['year'].dt.year
  16. amounts = tj['release_date'].tolist()
  17. print(years)
  18. print(amounts)
  19. # exit()
  20. # 绘制直方图
  21. width = 0.35 # the width of the bars: can also be len(x) sequence
  22. fig, ax = plt.subplots()
  23. ax.bar(years, amounts, width)
  24. ax.set_ylabel('电影数量')
  25. ax.set_xlabel('年份')
  26. ax.set_title('电影年产量')
  27. ax.legend()
  28. plt.show()
  1. release_date
  2. 1915-12-31 1915
  3. 1916-12-31 1916
  4. 1917-12-31 1917
  5. 1918-12-31 1918
  6. 1919-12-31 1919
  7. 1920-12-31 1920
  8. 1921-12-31 1921
  9. 1922-12-31 1922
  10. 1923-12-31 1923
  11. 1924-12-31 1924
  12. 1925-12-31 1925
  13. 1926-12-31 1926
  14. 1927-12-31 1927
  15. 1928-12-31 1928
  16. 1929-12-31 1929
  17. 1930-12-31 1930
  18. 1931-12-31 1931
  19. 1932-12-31 1932
  20. 1933-12-31 1933
  21. 1934-12-31 1934
  22. 1935-12-31 1935
  23. 1936-12-31 1936
  24. 1937-12-31 1937
  25. 1938-12-31 1938
  26. 1939-12-31 1939
  27. 1940-12-31 1940
  28. 1941-12-31 1941
  29. 1942-12-31 1942
  30. 1943-12-31 1943
  31. 1944-12-31 1944
  32. 1945-12-31 1945
  33. 1946-12-31 1946
  34. 1947-12-31 1947
  35. 1948-12-31 1948
  36. 1949-12-31 1949
  37. 1950-12-31 1950
  38. 1951-12-31 1951
  39. 1952-12-31 1952
  40. 1953-12-31 1953
  41. 1954-12-31 1954
  42. 1955-12-31 1955
  43. 1956-12-31 1956
  44. 1957-12-31 1957
  45. 1958-12-31 1958
  46. 1959-12-31 1959
  47. 1960-12-31 1960
  48. 1961-12-31 1961
  49. 1962-12-31 1962
  50. 1963-12-31 1963
  51. 1964-12-31 1964
  52. 1965-12-31 1965
  53. 1966-12-31 1966
  54. 1967-12-31 1967
  55. 1968-12-31 1968
  56. 1969-12-31 1969
  57. 1970-12-31 1970
  58. 1971-12-31 1971
  59. 1972-12-31 1972
  60. 1973-12-31 1973
  61. 1974-12-31 1974
  62. 1975-12-31 1975
  63. 1976-12-31 1976
  64. 1977-12-31 1977
  65. 1978-12-31 1978
  66. 1979-12-31 1979
  67. 1980-12-31 1980
  68. 1981-12-31 1981
  69. 1982-12-31 1982
  70. 1983-12-31 1983
  71. 1984-12-31 1984
  72. 1985-12-31 1985
  73. 1986-12-31 1986
  74. 1987-12-31 1987
  75. 1988-12-31 1988
  76. 1989-12-31 1989
  77. 1990-12-31 1990
  78. 1991-12-31 1991
  79. 1992-12-31 1992
  80. 1993-12-31 1993
  81. 1994-12-31 1994
  82. 1995-12-31 1995
  83. 1996-12-31 1996
  84. 1997-12-31 1997
  85. 1998-12-31 1998
  86. 1999-12-31 1999
  87. 2000-12-31 2000
  88. 2001-12-31 2001
  89. 2002-12-31 2002
  90. 2003-12-31 2003
  91. 2004-12-31 2004
  92. 2005-12-31 2005
  93. 2006-12-31 2006
  94. 2007-12-31 2007
  95. 2008-12-31 2008
  96. 2009-12-31 2009
  97. 2010-12-31 2010
  98. 2011-12-31 2011
  99. 2012-12-31 2012
  100. 2013-12-31 2013
  101. 2014-12-31 2014
  102. 2015-12-31 2015
  103. 2016-12-31 2016
  104. 2017-12-31 2017
  105. 2018-12-31 2018
  106. 2019-12-31 2019
  107. 2020-12-31 2020
  108. Name: year, dtype: int64
  109. [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]

image.png