Python 3.7:用不同的X轴值填充两行之间的区域,这些区域的Z形曲折很多

我有一个数据集,我想将其不确定性表示为主图周围的填充区域。在x轴方向上的误差很大,因此我无法使用string = 'foo' isString = {it instanceof String} println isString(string) // true // let's try it a different way isString2 = {String.isInstance(it)} println isString2(string) // true // let's try using .& for fancy FP eta reduction isString3 = String.&isInstance println isString3(string) // MethodMissingException // ^^^ No signature of method: java.lang.String.isInstance() // is applicable for argument types: // (java.lang.String) values: [foo] // EDIT: Case #4 // what if we try a method we know does not exist? isnotString = String.&isnotInstance println isnotString(string) // MethodMissingException // ^^^ gives the same error! // EDIT: Case #5 // Maybe I am misunderstanding how the .& operator works - let's try an example bar = 'bar' equalsBar = bar.&equals println equalsBar('bar') // true - same as 'bar'.equals('bar') println equalsBar('baz') // false - same as 'bar'.equals('baz') // the .& operator has worked as expected in this case ,它需要一组通用的x轴。

我曾尝试按照本网站上其他地方的建议使用plt.fill_between(x,y1,y2),但是由于我的数据呈锯齿状变化,所以填充的区域最终看起来像节点(参见附图)。

Python 3.7:用不同的X轴值填充两行之间的区域,这些区域的Z形曲折很多

我要完成的是点对点填充,即对于每个i值填充下限的第i个值和上限的第i个值之间的区域,例如具有实际数据的主线始终在填充区域内。

我当前正在使用的代码附在下面。预先感谢您的建议/帮助!

plt.fill(np.append(x1,x2[::-1]))
last_up_to 回答:Python 3.7:用不同的X轴值填充两行之间的区域,这些区域的Z形曲折很多

这更像您想要的东西吗?

enter image description here

您可以通过将用plt.fill()填充的多边形分解为x_lox_hiy_loy_hi数组的较小部分来创建并像这样分别填充每个段

from matplotlib.colors import to_rgb
color = to_rgb(color_set[i])
for k in range(1,len(x),1):
    xe = [x_lo[k],x_lo[k-1],x_hi[k-1],x_hi[k]]
    ye = [y_lo[k],y_lo[k-1],y_hi[k-1],y_hi[k]]
    plt.fill(xe,ye,fc=(color[0],color[1],color[2],0.3))

这并不是实现此目的的最佳方法,但它可能足以满足您的目的。一种更健壮的方法是使用shapely.ops.cascaded_union

创建一个级联的联合体
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import to_rgb
from matplotlib import patches
from shapely.ops import cascaded_union
from shapely import geometry

# Unchanged code omitted

for i in range(0,len(data)):
    # Unchanged code omitted
    # Set color
    color = to_rgb(color_set[i])
    color = (color[0],0.3)

    # Plot lines
    plt.plot(x,y,linestyle="-",linewidth=1.0,marker="o",markersize=2,color=color)
    # Plot region of error
    polygons = list()
    for k in range(1,1):
        polygons.append(geometry.Polygon([(x_lo[k],y_lo[k]),(x_lo[k-1],y_lo[k-1]),(x_hi[k-1],y_hi[k-1]),(x_hi[k],y_hi[k])]))

    boundary = cascaded_union(polygons)
    x,y = boundary.exterior.xy
    ax.add_patch(patches.Polygon(np.stack([x,y],1),fc=color))

plt.show()

这会给你看起来像这样的东西

enter image description here

本文链接:https://www.f2er.com/2967949.html

大家都在问