matplotlib畫圖筆記(1)

ps:整理了下畫圖中查到的資料(清空收藏夾)以備下次使用

目錄

學習鏈接

 0.模塊導入

1.python matplotlib中axes與axis的區別?

2.matplotlib設置畫面大小

3.matplotlib命令與格式:圖例legend語法及設置

4.matplotlib三維繪圖

5.matplotlib subplot子圖

6.matplotlib中添加註解

7.matplotlib繪圖設置座標軸刻度、文本

8.中文顯示參數設置

9. matplotlib 如何使x,y軸的單位長度相等呢? 

10.雙y軸座標軸圖及控制時間格式

11.matplotlib保存圖片


學習鏈接

https://matplotlib.org/index.html

https://liam.page/2014/09/11/matplotlib-tutorial-zh-cn/

https://blog.csdn.net/qq_31192383/category_6665721.html

 0.模塊導入

首先對於matplotlib的使用,有兩種形式,一種是matplotlib下的模塊pyplot,一種是pylab

第一種導入是

from matplotlib import pyplot
import matplotlib as mpl

第二種則直接是

from pylab import *

官方對於的解釋是:pylab結合了pyplot和numpy,對交互式使用來說比較方便,既可以畫圖又可以進行簡單的計算。但是,對於一個項目來說,建議分別導入使用 

 具體可參見這位的筆記

1.python matplotlib中axes與axis的區別?

來自知乎禹洋的回答 - 知乎

 用畫板和畫紙來做比喻的話,figure就好像是畫板,是畫紙的載體,但是具體畫畫等操作是在畫紙上完成的。在pyplot中,畫紙的概念對應的就是Axes/Subplot。

fig = plt.figure() 
plt.show()

結果是一個畫板

fig = plt.figure() 
ax = fig.add_subplot(111) 
plt.show()

結果加上了座標系

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set(xlim=[0.5, 4.5], ylim=[-2, 8],title='An Example Axes', ylabel='Y-Axis',xlabel='X-Axis')
plt.show()

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set(xlim=[0.5, 4.5], ylim=[-2, 8],title='An Example Axes', ylabel='Y-Axis',xlabel='X-Axis')
ax.plot([1, 2, 3, 4], [2,5,4,7])
plt.show()

2.matplotlib設置畫面大小

fig=plt.figure(figsize=(6, 5))

 代表的是畫板是600*500的

3.matplotlib命令與格式:圖例legend語法及設置

開碼牛的博客

4.matplotlib三維繪圖

https://blog.csdn.net/u014636245/article/details/82799573

https://blog.csdn.net/guduruyu/article/details/78050268

5.matplotlib subplot子圖

https://blog.csdn.net/claroja/article/details/70841382

https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplots_adjust.html

6.matplotlib中添加註解

(1)可通過plt.annotate()函數

具體見https://blog.csdn.net/u012735708/article/details/82114565

 (2)可通過ax.text()函數

7.matplotlib繪圖設置座標軸刻度、文本

https://www.jb51.net/article/134638.htm

https://blog.csdn.net/u010358304/article/details/78906768

8.中文顯示參數設置

https://segmentfault.com/a/1190000005144275

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用來正常顯示中文標籤
plt.rcParams['axes.unicode_minus']=False #用來正常顯示負號

9. matplotlib 如何使x,y軸的單位長度相等呢? 

https://www.zhihu.com/question/51213258

import pylab as plt
fig=plt.figure()
plt.plot([1,2,3,4])
ax = plt.gca()
#第一種
ax.set_aspect(1)
#第二種
plt.axis("equal")
#第三種
plt.figure(figsize=(6,6)) #figsize=(*,*) 來設置,使這兩個值保持相等

10.雙y軸座標軸圖及控制時間格式

https://segmentfault.com/a/1190000006158803 

11.matplotlib保存圖片

解決使用 plt.savefig 保存圖片時一片空白

(1)在 plt.show() 之前調用 plt.savefig()

import matplotlib.pyplot as plt

""" 一些畫圖代碼 """

plt.savefig("filename.png")
plt.show()

(2)畫圖的時候獲取當前圖像(這一點非常類似於 Matlab 的句柄的概念):、

# gcf: Get Current Figure
fig = plt.gcf()
plt.show()
fig1.savefig('tessstttyyy.png', dpi=100)

 去掉圖片邊框

#! usr/bin/python
#coding=utf-8 
import numpy as np
import matplotlib.pyplot as plt 
data=np.random.rand(10,10)
fig, ax=plt.subplots()
data[data==-1]=np.nan#去掉缺省值-1
im =ax.imshow(data,interpolation='none',cmap='Reds_r',vmin=0.6,vmax=.9)#不插值
#去掉邊框
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
plt.show()

使用 plt.savefig() 輸出圖片去除旁邊的空白區域

 subplot_adjust + margin(0,0)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章