python绘制棉棒图

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

python绘制棉棒图

Vergil_Zsh   2022-05-28 我要评论

用法:

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()

在这里插入图片描述

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注的更多内容!  

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们