網站首頁 編程語言 正文
python畫圖時linestyle,color和loc參數的設置
本人沒有看過專門介紹matplotlib的書籍,所以一直以來對一些畫圖的風格不是很清楚,今天總結一下linestyle,color和loc,供那些像我一樣沒看過這方面介紹的人使用。
linestyle和color是matplotlib做表時常用的兩個參數,只要是需要線條的,不論是主圖,還是添加的網格線,基本上是都會遇到的。loc則是用來確定位置的,一般添加圖例的時候回用到。
顏色字符(color)
字符 | 代表顏色 |
---|---|
r | 紅色 |
b | 藍色 |
g | 綠色 |
w | 白色 |
c | 青色 |
m | 洋紅 |
y | 黃色 |
k | 黑色 |
風格字符(linestyle)
字符 | 代表風格 |
---|---|
- (一個連字符) | 實線 |
– (兩個連字符) | 虛線 |
-. | 點劃線 |
: | 點虛線 |
’ ’ | 留空,空格 |
loc 參數(以matplotlib添加圖例為例說明位置)
loc string | loc code | 位置 |
---|---|---|
"best" | 0 | 右上角(默認) |
“upper right” | 1 | 右上角 |
“upper left” | 2 | 左上角 |
“lower left” | 3 | 左下角 |
“lower right” | 4 | 右下角 |
"right" | 5 | 中右側 |
“center left” | 6 | 中左側 |
“center right” | 7 | 中右側 |
“low center” | 8 | 中下方 |
“upper center” | 9 | 中上方 |
“center” | 10 | 中間 |
python畫圖基礎
python用于畫圖常用matplotlib和seaborn**
1.matplotlib
使用前需要導入api
import matplotlib.pyplot as plt
1.1繪制基本圖像
1.1.1折線圖
1.準備數據
import random
x = range(60)
y = [random.uniform(15,18) for i in x]
#random.uniform 中參數即為取值范圍。
2.創建畫布
plt.figure(figsize=(20,5),dpi=100)
#其中參數figsize即為圖像大小,dpi為分辨率。
3.繪制圖像
plt.plot(x,y)
4.圖像顯示
plt.show()
1.1.2 散點圖
重復1,2,4步
第3步改為plt.scatter(x,y)
圖像顯示:
1.1.3柱狀圖
重復1,2,4步
第3步改為plt.bar(x,y)
圖像顯示:
1.1.4 直方圖
重復1,2,4步
第三步改為plt.hist(y)
只傳入一個y參數,表示y的分布情況
圖像顯示:
也可以傳入x參數,表示x的分布情況(但因x為0~59,所以沒有意義)
圖像顯示:
1.2實現一些其他功能
我們用自己創建的數據作為上海市早上10時~11時的溫度變化
#1.數據準備
x_shanghai = x
y_shanghai = y
#2.創建畫布
plt.figure(figsize=(20,5),dpi=100)
#3.繪制圖像
plt.plot(x_shanghai,y_shanghai)
#3.1添加x、y的刻度
x_ticks = ["10:{}分"format(i) for i in x_shanghai]
y_ticks = range(40)
#3.2修改x、y的刻度
plt.xticks(x_shanghai[::5],x_ticks[::5])
#plt.xticks第一個參數是刻度,第二個參數是刻度標簽(plt.yticks也一樣)
plt.yticks(y_ticks[::5])
#五分鐘取一個刻度
#3.3添加網格顯示
plt.grid(True,linestyle = "--",alpha = 0.6)
參數linestyle為網格線的種類,alpha為網格線深淺
圖像顯示:
1.3 在一個坐標系中畫出多個圖像
#0 準備數據
x = range(60)
y_shanghai = [random.uniform(15,18) for i in x]
y_beijing = [random.uniform(1,3) for i in x]
#1 創建畫布
plt.figure(figsize=(20,5),dpi=100)
#2 繪制圖像
plt.plot(x,y_shanghai,label = "上海")
#畫一條標簽為上海的圖像
plt.plot(x,y_beijing,color = 'r',linestyle="--",label="北京")
#畫一條標簽為北京的圖像
plt.legend(loc ="best")
#plt.legend()函數設置圖例位置
#2.1 添加x,y刻度
x_shanghai_ticks = ["10點{}分".format(i) for i in x]
y_shanghai_ticks = range(40)
#2.2 修改x,y刻度
plt.xticks(x[::5],x_shanghai_ticks[::5])
plt.yticks(y_shanghai_ticks[::5])
#2.3 添加網格顯示
plt.grid(True , linestyle = "--",alpha = 0.6)
#2.4 添加標簽數據
plt.xlabel("時間",fontsize=20)
plt.ylabel("溫度",fontsize=20)
plt.title("某市某日10時至11時的溫度變化折線圖",fontsize=20)
#fontsize為字體大小
# 圖像保存
plt.savefig("test.png")
#3 圖像顯示
plt.show()
圖像顯示:
1.4多坐標作圖
#0 準備數據
x = range(60)
y_shanghai = [random.uniform(15,18) for i in x]
y_beijing = [random.uniform(1,3) for i in x]
#1 創建畫布
plt.figure(figsize=(20,8),dpi=100)
# fig,axes=plt.subplot(nrows=1,ncols=2,figsize=(20,8),dpi=100)
#2 繪制圖像
# axes[0].plot(x,y_shanghai,label = "上海")
# axes[1].plot(x,y_beijing,color = 'r',linestyle="--",label="北京")
f1=plt.subplot(1,2,1)#將figure分成一行兩列,第三個數字代表的是第一個位置的圖
f1.scatter(x,y_shanghai,label="上海",color='r',linestyle='--')#分別為x的顯示范圍為0-3,y的坐標0-1
f2=plt.subplot(1,2,2)#將figure分成一行兩列,第三個數字代表的是第二個位置的圖
f2.scatter(x,y_beijing,label="北京")
# #2.1 添加x,y刻度
x_ticks= ["10點{}分".format(i) for i in x]
y_shanghai_ticks = range(40)
# #2.2 修改x,y刻度
# plt.xticks(x[::5],x_ticks[::5])
# plt.yticks(y_shanghai_ticks[::5])
f1.set_xticks(x[::5])
f1.set_yticks(y_shanghai_ticks[::5])
f1.set_xticklabels(x_ticks[::5])
f2.set_xticks(x[::5])
f2.set_yticks(y_shanghai_ticks[::5])
f2.set_xticklabels(x_ticks[::5])
# #2.3 添加網格顯示
f1.grid(True , linestyle = "--",alpha = 0.6)
f2.grid(True , linestyle = "--",alpha = 0.6)
# #2.4 添加標簽數據
# plt.xlabel("時間",fontsize=20)
# plt.ylabel("溫度",fontsize=20)
# plt.title("某市某日10時至11時的溫度變化折線圖",fontsize=20)
f1.set_xlabel("時間")
f1.set_ylabel("溫度")
f1.set_title("上海市十時至十一時的溫度變化",fontsize=20)
f2.set_xlabel("時間")
f2.set_ylabel("溫度")
f2.set_title("北京市十時至十一時的溫度變化",fontsize=20)
f1.legend(loc=0)
f2.legend(loc=0)
# 圖像保存
plt.savefig("test.png")
#3 圖像顯示
plt.show()
圖像顯示:
1.5 繪圖應用
import numpy as np
# 繪制一般函數的圖像
# 繪制反正切函數圖像
# 0 準備數據
x = np.linspace(-10,10,1000)
# x為-10到10等間距取1000份
y = np.arctan(x)
# 1 創建畫布
plt.figure(figsize=(20,8),dpi = 100)
# 2 繪制圖像
plt.plot(x,y)
# 2.1添加網格
plt.grid(True,linestyle = "--",alpha=0.6)
# 3 顯示圖像
plt.show()
圖像顯示:
#繪制正比例函數y = x
圖像顯示:
原文鏈接:https://blog.csdn.net/LittleShengs/article/details/88777677
相關推薦
- 2022-06-27 Python實現從文件中加載數據的方法詳解_python
- 2022-05-28 python爬蟲框架scrapy下載中間件的編寫方法_python
- 2022-09-26 利用QDir實現刪除選定文件目錄下的空文件夾_C 語言
- 2022-05-13 python list.sort()方法排序一探究竟
- 2021-12-27 readAsText 讀取本地文件
- 2022-10-07 Android?Gradle?三方依賴管理詳解_Android
- 2022-06-16 HTTP服務壓力測試工具及相關術語講解_Golang
- 2023-12-07 WARNING: Access control is not enabled for the dat
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支