網(wǎng)站首頁 編程語言 正文
首先如果柱狀圖中有中文,比如X軸和Y軸標(biāo)簽需要寫中文,解決中文無法識別和亂碼的情況,加下面這行代碼就可以解決了:
plt.rcParams['font.sans-serif'] = ['SimHei'] # 解決中文亂碼
以下總共展示了三種畫不同需求的柱狀圖:
畫多組兩個并列的柱狀圖:
import matplotlib import matplotlib.pyplot as plt import numpy as np plt.rcParams['font.sans-serif'] = ['SimHei'] labels = ['G1', 'G2', 'G3', 'G4', 'G5'] men_means = [20, 34, 30, 35, 27] women_means = [25, 32, 34, 20, 25] x = np.arange(len(labels)) # the label locations width = 0.35 # the width of the bars fig, ax = plt.subplots() rects1 = ax.bar(x - width/2, men_means, width, label='Men') rects2 = ax.bar(x + width/2, women_means, width, label='Women') # Add some text for labels, title and custom x-axis tick labels, etc. ax.set_ylabel('Scores') ax.set_title('Scores by group and gender') ax.set_xticks(x) ax.set_xticklabels(labels) ax.legend() def autolabel(rects): """Attach a text label above each bar in *rects*, displaying its height.""" for rect in rects: height = rect.get_height() ax.annotate('{}'.format(height), xy=(rect.get_x() + rect.get_width() / 2, height), xytext=(0, 3), # 3 points vertical offset textcoords="offset points", ha='center', va='bottom') autolabel(rects1) autolabel(rects2) fig.tight_layout() plt.show()
繪制好的柱狀圖如下:
畫兩組5個并列的柱狀圖:
import matplotlib import matplotlib.pyplot as plt import numpy as np plt.rcParams['font.sans-serif']=['SimHei'] # 解決中文亂碼 labels = ['第一項(xiàng)', '第二項(xiàng)'] a = [4.0, 3.8] b = [26.9, 48.1] c = [55.6, 63.0] d = [59.3, 81.5] e = [89, 90] x = np.arange(len(labels)) # 標(biāo)簽位置 width = 0.1 # 柱狀圖的寬度,可以根據(jù)自己的需求和審美來改 fig, ax = plt.subplots() rects1 = ax.bar(x - width*2, a, width, label='a') rects2 = ax.bar(x - width+0.01, b, width, label='b') rects3 = ax.bar(x + 0.02, c, width, label='c') rects4 = ax.bar(x + width+ 0.03, d, width, label='d') rects5 = ax.bar(x + width*2 + 0.04, e, width, label='e') # 為y軸、標(biāo)題和x軸等添加一些文本。 ax.set_ylabel('Y軸', fontsize=16) ax.set_xlabel('X軸', fontsize=16) ax.set_title('這里是標(biāo)題') ax.set_xticks(x) ax.set_xticklabels(labels) ax.legend() def autolabel(rects): """在*rects*中的每個柱狀條上方附加一個文本標(biāo)簽,顯示其高度""" for rect in rects: height = rect.get_height() ax.annotate('{}'.format(height), xy=(rect.get_x() + rect.get_width() / 2, height), xytext=(0, 3), # 3點(diǎn)垂直偏移 textcoords="offset points", ha='center', va='bottom') autolabel(rects1) autolabel(rects2) autolabel(rects3) autolabel(rects4) autolabel(rects5) fig.tight_layout() plt.show()
繪制好的柱狀圖如下:
3. 要將柱狀圖的樣式畫成適合論文中使用的黑白并且?guī)Щy的樣式:
import matplotlib.pyplot as plt import numpy as np plt.rcParams['font.sans-serif'] = ['SimHei'] # 解決中文亂碼 labels = ['第一項(xiàng)', '第二項(xiàng)'] a = [50, 80] b = [37, 69] c = [78, 60] d = [66, 86] e = [80, 95] # marks = ["o", "X", "+", "*", "O"] x = np.arange(len(labels)) # 標(biāo)簽位置 width = 0.1 # 柱狀圖的寬度 fig, ax = plt.subplots() rects1 = ax.bar(x - width * 2, a, width, label='a', hatch="...", color='w', edgecolor="k") rects2 = ax.bar(x - width + 0.01, b, width, label='b', hatch="oo", color='w', edgecolor="k") rects3 = ax.bar(x + 0.02, c, width, label='c', hatch="++", color='w', edgecolor="k") rects4 = ax.bar(x + width + 0.03, d, width, label='d', hatch="XX", color='w', edgecolor="k") rects5 = ax.bar(x + width * 2 + 0.04, e, width, label='e', hatch="**", color='w', edgecolor="k") # 為y軸、標(biāo)題和x軸等添加一些文本。 ax.set_ylabel('Y', fontsize=16) ax.set_xlabel('X', fontsize=16) ax.set_title('標(biāo)題') ax.set_xticks(x) ax.set_xticklabels(labels) ax.legend()
以上
總結(jié)
原文鏈接:https://blog.csdn.net/weixin_44293949/article/details/114590319
相關(guān)推薦
- 2022-09-23 React深入了解原理_React
- 2022-12-22 OpenHarmony如何調(diào)用電話服務(wù)API撥打電話_Android
- 2022-08-25 .NET6環(huán)境下實(shí)現(xiàn)MQTT通信及詳細(xì)代碼演示_實(shí)用技巧
- 2022-05-25 C語言中操作字符串的函數(shù)詳解_C 語言
- 2022-10-27 樹莓派4B更換清華源和沒有公鑰報錯
- 2023-12-17 當(dāng)springsecurity出現(xiàn)SerializationException問題
- 2022-07-27 PostgreSQL出現(xiàn)死鎖該如何解決_PostgreSQL
- 2022-11-30 React中常見的TypeScript定義實(shí)戰(zhàn)教程_React
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支