點擊上方藍字關注我們
點擊上方“印象python”,選擇“星標”公衆號
重磅乾貨,第一時間送達!
使用起來極爲簡單。
以漸進、交互式方式實現數據可視化。
表達式和文本使用LaTeX排版。
對圖像元素控制力強。
可輸出PNG、PDF、SVG和EPS等多種格式。
import matplotlib.pyplot as plt
import numpy as np
#x的值是從-pi到pi
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
#正弦函數
y = np.sin(x)
#餘弦函數
y1 = np.cos(x)
#圖形展示,並且給圖形加上標籤說明
plt.plot(x,y,label = 'sin')
plt.plot(x,y1,label = 'cos')
#標籤展示 0代表最優位置
plt.legend(loc = 0)
location的位置參數如下:
Location String | Location Code |
'best' | 0 |
'upper right' | 1 |
upper left' | 2 |
'lower left' | 3 |
'lower right' | 4 |
'right' | 5 |
'center left' | 6 |
'center right' | 7 |
'lower center' | 8 |
'upper center' | 9 |
'center' | 10 |
如果我們在實際中有指定x、y範圍的這種需求,那該怎麼做呢?
先來看看第一種方式:使用xlim()、ylim()方法設置x、y的範圍。
#設置x的範圍[-4,4]
plt.xlim(-4,4)
#設置y的範圍[-1,1]
plt.ylim(-1,1)
第二種方法使用axis()方法可同時設置x、y的取值範圍
#同時設置x的取值範圍[-3,3]和y的取值範圍[-2,2]
plt.axis([-3,3,-2,2])
我們刻度使用-pi,0,pi來代替-1,0,1這樣正餘弦函數看起來更有意義。這該如何操作呢?我們來使用xticks()方法來實現。我們先來了解一下這個函數的使用方法:
Signature: plt.xticks(*args, **kwargs)
Docstring:
Get or set the current tick locations and labels of the x-axis.
Call signatures::
locs, labels = xticks() # Get locations and labels
xticks(locs, [labels], **kwargs) # Set locations and labels
Parameters
----------
locs : array_like
A list of positions at which ticks should be placed. You can pass an
empty list to disable xticks.
labels : array_like, optional
A list of explicit labels to place at the given *locs*.
**kwargs
:class:`.Text` properties can be used to control the appearance of
the labels.
plt.xticks([-np.pi,0,np.pi],[r'$-\pi$',0,r'$+\pi$'])
對於y軸同理也是如此。就不在此贅述了哈!
回覆下方 「關鍵詞」,獲取優質資源
回覆關鍵詞 「linux」,即可獲取 185 頁 Linux 工具快速教程手冊和154頁的Linux筆記。
回覆關鍵詞 「Python進階」,即可獲取 106 頁 Python 進階文檔 PDF
回覆關鍵詞 「Python面試題」,即可獲取最新 100道 面試題 PDF
回覆關鍵詞 「python數據分析」,即可獲取47頁python數據分析與自然語言處理的 PDF
回覆關鍵詞 「python爬蟲」,滿滿五份PPT爬蟲教程和70多個案例
回覆關鍵詞 「Python最強基礎學習文檔」,即可獲取 168 頁 Python 最強基礎學習文檔 PDF,讓你快速入門Python
推薦我的微信號
來圍觀我的朋友圈,我的經驗分享,技術更新,不定期送書,坑位有限,速速掃碼添加!
備註:開發方向_暱稱_城市,另送你10本Python電子書。
本文分享自微信公衆號 - 印象Python(Python_logic)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。