【可视化】使用matplotlib进行区域填充

版权声明:本文为博主原创文章,转载请在文章开头注明出处(作者+原文链接)。 https://blog.csdn.net/ChenVast/article/details/81904497

效果图

matplotlib.pyplot.fill_between

函数:

matplotlib.pyplot.fill_between(xy1y2=0where=Noneinterpolate=Falsestep=Nonehold=Nonedata=None**kwargs)

效果:

在两条水平曲线之间填充区域。

曲线由点(xy1)和(xy2)定义。这将创建一个或多个描述填充区域的多边形。

您可以使用where来排除某些水平部分。

默认情况下,边直接连接给定点。如果填充应该是阶梯函数,即在x之间保持不变,请使用步骤

参数:

参数:

x : array (length N)

The x coordinates of the nodes defining the curves.

定义曲线的节点的x坐标

y1 : array (length N) or scalar

The y coordinates of the nodes defining the first curve.
定义第一条曲线的节点的y坐标

y2 : array (length N) or scalar, optional, default: 0

The y coordinates of the nodes defining the second curve.

定义第二条曲线的节点的y坐标

where : array of bool (length N), optional, default: None

Define where to exclude some horizontal regions from being filled. The filled regions are defined by the coordinates x[where]. More precisely, fill between x[i] and x[i+1] if where[i] and where[i+1]. Note that this definition implies that an isolated True value between two False values in where will not result in filling. Both sides of the True position remain unfilled due to the adjacent False values.

定义其中从充满排除某些水平区域。填充区域由坐标定义 x[where]。更确切地说,填写之间x[i]x[i+1] if 。请注意,这个定义意味着一个孤立的真实两者之间的价值价值观 在那里不会导致填充。 由于相邻的False值,True位置的两侧仍未填充。where[i] and where[i+1]

interpolate : bool, optional

This option is only relvant if where is used and the two curves are crossing each other.

Semantically, where is often used for y1 > y2 or similar. By default, the nodes of the polygon defining the filled region will only be placed at the positions in the x array. Such a polygon cannot describe the above semantics close to the intersection. The x-sections containing the intersecion are simply clipped.

Setting interpolate to True will calculate the actual interscection point and extend the filled region up to this point.

如果使用where并且两条曲线相互交叉则此选项仅为relvant 。

语义上,在那里经常用于Y1 > Y2或相似。默认情况下,定义填充区域的多边形节点将仅放置在x数组中的位置。这样的多边形无法描述靠近交叉点的上述语义。包含交叉点的x部分被简单地修剪。

interpolate设置为True将计算实际的截距点并将填充区域扩展到此点。

step : {'pre', 'post', 'mid'}, optional

Define step if the filling should be a step function, i.e. constant in between x. The value determines where the step will occur:如果填充应该是阶梯函数,即在x之间保持不变,则定义步骤。该值确定步骤的发生位置:

  • 'pre': The y value is continued constantly to the left from every x position, i.e. the interval (x[i-1], x[i]] has the value y[i].y值从每个x位置恒定地向左连续,即间隔(x [i-1],x [i])具有值y [i]
  • 'post': The y value is continued constantly to the right from every x position, i.e. the interval [x[i], x[i+1]) has the value y[i].y值从每个x位置向右持续向右,即区间(x [i],x [i+1])具有值y [i]
  • 'mid': Steps occur half-way between the x positions.步骤发生在x位置的中间位置。
Returns:

`.PolyCollection`

PolyCollection containing the plotted polygons.

其他参数:

**kwargs

All other keyword arguments are passed on to PolyCollection. They control the Polygon properties:

所有其他关键字参数都传递给PolyCollection。他们控制Polygon属性:

Property Description
agg_filter

a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array

一个过滤函数,它取一个(m, n, 3) 浮点数组和一个dpi值,并返回一个(m, n, 3) 数组

alpha

float or None

浮动或无

animated bool
antialiased or antialiaseds

Boolean or sequence of booleans

布尔值或布尔序列

array ndarray
capstyle unknown
clim

a length 2 sequence of floats; may be overridden in methods that have vmin and vmax kwargs.

长度为2的漂浮物序列; 可能会在有vminvmax kwargs的方法中被覆盖。

clip_box Bbox instance
clip_on bool
clip_path [(PathTransform) | Patch | None]
cmap

a colormap or registered colormap name

色彩映射或注册的色彩映射名称

color

matplotlib color arg or sequence of rgba tuples

matplotlib颜色arg或rgba元组的序列

contains

a callable function

一个可调用的函数

edgecolor or edgecolors

matplotlib color spec or sequence of specs

matplotlib颜色规范或规格序列

facecolor or facecolors

matplotlib color spec or sequence of specs

matplotlib颜色规范或规格序列

figure Figure instance
gid an id string
hatch [ '/' | '\' | '|' | '-' | '+' | 'x' | 'o' | 'O' | '.' | '*' ]
joinstyle unknown
label object
linestyle or dashes or linestyles

['solid' | 'dashed', 'dashdot', 'dotted' | (offset, on-off-dash-seq) | '-' | '--' | '-.' | ':' | 'None' | ' ' | '']

['solid'| '虚线','dashdot','点'| (偏移,开 - 关 - 破折号)|'-''--''-.'':''None'| | ]' '''

linewidth or linewidths or lw

float or sequence of floats

漂浮或漂浮的序列

norm Normalize
offset_position [ 'screen' | 'data' ]
offsets float or sequence of floats
path_effects AbstractPathEffect
picker [None | bool | float | callable]
pickradius float distance in points
rasterized bool or None
sketch_params (scale: float, length: float, randomness: float)
snap bool or None
transform Transform
url a url string
urls List[str] or None
visible bool
zorder float

代码

# Import
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['SimHei']
import seaborn as sns
import matplotlib
matplotlib.rcParams['axes.unicode_minus']=False


# 生产绘图数据
price = pd.Series(np.random.randn(150).cumsum(),
                  index=pd.date_range('2000-1-1', periods=150, freq='B'))

# mean
ma = price.rolling(20).mean()
# std
mstd = price.rolling(20).std()


plt.figure()

plt.plot(price.index, price, 'k')
plt.plot(ma.index, ma, 'b')
plt.fill_between(mstd.index, ma-2*mstd, ma+2*mstd, color='b', alpha=0.2)

详情参考:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.fill_between.html?highlight=fill_between

猜你喜欢

转载自blog.csdn.net/ChenVast/article/details/81904497
今日推荐