首頁 > 軟體

python繪製棉棒圖的方法詳解

2022-03-24 19:00:51

用法:

matplotlib.pyplot.stem(*args, linefmt=None, markerfmt=None, basefmt=None, bottom=0, label=None, use_line_collection=True, orientation='vertical', data=None)

引數說明

引數 
*argsx,y,x—棉棒的x軸基線的取值範圍,y—棉棒的長度
linefmt棉棒的樣式,{‘-’,’–’,’:’,’-.’},包括指定顏色等
markerfmt棉棒末端的樣式
basefmt指定基線的樣式
bottom預設值為0,極限的y/x的位置,取決於方向
label用於標籤
use_line_collection預設值是True,如果為True,棉棒的莖線儲存並繪製為線集合,而不是單獨的線,這將顯著提高效能,而不是單獨的線
orientation控制方向,預設為垂直(True).

案例

引數

x,y

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.1, 2 * np.pi, 41)
y = np.exp(np.sin(x))
plt.stem(x, y)
plt.show()

linefmtlinefmt用來指定棉棒的顏色和樣式等

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0.1,2 * np.pi, 41)
y = np.exp(np.sin(x))
fit, axs = plt.subplots(nrows=2, ncols=2, figsize=(14,7))
axs[0,0].set_title("linefmt='-',color='green'")
axs[0,0] = axs[0,0].stem(x,y,linefmt='g-')
axs[0,1].set_title("linefmt='-.', color='blue'")
axs[0,1] = axs[0,1].stem(x,y,linefmt='b-.')
axs[1,0].set_title("linefmt=':', color='red'")
axs[1,0] = axs[1,0].stem(x,y,linefmt='r:')
axs[1,1].set_title("linefmt='--', color='p'")
axs[1,1] = axs[1,1].stem(x,y,linefmt='o--')
plt.show()

basefmt

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0.1,2 * np.pi, 41)
y = np.exp(np.sin(x))
fit, axs = plt.subplots(nrows=2, ncols=1, figsize=(14,7))
axs[0].set_title("basefmt='-',color='C2'")
axs[0] = axs[0].stem(x,y,linefmt='b--',basefmt='C2--')
axs[1].set_title("basefmt='-.', color='C1'")
axs[1] = axs[1].stem(x,y,linefmt='b-.',basefmt='C1-.')
plt.show()

bottom

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0.1,2 * np.pi, 41)
y = np.exp(np.sin(x))
fit, axs = plt.subplots(nrows=1, ncols=2, figsize=(14,7))
axs[0].set_title("bottom")
axs[0].stem(x,y,linefmt='grey',markerfmt='D',bottom=1.1)
axs[1].set_title('no bottom')
axs[1].stem(x,y,linefmt='grey',markerfmt='D')
plt.show()

案例

import numpy as np
import matplotlib.pyplot as plt
# 生成模擬資料集
x=np.linspace(0,10,20)
y=np.random.randn(20)
# 繪製棉棒圖
markerline, stemlines, baseline = plt.stem(x,y,linefmt='-',markerfmt='o',basefmt='--',label='TestStem')
# 可單獨設定棉棒末端,棉棒連線以及基線的屬性
plt.setp(markerline, color='k')#將棉棒末端設定為黑色
plt.legend()
plt.show()

總結

本篇文章就到這裡了,希望能夠給你帶來幫助,也希望您能夠多多關注it145.com的更多內容!  


IT145.com E-mail:sddin#qq.com