Python——使用matplotlib绘制各种柱状图

Python——使用matplotlib绘制柱状图

转载自:https://blog.csdn.net/qq_29721419/article/details/71638912

1、基本柱状图

          首先要安装matplotlib(http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot) 可以使用pip命令直接安装

   
   
  1. # -*- coding: utf-8 -*-
  2. import matplotlib.pyplot as plt
  3. num_list = [ 1.5, 0.6, 7.8, 6]
  4. plt.bar(range(len(num_list)), num_list)
  5. plt.show()

2、设置颜色

          

    
    
  1. # -*- coding: utf-8 -*-
  2. import matplotlib.pyplot as plt
  3. num_list = [ 1.5, 0.6, 7.8, 6]
  4. plt.bar(range(len(num_list)), num_list,fc= ‘r’)
  5. plt.show()


    
    
  1. # -*- coding: utf -8 -*-
  2. import matplotlib.pyplot as plt
  3. num_list = [ 1.5, 0.6, 7.8, 6]
  4. plt.bar(range(len(num_list)), num_list,color= ‘rgb’)
  5. plt.show()

3、设置标签


    
    
  1. # -*- coding: utf-8 -*-
  2. import matplotlib.pyplot as plt
  3. name_list = [ ‘Monday’, ‘Tuesday’, ‘Friday’, ‘Sunday’]
  4. num_list = [ 1.5, 0.6, 7.8, 6]
  5. plt.bar(range(len(num_list)), num_list,color= ‘rgb’,tick_label=name_list)
  6. plt.show()

4、堆叠柱状图


     
     
  1. # -*- coding: utf-8 -*-
  2. import matplotlib.pyplot as plt
  3. name_list = [ ‘Monday’, ‘Tuesday’, ‘Friday’, ‘Sunday’]
  4. num_list = [ 1.5, 0.6, 7.8, 6]
  5. num_list1 = [ 1, 2, 3, 1]
  6. plt.bar(range(len(num_list)), num_list, label= ‘boy’,fc = ‘y’)
  7. plt.bar(range(len(num_list)), num_list1, bottom=num_list, label= ‘girl’,tick_label = name_list,fc = ‘r’)
  8. plt.legend()
  9. plt.show()

5、并列柱状图


     
     
  1. # -*- coding: utf-8 -*-
  2. import matplotlib.pyplot as plt
  3. name_list = [ ‘Monday’, ‘Tuesday’, ‘Friday’, ‘Sunday’]
  4. num_list = [ 1.5, 0.6, 7.8, 6]
  5. num_list1 = [ 1, 2, 3, 1]
  6. x =list(range(len(num_list)))
  7. total_width, n = 0.8, 2
  8. width = total_width / n
  9. plt.bar(x, num_list, width=width, label= ‘boy’,fc = ‘y’)
  10. for i in range(len(x)):
  11. x[i] = x[i] + width
  12. plt.bar(x, num_list1, width=width, label= ‘girl’,tick_label = name_list,fc = ‘r’)
  13. plt.legend()
  14. plt.show()

6、条形柱状图


     
     
  1. # -*- coding: utf-8 -*-
  2. import matplotlib.pyplot as plt
  3. name_list = [ ‘Monday’, ‘Tuesday’, ‘Friday’, ‘Sunday’]
  4. num_list = [ 1.5, 0.6, 7.8, 6]
  5. plt.barh(range(len(num_list)), num_list,tick_label = name_list)
  6. plt.show()








猜你喜欢

转载自blog.csdn.net/weixin_38285131/article/details/82698252