網(wǎng)站首頁 編程語言 正文
matplotlib介紹
- Matplotlib 是 Python 的繪圖庫。 它可與 NumPy 一起使用,提供了一種有效的 MatLab 開源替代方案。 它也可以和圖形工具包一起使用,如 PyQt 和 wxPython。
- 安裝Matplotlib庫命令:在cmd命令窗口輸入pip install matplotlib。
matplotlib繪制折線圖
1、繪制一條折線的折線圖
# -*- coding:utf-8 -*-
import matplotlib
import matplotlib.pyplot as plt
# 處理亂碼
matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用黑體顯示中文
x = [1, 2, 3, 4]
y = [10, 50, 20, 100]
# "r" 表示紅色,ms用來設(shè)置*的大小
plt.plot(x, y, "r", marker='*', ms=10, label="a")
# plt.plot([1, 2, 3, 4], [20, 30, 80, 40], label="b")
plt.xticks(rotation=45)
plt.xlabel("發(fā)布日期")
plt.ylabel("小說數(shù)量")
plt.title("80小說網(wǎng)活躍度")
# upper left 將圖例a顯示到左上角
plt.legend(loc="upper left")
# 在折線圖上顯示具體數(shù)值, ha參數(shù)控制水平對齊方式, va控制垂直對齊方式
for x1, y1 in zip(x, y):
plt.text(x1, y1 + 1, str(y1), ha='center', va='bottom', fontsize=20, rotation=0)
plt.savefig("a.jpg")
plt.show()
圖形效果展示:
注意:savefig()是圖形存儲成圖片,show()是將圖形顯示出來。
2、繪制多條折線
# -*- coding:utf-8 -*-
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用黑體顯示中文
x = [1, 2, 3, 4]
y1 = [45, 50, 20, 100]
y2 = [26, 10, 76, 25]
y3 = [11, 66, 55, 88]
y4 = [69, 50, 35, 100]
plt.plot(x, y1, marker='*', ms=10, label="a")
plt.plot(x, y2, marker='*', ms=10, label="b")
plt.plot(x, y3, marker='*', ms=10, label="c")
plt.plot(x, y4, marker='*', ms=10, label="d")
plt.xticks(rotation=45)
plt.xlabel("發(fā)布日期")
plt.ylabel("小說數(shù)量")
plt.title("80小說網(wǎng)活躍度")
plt.legend(loc="upper left")
# 在折線圖上顯示具體數(shù)值, ha參數(shù)控制水平對齊方式, va控制垂直對齊方式
for y in [y1, y2, y3, y4]:
for x1, yy in zip(x, y):
plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0)
plt.savefig("a.jpg")
plt.show()
圖形效果展示:
matplotlib繪制柱狀圖
1、繪制普通柱狀圖
# -*- coding:utf-8 -*-
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用黑體顯示中文
# 構(gòu)建數(shù)據(jù)
x = [1, 2, 3, 4]
y = [450, 500, 200, 1000]
# 繪圖
plt.bar(x=x, height=y, label='書庫大全', color='steelblue', alpha=0.8)
# 在柱狀圖上顯示具體數(shù)值, ha參數(shù)控制水平對齊方式, va控制垂直對齊方式
for x1, yy in zip(x, y):
plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0)
# 設(shè)置標題
plt.title("80小說網(wǎng)活躍度")
# 為兩條坐標軸設(shè)置名稱
plt.xlabel("發(fā)布日期")
plt.ylabel("小說數(shù)量")
# 顯示圖例
plt.legend()
plt.savefig("a.jpg")
plt.show()
圖形效果展示:
2、繪制多組柱狀圖
# -*- coding:utf-8 -*-
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用黑體顯示中文
# 構(gòu)建數(shù)據(jù)
x = ['2015', '2016', '2017', '2018', '2019']
y1 = [4500, 5000, 2000, 7000, 10000]
y2 = [5200, 7000, 5000, 9000, 11000]
# 繪圖
plt.bar(x=x, height=y1, label='python', color='steelblue', alpha=0.8)
plt.bar(x=x, height=y2, label='java', color='indianred', alpha=0.8)
# 在柱狀圖上顯示具體數(shù)值, ha參數(shù)控制水平對齊方式, va控制垂直對齊方式
for x1, yy in zip(x, y1):
plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0)
for x1, yy in zip(x, y2):
plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0)
# 設(shè)置標題
plt.title("python與java圖書對比")
# 為兩條坐標軸設(shè)置名稱
plt.xlabel("年份")
plt.ylabel("銷量")
# 顯示圖例
plt.legend()
plt.savefig("a.jpg")
plt.show()
圖形效果展示:
3、繪制柱狀圖的條柱并列顯示
# -*- coding:utf-8 -*-
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用黑體顯示中文
# 構(gòu)建數(shù)據(jù)
x = ['2015', '2016', '2017', '2018', '2019']
y1 = [4500, 5000, 2000, 7000, 10000]
y2 = [5200, 7000, 5000, 9000, 11000]
bar_width = 0.3
# 將X軸數(shù)據(jù)改為使用range(len(x_data), 就是0、1、2...
plt.bar(x=range(len(x)), height=y1, label='python', color='steelblue', alpha=0.8, width=bar_width)
# 將X軸數(shù)據(jù)改為使用np.arange(len(x_data))+bar_width,
# 就是bar_width、1+bar_width、2+bar_width...這樣就和第一個柱狀圖并列了
plt.bar(x=np.arange(len(x)) + bar_width, height=y2, label='java', color='indianred', alpha=0.8, width=bar_width)
# 在柱狀圖上顯示具體數(shù)值, ha參數(shù)控制水平對齊方式, va控制垂直對齊方式
for x1, yy in enumerate(y1):
plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0)
for x1, yy in enumerate(y2):
plt.text(x1 + bar_width, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0)
# 設(shè)置標題
plt.title("python與java對比")
# 為兩條坐標軸設(shè)置名稱
plt.xlabel("年份")
plt.ylabel("銷量")
# 顯示圖例
plt.legend()
plt.savefig("a.jpg")
plt.show()
圖形效果展示:
matplotlib繪制柱線混合圖
1、繪制柱線混合圖
# -*- coding:utf-8 -*-
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用黑體顯示中文
# 構(gòu)建數(shù)據(jù)
x = [2, 4, 6, 8]
y = [450, 500, 200, 1000]
# 繪圖
plt.bar(x=x, height=y, label='書庫大全', color='steelblue', alpha=0.8)
# 在柱狀圖上顯示具體數(shù)值, ha參數(shù)控制水平對齊方式, va控制垂直對齊方式
for x1, yy in zip(x, y):
plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0)
# 設(shè)置標題
plt.title("80小說網(wǎng)活躍度")
# 為兩條坐標軸設(shè)置名稱
plt.xlabel("發(fā)布日期")
plt.ylabel("小說數(shù)量")
# 顯示圖例
plt.legend()
# 畫折線圖
plt.plot(x, y, "r", marker='*', ms=10, label="a")
plt.xticks(rotation=45)
plt.legend(loc="upper left")
plt.savefig("a.jpg")
plt.show()
圖形效果展示:
總結(jié)?
原文鏈接:https://blog.csdn.net/weixin_45459224/article/details/100177163
相關(guān)推薦
- 2022-07-18 MVC與MVVM的區(qū)別與理解
- 2023-01-14 詳解Go語言如何進行Http調(diào)用_Golang
- 2022-07-18 C語言枚舉類型
- 2023-07-24 表單默認空白,無數(shù)據(jù)時賦默認值,新增時賦默認值
- 2022-10-04 解決Pandas生成Excel時的sheet問題的方法總結(jié)_python
- 2022-07-06 Nginx的mirror指令示例配置_nginx
- 2021-12-10 C#?Quartzs定時器的使用教程_C#教程
- 2024-01-29 在springboot多數(shù)據(jù)源項目中創(chuàng)建多個事務(wù)(解決@Transactional影響切換數(shù)據(jù)源問題
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(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被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支