stem函数--Matplotlib

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

在这里插入图片描述

在这里插入图片描述
输入x,y值
在这里插入图片描述

linefmt: str, optional
A string defining the properties of the vertical lines. Usually, this will be a color or a color and a linestyle:
线条样式:可选参数,字符串类型,定义垂直线的属性。通常,定义垂直线的颜色,或线的颜色和线条类型
线条类型可选参数如下:
在这里插入图片描述
修改参数 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()

在这里插入图片描述
设置棉棒线条类型, 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()

在这里插入图片描述
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()

在这里插入图片描述
同时指定颜色和样式,参数 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()

在这里插入图片描述

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

在这里插入图片描述
该参数中的颜色只能是默认颜色循环 C 0 − C 9 C0-C9 C0C9中的一个,其他颜色无法识别

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

在这里插入图片描述
可以简写的颜色也可以使用:

基础颜色
支持常见的 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, 其他都是首字母)

在这里插入图片描述

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

在这里插入图片描述
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()

在这里插入图片描述

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

在这里插入图片描述
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()

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

官方文档stem

  • 8
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值