matplotlib 曲線圖 和 折線圖 plt.plot()例項
我就廢話不多說了,大家還是直接看程式碼吧!
繪製曲線:
import time import numpy as np import matplotlib.pyplot as plt x = np.linspace(0,10,1000) y = np.sin(x) plt.figure(figsize=(6,4)) plt.plot(x,y,color="red",linewidth=1 ) plt.xlabel("x") #xlabel、ylabel:分別設定X、Y軸的標題文字。 plt.ylabel("sin(x)") plt.title("正弦曲線圖") # title:設定子圖的標題。 plt.ylim(-1.1,1.1)# xlim、ylim:分別設定X、Y軸的顯示範圍。 plt.savefig('quxiantu.png',dpi=120,bbox_inches='tight') # plt.show() # plt.close()
import matplotlib.pyplot as plt squares=[1,4,9,6,25] plt.plot(squares) plt.savefig('zhexiantu.png',bbox_inches='tight') #dpi 代表畫素 #繪製折線圖
補充知識:matplotlib 畫箭頭的兩種方式
如下所示:
def drawArrow(A,B): fig = plt.figure(figsize=(5,5)) print("xasxcsasdc") ax = fig.add_subplot(121) # fc: filling color # ec: edge color """第一種方式""" ax.arrow(A[0],A[1],B[0]-A[0],B[1]-A[1],width=0.01,length_includes_head=True,# 增加的長度包含箭頭部分 head_width=0.25,head_length=1,fc='r',ec='b') ax.set_xlim(0,5) ax.set_ylim(0,5) ax.grid() ax.set_aspect('equal') """第二種方式""" # 這種方式是在圖上做標註時產生的 # Example: ax = fig.add_subplot(122) ax.annotate("",xy=(B[0],B[1]),xytext=(A[0],A[1]),# xycoords="figure points",arrowprops=dict(arrowstyle="->",color="r")) ax.set_xlim(0,5) ax.grid() ax.set_aspect('equal') #x軸y軸等比例 #x軸y軸等比例 plt.show()
第一種
Axes.arrow(x,# 座標x,y
dx,dy,# 箭頭兩端橫縱座標距離差
* * kwargs) # 箭頭架構和屬性設定
Constructor arguments
width 箭頭尾巴的線寬
length_includes_head: bool (default: False) # 增加的長度包含箭頭部分
head_width: float or None (default: 3*width) # 箭頭部分的寬度
head_length: float or None (default: 1.5 * head_width) # 箭頭部分的長度
shape: [‘full',‘left',‘right'] (default: ‘full') # 箭頭是否全部顯示 full 完整顯示 left左半部 right 右半部
alpha:透明度
color 箭頭的顏色
fc : 箭頭尾部的
ec:箭頭邊界的顏色
fill:箭頭部分是否填充顏色
antialiased :False時會讓箭頭部分帶上鋸齒
hatch:箭頭部分的填充形狀
{'/',‘',‘|',‘-',‘+',‘x',‘o',‘O',‘.',‘*'}
第二種
Axes.annotate(s,標註的資訊
xy,標註點的座標
*args,
**kwargs)[source]
引數:
s : str 標註的資訊
xy : (float,float) 標註點的座標(箭頭的頭端點)
xytext : (float,float),標註的位置(箭頭的尾巴)
arrowprops : dict,optional
標註指向的線條的形狀:
‘-' 、 ‘->' 、 ‘-[' 、 ‘|-|' 、 ‘-|>' 、 ‘<-' 、 ‘<->' 、 ‘<|-' 、 ‘<|-|>'、 ‘fancy' 、 ‘simple' 、 ‘wedge' 、
以上這篇matplotlib 曲線圖 和 折線圖 plt.plot()例項就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。