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',dpi=120,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.set_ylim(0, 5)
ax.grid()
ax.set_aspect('equal') #x軸y軸等比例
#x軸y軸等比例
plt.show()

第一種
Axes.arrow(x,y,# 坐標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 右半部
overhang: float (default: 0) # 不知道怎么形容 會改變箭頭部分的形狀
alpha:透明度
color 箭頭的顏色
fc : 箭頭尾部的
ec:箭頭邊界的顏色
fill:箭頭部分是否填充顏色
antialiased :False時會讓箭頭部分帶上鋸齒
hatch:箭頭部分的填充形狀
{'/', ‘', ‘|', ‘-', ‘+', ‘x', ‘o', ‘O', ‘.', ‘*'}
第二種
Axes.annotate(s, 標注的信息
xy, 標注點的坐標
*args,
**kwargs)[source]
參數(shù):
s : str 標注的信息
xy : (float, float) 標注點的坐標(箭頭的頭端點)
xytext : (float, float), 標注的位置(箭頭的尾巴)
arrowprops : dict, optional
標注指向的線條的形狀:
‘-' 、 ‘->' 、 ‘-[' 、 ‘|-|' 、 ‘-|>' 、 ‘<-' 、 ‘<->' 、 ‘<|-' 、 ‘<|-|>'、 ‘fancy' 、 ‘simple' 、 ‘wedge' 、
以上這篇matplotlib 曲線圖 和 折線圖 plt.plot()實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python 列表中的修改、添加和刪除元素的實現(xiàn)
這篇文章主要介紹了Python 列表中的修改、添加和刪除元素的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06
python可視化 matplotlib畫圖使用colorbar工具自定義顏色
這篇文章主要介紹了python可視化 matplotlib畫圖使用colorbar工具自定義顏色,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12
TensorFlow卷積神經網絡MNIST數(shù)據(jù)集實現(xiàn)示例
這篇文章主要介紹了TensorFlow卷積神經網絡MNIST數(shù)據(jù)集的實現(xiàn)示例的過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2021-11-11
pytorch中交叉熵損失(nn.CrossEntropyLoss())的計算過程詳解
今天小編就為大家分享一篇pytorch中交叉熵損失(nn.CrossEntropyLoss())的計算過程詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01

