stem函数–Matplotlib
函数功能: Create a stem plot.
创建棉棒图
A stem plot plots vertical lines at each x location from the baseline to y, and places a marker there.
在每个x的位置绘制基准线到y的垂直线,并在y处绘制标记。
函数语法:
stem([x,] y, linefmt=None, markerfmt=None, basefmt=None,bottom=0, label=None, use_line_collection=True, data=None)
函数参数:
x: array-like, optional;The x-positions of the stems. Default: (0, 1, …, len(y) - 1).
可选参数,数组,每根棉棒的x轴位置,默认设置为(0, 1,2,…,len(y)-1)
y: array-like ;The y-values of the stem heads.
数组,棉棒头部的y值,y坐标
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.5, 2*np.pi, 20)
y = np.random.randn(20)
plt.stem(x, y)
plt.show()
当只有y值,x值使用默认设置(0,1,2,…,len(y)-1)
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.5, 2*np.pi, 20)
y = np.random.randn(20)
plt.stem(y)
print(plt.xlim())
plt.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201026111429719.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Nob25nYmFpa2Fpc2hp,size_16,color_FFFFFF,t_70#pic_center)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201026110716344.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Nob25nYmFpa2Fpc2hp,size_16,color_FFFFFF,t_70#pic_center)
输入x,y值
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201026110500428.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Nob25nYmFpa2Fpc2hp,size_16,color_FFFFFF,t_70#pic_center)
linefmt: str, optional
A string defining the properties of the vertical lines. Usually, this will be a color or a color and a linestyle:
线条样式:可选参数,字符串类型,定义垂直线的属性。通常,定义垂直线的颜色,或线的颜色和线条类型
线条类型可选参数如下:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201026104807183.png#pic_center)
修改参数
l
i
n
e
f
m
t
linefmt
linefmt,设置棉棒线条类型,
l
i
n
e
f
m
t
=
′
−
−
′
linefmt='--'
linefmt=′−−′
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.5, 2*np.pi, 20)
y = np.random.randn(20)
plt.stem(x, y, linefmt='--')
plt.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201026111612835.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Nob25nYmFpa2Fpc2hp,size_16,color_FFFFFF,t_70#pic_center)
设置棉棒线条类型,
l
i
n
e
f
m
t
=
′
−
.
′
linefmt='-.'
linefmt=′−.′
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.5, 2*np.pi, 20)
y = np.random.randn(20)
plt.stem(x, y, linefmt='-.')
plt.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201026111749623.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Nob25nYmFpa2Fpc2hp,size_16,color_FFFFFF,t_70#pic_center)
Default: ‘C0-’, i.e. solid line with the first color of the color cycle.
Note: While it is technically possible to specify valid formats other than color or color and linestyle (e.g. ‘rx’ or ‘-.’), this is beyond the intention of the method and will most likely not result in a reasonable plot.
默认
l
i
n
e
f
m
t
linefmt
linefmt参数设置为:
C
0
−
C0-
C0−:即颜色循环第一种颜色,实线绘制。
注意:尽管在技术上可以指定颜色,颜色和线条样式以外的有效格式(例如’rx’或’-.’)。但这超出了该方法的意图,并且很可能不会导致合理的绘图。
指定颜色:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.5, 2*np.pi, 20)
y = np.random.randn(20)
plt.stem(x, y, linefmt='r')
plt.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201026113345244.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Nob25nYmFpa2Fpc2hp,size_16,color_FFFFFF,t_70#pic_center)
同时指定颜色和样式,参数
l
i
n
e
f
m
t
=
′
c
−
−
′
linefmt='c--'
linefmt=′c−−′
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.5, 2*np.pi, 20)
y = np.random.randn(20)
plt.stem(x, y, linefmt='c--')
plt.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201026113441323.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Nob25nYmFpa2Fpc2hp,size_16,color_FFFFFF,t_70#pic_center)
markerfmt: str, optional
A string defining the properties of the markers at the stem heads. Default: ‘C0o’, i.e. filled circles with the first color of the color cycle.
棉棒头部标记样式:字符串,可选参数
定义棉棒头部标记属性的字符串,默认是
C
0
o
C0o
C0o,即:用颜色循环的第一种颜色的圆圈样式
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.5, 2*np.pi, 20)
y = np.random.randn(20)
plt.stem(x, y, linefmt='c--', markerfmt='r*')
plt.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201102101202663.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Nob25nYmFpa2Fpc2hp,size_16,color_FFFFFF,t_70#pic_center)
该参数中的颜色只能是默认颜色循环
C
0
−
C
9
C0-C9
C0−C9中的一个,其他颜色无法识别
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.5, 2*np.pi, 20)
y = np.random.randn(20)
plt.stem(x, y, linefmt='c--', markerfmt='C6o')
plt.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201102101722240.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Nob25nYmFpa2Fpc2hp,size_16,color_FFFFFF,t_70#pic_center)
可以简写的颜色也可以使用:
基础颜色
支持常见的
b
l
u
e
,
c
y
a
n
,
g
r
e
e
n
,
r
e
d
,
y
e
l
l
o
w
,
m
a
g
e
n
t
a
,
w
h
i
t
e
,
b
l
a
c
k
blue, cyan, green, red, yellow, magenta, white,black
blue,cyan,green,red,yellow,magenta,white,black. 这八种颜色支持缩写(除了黑色是k, 其他都是首字母)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201102102029454.png#pic_center)
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.5, 2*np.pi, 20)
y = np.random.randn(20)
plt.stem(x, y, linefmt='c--', markerfmt='m*')
plt.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201102102054292.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Nob25nYmFpa2Fpc2hp,size_16,color_FFFFFF,t_70#pic_center)
basefmt: str, default: ‘C3-’ (‘C2-’ in classic mode)
A format string defining the properties of the baseline.
基线格式: 字符串,默认为
C
3
−
C3-
C3−(经典模式下为
C
2
−
C2-
C2−)
定义基线属性的字符串
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.5, 2*np.pi, 20)
y = np.random.randn(20)
plt.stem(x, y, linefmt='c--', markerfmt='r*',
basefmt='C3-')
plt.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201102102508651.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Nob25nYmFpa2Fpc2hp,size_16,color_FFFFFF,t_70#pic_center)
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.5, 2*np.pi, 20)
y = np.random.randn(20)
plt.stem(x, y, linefmt='c--', markerfmt='r*',
basefmt='C2-')
plt.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201102102540545.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Nob25nYmFpa2Fpc2hp,size_16,color_FFFFFF,t_70#pic_center)
bottom: float, default: 0
The y-position of the baseline.
基线位置:浮点型,默认值为0。 基线所在的y轴位置
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.5, 2*np.pi, 20)
y = np.random.randn(20)
plt.stem(x, y, linefmt='c--', markerfmt='r*',
basefmt='--', bottom=0.5)
plt.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201102102952238.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Nob25nYmFpa2Fpc2hp,size_16,color_FFFFFF,t_70#pic_center)
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.5, 2*np.pi, 20)
y = np.random.randn(20)
plt.stem(x, y, linefmt='c--', markerfmt='r*',
basefmt='--', bottom=-0.3)
plt.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201102103051224.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Nob25nYmFpa2Fpc2hp,size_16,color_FFFFFF,t_70#pic_center)
label: str, default: None
The label to use for the stems in legends.
标签:字符串,默认无,棉棒图例的标签
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.5, 2*np.pi, 20)
y = np.random.randn(20)
plt.stem(x, y, linefmt='c--', markerfmt='r*',
basefmt='--', bottom=-0.3, label='stem')
plt.legend()
plt.show()
![在这里插入图片描述](https://img-blog.csdnimg.cn/2020110210352590.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Nob25nYmFpa2Fpc2hp,size_16,color_FFFFFF,t_70#pic_center)
官方文档stem