自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python數(shù)據(jù)分析條形圖的各種繪制方式

大數(shù)據(jù) 數(shù)據(jù)分析 后端
Python數(shù)據(jù)分析條形圖的各種繪制方式,包括:簡單垂直條形圖、簡單水平條形圖、水平交錯(cuò)條形圖、垂直堆疊條形圖等。

簡單垂直條形圖

  1. GDP = [12406.8, 13908.57, 9386.87, 9143.64] 
  2.  
  3. # 繪圖 
  4. plt.bar(range(4), GDP, align='center', color='steelblue', alpha=0.8) 
  5. # 添加軸標(biāo)簽 
  6. plt.ylabel('GDP'
  7. # 添加標(biāo)題 
  8. plt.title('GDP'
  9. # 添加刻度標(biāo)簽 
  10. plt.xticks(range(4), ['beijing''shanghai''tianjing''chongjing']) 
  11. # 設(shè)置Y軸的刻度范圍 
  12. plt.ylim([5000, 15000]) 
  13.  
  14. # 為每個(gè)條形圖添加數(shù)值標(biāo)簽 
  15. for x, y in enumerate(GDP): 
  16.     plt.text(x, y + 100, '%s' % round(y, 1), ha='center')  
  17.  
  18. plt.show() 

 python數(shù)據(jù)分析條形圖的各種繪制方式

簡單水平條形圖

  1. # 導(dǎo)入繪圖模塊 
  2. import matplotlib.pyplot as plt 
  3.  
  4. # 構(gòu)建數(shù)據(jù) 
  5. price = [39.5, 39.9, 45.4, 38.9, 33.34] 
  6.  
  7. # 繪圖 
  8. plt.barh(range(5), price, align='center', color='steelblue', alpha=0.8) 
  9. # 添加軸標(biāo)簽 
  10. plt.xlabel('price'
  11. # 添加標(biāo)題 
  12. plt.title('Books at different prices'
  13. # 添加刻度標(biāo)簽 
  14. plt.yticks(range(5), ['Amazon''Dangdang''China Books Network''Jingdong''Tianmao']) 
  15. # 設(shè)置Y軸的刻度范圍 
  16. plt.xlim([32, 47]) 
  17.  
  18. # 為每個(gè)條形圖添加數(shù)值標(biāo)簽 
  19. for x, y in enumerate(price): 
  20.     plt.text(y + 0.1, x, '%s' % y, va='center'
  21.  
  22. plt.savefig('foo.png'
  23. # 顯示圖形     
  24. plt.show() 

 python數(shù)據(jù)分析條形圖的各種繪制方式

水平交錯(cuò)條形圖

  1. # 導(dǎo)入繪圖模塊 
  2. import matplotlib.pyplot as plt 
  3. import numpy as np 
  4.  
  5. Y2016 = [15600, 12700, 11300, 4270, 3620] 
  6. Y2017 = [17400, 14800, 12000, 5200, 4020] 
  7. labels = ['Beijing''Shanghai''Hong Kong''Shenzhen''Guangzhou'
  8. bar_width = 0.45 
  9.  
  10. # 繪圖 
  11. plt.bar(np.arange(5), Y2016, label='2016', color='steelblue', alpha=0.8, width=bar_width) 
  12. plt.bar(np.arange(5) + bar_width, Y2017, label='2017', color='indianred', alpha=0.8, width=bar_width) 
  13. # 添加軸標(biāo)簽 
  14. plt.xlabel('Top5 City'
  15. plt.ylabel('Number of households'
  16. # 添加標(biāo)題 
  17. plt.title('Billionaires Top5 Cities'
  18. # 添加刻度標(biāo)簽 
  19. plt.xticks(np.arange(5) + bar_width, labels) 
  20. # 設(shè)置Y軸的刻度范圍 
  21. plt.ylim([2500, 19000]) 
  22.  
  23. # 為每個(gè)條形圖添加數(shù)值標(biāo)簽 
  24. for x2016, y2016 in enumerate(Y2016): 
  25.     plt.text(x2016, y2016 + 100, '%s' % y2016, ha='center'
  26.  
  27. for x2017, y2017 in enumerate(Y2017): 
  28.     plt.text(x2017 + bar_width, y2017 + 100, '%s' % y2017, ha='center'
  29. # 顯示圖例 
  30. plt.legend() 
  31. plt.savefig('foo.png'
  32. # 顯示圖形 
  33. plt.show() 

 python數(shù)據(jù)分析條形圖的各種繪制方式

垂直堆疊條形圖

  1. # 導(dǎo)入模塊 
  2. import matplotlib.pyplot as plt 
  3. import numpy as np 
  4. import pandas as pd 
  5.  
  6. # 導(dǎo)入數(shù)據(jù) 
  7. traffic_volume = {'Index': ['railway''green''water transport''air transport'], 
  8.                   'Jan': [31058, 255802, 52244, 57], 
  9.                   'Feb': [28121, 179276, 46482, 42], 
  10.                   'Mar': [32185, 285446, 50688, 59], 
  11.                   'Api': [30133, 309576, 54728, 57], 
  12.                   'May': [30304, 319713, 55813, 60], 
  13.                   'Jun': [29934, 320028, 59054, 58], 
  14.                   'Jul': [31002, 319809, 57353, 55], 
  15.                   'Aug': [31590, 331077, 57583, 57]} 
  16.  
  17. data = pd.DataFrame(traffic_volume) 
  18. print(data) 
  19.  
  20. # 繪圖 
  21. plt.bar(np.arange(8), data.loc[0, :][1:], color='red', alpha=0.8, label='railway', align='center'
  22. plt.bar(np.arange(8), data.loc[1, :][1:], bottom=data.loc[0, :][1:], color='green', alpha=0.8, label='highway'
  23.         align='center'
  24. plt.bar(np.arange(8), data.loc[2, :][1:], bottom=data.loc[0, :][1:] + data.loc[1, :][1:], color='m', alpha=0.8, 
  25.         label='water transport', align='center'
  26. plt.bar(np.arange(8), data.loc[3, :][1:], bottom=data.loc[0, :][1:] + data.loc[1, :][1:] + data.loc[2, :][1:], 
  27.         color='black', alpha=0.8, label='air transport', align='center'
  28. # 添加軸標(biāo)簽 
  29. plt.xlabel('month'
  30. plt.ylabel('Cargo volume (10,000 tons)'
  31. # 添加標(biāo)題 
  32. plt.title('Monthly logistics volume in 2017'
  33. # 添加刻度標(biāo)簽 
  34. plt.xticks(np.arange(8), data.columns[1:]) 
  35. # 設(shè)置Y軸的刻度范圍 
  36. plt.ylim([0, 500000]) 
  37.  
  38. # 為每個(gè)條形圖添加數(shù)值標(biāo)簽 
  39. for x_t, y_t in enumerate(data.loc[0, :][1:]): 
  40.     plt.text(x_t, y_t / 2, '%sW' % (round(y_t / 10000, 2)), ha='center', color='white', fontsize=8) 
  41.  
  42. for x_g, y_g in enumerate(data.loc[0, :][1:] + data.loc[1, :][1:]): 
  43.     plt.text(x_g, y_g / 2, '%sW' % (round(y_g / 10000, 2)), ha='center', color='white', fontsize=8) 
  44.  
  45. for x_s, y_s in enumerate(data.loc[0, :][1:] + data.loc[1, :][1:] + data.loc[2, :][1:]): 
  46.     plt.text(x_s, y_s - 20000, '%sW' % (round(y_s / 10000, 2)), ha='center', color='white', fontsize=8) 
  47.  
  48. # 顯示圖例 
  49. plt.legend(loc='upper center', ncol=4) 
  50.  
  51. # 顯示圖形 
  52. plt.show() 

 python數(shù)據(jù)分析條形圖的各種繪制方式

以上就是各種條形圖的繪制方式,你Get到了嗎?

 

責(zé)任編輯:未麗燕 來源: 今日頭條
相關(guān)推薦

2020-06-17 08:35:12

數(shù)據(jù)分析Python代碼

2022-11-11 09:01:08

SwiftUI條形圖子視圖

2023-03-06 08:03:10

Python可視化工具

2022-10-18 23:53:20

Python數(shù)據(jù)Matplotlib

2023-09-04 15:35:54

2020-07-14 08:48:07

數(shù)據(jù)分析技術(shù)IT

2016-11-23 20:34:29

Cloudera

2021-03-16 11:33:23

數(shù)據(jù)分析

2023-11-29 20:24:45

數(shù)據(jù)可視化圖表

2020-06-08 15:06:33

Pandas可視化數(shù)據(jù)

2023-05-11 07:38:51

2023-11-24 08:47:36

ScipyPython

2010-02-03 10:17:29

C++繼承方式

2020-05-13 11:32:28

數(shù)據(jù)分析數(shù)值分析

2017-11-27 16:37:42

Python大數(shù)據(jù)數(shù)據(jù)分析

2022-06-01 11:56:04

區(qū)塊鏈數(shù)據(jù)分析

2017-11-24 14:20:38

2022-02-24 11:49:18

數(shù)據(jù)分析業(yè)務(wù)數(shù)據(jù)

2025-04-27 01:44:00

2021-05-10 15:14:23

Python棒棒糖圖表
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號