日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

matplotlib繪制兩點間連線的幾種方法實現_python

作者:津津小可愛 ? 更新時間: 2022-05-06 編程語言

為了找到matplotlib在兩個點之間連線的方法真是費了好大功夫,本文主要介紹了?matplotlib繪制兩點間連線的幾種方法,具體如下

25bd872893d525be3eef175c36d91618.png

繪制方法 <1>

本文將通過最簡單的模式拆解Matplotlib繪圖的幾個組成部分,將cover以下內容
1. Create a dataset
2. Create a canvas
3. Add data to canvas
4. Show the figure
import numpy as np
import matplotlib.pyplot as plt
 
# create a dataset
points = np.linspace(-5, 5, 256)
y1 = np.tanh(points) + 0.5
y2 = np.sin(points) - 0.2
 
# create a canvas
fig, axe = plt.subplots(figsize=(7, 3.5), dpi=300)
 
# add data to canvas 
axe.plot(points, y1)
axe.plot(points, y2)
 
# show the figure
fig.savefig('output/to.png')
 
plt.close(fig)

416678f59ed73e91b34b9187a032fcc4.png

繪制方法<2> 使用pyplot繪制圖像

import matplotlib.pyplot as plt
import numpy as np
 
x = np.linspace(-3, 3, 256)
y = np.sin(x)
 
plt.plot(x, y)

1cb3b0d6b617e18981a04ab78dcf521e.png

繪制方法<3> 使用axes類繪制圖像

使用axes使用subplot()繪制單一圖像,使用subplots(nrows,ncols)繪制多個圖形

import matplotlib.pyplot as plt
import numpy as np
 
x = np.linspace(-3, 3, 256)
y = np.sin(x)
 
ax = plt.subplot()
ax.plot(x, y)

1cb3b0d6b617e18981a04ab78dcf521e.png

繪制方法<4> 使用figure類繪制圖像

import matplotlib.pyplot as plt
import numpy as np
 
x = np.linspace(-3, 3, 256)
y = np.sin(x)
 
fig = plt.figure(dpi=300)
ax = fig.add_subplot(111)
ax.plot(x, y)
fig.savefig('output/to.png')
plt.close(fig)

1cb3b0d6b617e18981a04ab78dcf521e.png

表示了圖像的position。如果使用subplots,則有 nrows, ncols, and index三個參數,其中idex從1開始,代表了左上角的圖像

原文鏈接:https://blog.csdn.net/weixin_33918358/article/details/112486438

欄目分類
最近更新