首页 > 解决方案 > Python:根据条件为多边形着色并为多边形提供恒定宽度

问题描述

对不起,我的英语一般,如果我使用了不正确的技术术语,那只是一种爱好。

我用 MatPlotLib 创建了一个 3d 图来显示来自 Garmin 设备的 GPS 数据。根据轨道的坡度,有多个具有不同颜色的多边形。我对结果感到满意,但在我看来,我这样做的方式效率不高:创建一个多边形,使用嵌套循环将其拆分为多个多边形,从每个多边形的颜色图中获取特定颜色, ...

有没有更好的方法可以根据条件在 3d 图中的一条线下获得彩色窗帘?

另外我想给线下的多边形(或对象)一些恒定的宽度。现在它只是房间里的一张卷曲的纸。

带有一些数据的代码如下。

感谢您的意见!

### MatPlotLib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection 
from matplotlib import cm

### NumPy
import numpy as np

###############################################################################
###############################################################################
  
### Function 3D Plot
def plot3d():
  
    ### Data
    longitude_values = [11.576064582914114, 11.576026361435652, 11.576014794409275, 11.576004149392247, 11.576001718640327, 11.576027199625969, 11.576056201010942, 11.576067600399256, 11.576094757765532, 11.57611663453281, 11.576139349490404, 11.57617086544633, 11.576194670051336, 11.576198861002922, 11.576215121895075, 11.576283434405923, 11.576311262324452, 11.576398182660341, 11.57647730782628, 11.576566155999899, 11.5767318662256, 11.576855918392539, 11.576994303613901, 11.577197313308716, 11.57743594609201, 11.577681535854936, 11.577779268845916, 11.577828470617533, 11.578180091455579, 11.578272627666593, 11.578320823609829, 11.578463232144713, 11.578651322051883, 11.578963128849864, 11.579086510464549, 11.579212574288249, 11.579246940091252, 11.579429917037487, 11.579581461846828, 11.579634770751, 11.579778268933296, 11.579825794324279, 11.580003574490547, 11.580156711861491, 11.580243296921253, 11.580299204215407, 11.580498274415731, 11.580694830045104, 11.580774206668139, 11.581002613529563, 11.581115433946252, 11.581309894099832, 11.58138264901936, 11.581614827737212, 11.581870140507817, 11.582055632025003, 11.58235888928175, 11.582465004175901, 11.582796508446336, 11.582908742129803, 11.583242090418935, 11.583574851974845, 11.583913648501039, 11.584140378981829, 11.584253534674644, 11.584567939862609, 11.584863569587469, 11.58501603640616, 11.585076469928026, 11.585230696946383, 11.585316862910986, 11.585504114627838, 11.58591315150261, 11.586121441796422, 11.586430566385388, 11.586537770926952, 11.586651345714927, 11.586767183616757, 11.587000954896212, 11.58712257631123, 11.587248807772994, 11.587378475815058, 11.587510323151946, 11.587645690888166, 11.587918773293495, 11.58805238083005, 11.588179199025035, 11.588295456022024, 11.588397463783622, 11.588555546477437, 11.588752688840032, 11.5888268686831, 11.588897863402963, 11.588967936113477, 11.589034656062722, 11.589296171441674, 11.58970738761127, 11.590109383687377, 11.59031230956316, 11.590717826038599]
    latitude_values = [50.813197530806065, 50.81328931264579, 50.81333616748452, 50.81352811306715, 50.81367521546781, 50.81386431120336, 50.81405625678599, 50.814157761633396, 50.81435976549983, 50.814510472118855, 50.814800234511495, 50.815149592235684, 50.815400294959545, 50.81545150838792, 50.81555485725403, 50.815920643508434, 50.81602206453681, 50.81627863459289, 50.81647929735482, 50.816682474687696, 50.81698715686798, 50.81719318404794, 50.817399797961116, 50.817658212035894, 50.817910423502326, 50.818164898082614, 50.81826958805323, 50.81832197494805, 50.81869128160179, 50.818797228857875, 50.81885020248592, 50.819008788093925, 50.81922546029091, 50.81962091848254, 50.81979132257402, 50.81996449269354, 50.82002593204379, 50.82036984153092, 50.82065708935261, 50.820768317207694, 50.821055732667446, 50.82117106765509, 50.821617655456066, 50.82200339064002, 50.82222467288375, 50.82232089713216, 50.82287737168372, 50.823250114917755, 50.82335136830807, 50.82357818260789, 50.82365839742124, 50.82376576960087, 50.823795190081, 50.823875991627574, 50.82394262775779, 50.82397263497114, 50.82399501465261, 50.82399694249034, 50.82397900521755, 50.823965510353446, 50.82390231080353, 50.82382595166564, 50.82374590449035, 50.82369200885296, 50.8236638456583, 50.82357415929437, 50.82347131334245, 50.82339361310005, 50.82337877713144, 50.823350781574845, 50.823336532339454, 50.82330359145999, 50.823213988915086, 50.82316478714347, 50.823096726089716, 50.82307283766568, 50.8230491168797, 50.823029754683375, 50.82298390567303, 50.822960855439305, 50.82293713465333, 50.822912994772196, 50.82288592122495, 50.82285641692579, 50.82280000671744, 50.82276840694249, 50.82273077219725, 50.82268391735852, 50.822632955387235, 50.82253513857722, 50.82242164760828, 50.82242097705603, 50.822448721155524, 50.82249356433749, 50.82254729233682, 50.8227676525712, 50.82304618321359, 50.82331448793411, 50.823451951146126, 50.82371891476214]
    altitude_values = [176.60000000000002, 177.39999999999998, 177.79999999999995, 179.0, 179.39999999999998, 180.60000000000002, 181.0, 181.20000000000005, 181.79999999999995, 181.79999999999995, 182.60000000000002, 184.0, 184.39999999999998, 184.60000000000002, 184.79999999999995, 185.60000000000002, 185.60000000000002, 186.20000000000005, 186.39999999999998, 186.60000000000002, 186.60000000000002, 186.60000000000002, 186.60000000000002, 186.60000000000002, 186.20000000000005, 185.79999999999995, 185.79999999999995, 185.79999999999995, 185.20000000000005, 185.0, 185.0, 184.60000000000002, 184.20000000000005, 183.39999999999998, 183.39999999999998, 183.0, 183.0, 183.0, 183.0, 183.0, 183.39999999999998, 183.39999999999998, 183.60000000000002, 183.60000000000002, 183.60000000000002, 183.60000000000002, 184.0, 184.20000000000005, 184.20000000000005, 184.39999999999998, 184.79999999999995, 184.79999999999995, 184.79999999999995, 185.20000000000005, 185.79999999999995, 186.0, 186.0, 186.20000000000005, 186.0, 186.0, 185.79999999999995, 184.60000000000002, 184.0, 183.60000000000002, 183.39999999999998, 182.39999999999998, 181.20000000000005, 180.39999999999998, 180.20000000000005, 180.20000000000005, 180.20000000000005, 180.0, 179.79999999999995, 179.79999999999995, 179.0, 178.39999999999998, 178.20000000000005, 178.0, 177.39999999999998, 177.0, 176.79999999999995, 176.60000000000002, 176.20000000000005, 175.79999999999995, 174.79999999999995, 174.0, 173.39999999999998, 172.39999999999998, 171.0, 168.79999999999995, 165.39999999999998, 165.39999999999998, 164.0, 162.60000000000002, 162.0, 161.0, 159.20000000000005, 158.60000000000002, 158.39999999999998, 158.60000000000002] 
    slope_values = [4.98, 6.2, 6.16, 6.76, 5.11, 4.48, 3.26, 3.04, 2.08, 1.47, 1.66, 1.96, 2.43, 2.81, 2.23, 2.42, 1.25, 1.35, 0.97, 1.24, 0.56, 0.28, 0.0, 0.0, -0.4, -0.8, -0.8, -0.39, -0.42, -0.9, -0.9, -1.13, -1.14, -1.68, -1.03, -1.17, -0.63, -0.63, 0.0, 0.0, 0.4, 0.4, 0.53, 0.13, 0.13, 0.0, 0.21, 0.36, 0.36, 0.38, 1.35, 1.35, 1.13, 0.72, 1.76, 2.25, 1.52, 1.37, 0.6, 0.6, -0.55, -1.86, -2.65, -3.17, -2.38, -3.08, -4.27, -5.21, -4.93, -2.96, -1.21, -0.46, -0.68, -0.68, -1.35, -3.47, -4.22, -3.87, -2.64, -3.3, -3.21, -2.76, -2.68, -3.31, -4.3, -5.56, -6.24, -8.39, -11.78, -15.3, -18.2, -12.22, -13.44, -13.14, -15.85, -10.25, -5.26, -3.04, -2.24, -0.65]
     
    ### Normal Zero Polygon
    h = 0
    
    ### Position Data for Polygon
    xs = longitude_values
    ys = latitude_values
    zs = altitude_values
    
    ### Polygon 
    polygon = []
    for k in range(0, len(xs) - 1):
        x = [xs[k], xs[k+1], xs[k+1], xs[k]]
        y = [ys[k], ys[k+1], ys[k+1], ys[k]]
        z = [zs[k], zs[k+1],       h,     h]
        polygon.append(list(zip(x, y, z)))    

    ### Round Slope Values
    slope_values = [int(round(value, 0)) for value in slope_values]

    ### Number of Colors, Color Map, Interval for Color Map 
    num_colors = 31
    cmap = cm.get_cmap("jet")
    min_slope_col = -15     
    max_slope_col = 15

    ### Slope 
    slopes = list(np.linspace(min_slope_col, max_slope_col, num = num_colors))
    
    ### List Splited Polygons
    list_poly_slopes = [[] for x in range(0, num_colors)]
    for i, slo in enumerate(slopes):
        for index, percent in enumerate(slope_values):
            if percent == slo:
                list_poly_slopes[i].append(index)
            elif percent < min_slope_col:
                list_poly_slopes[0].append(index)
            elif percent > max_slope_col:
                list_poly_slopes[-1].append(index)
                        
    ### List with Colors
    list_poly_colors = []
    for item in np.linspace(0, 1, num = num_colors):
        rgba = cmap(item)
        color = [rgba[0], rgba[1], rgba[2]]
        list_poly_colors.append(color)
    
    ### 3D Plot
    fig = plt.figure()
    ax = Axes3D(fig)
    
    ### Polygons
    for poly_slope, poly_color in zip(list_poly_slopes, list_poly_colors):
        if len(poly_slope) != 0:
            ax.add_collection3d(Poly3DCollection([element for index, element in enumerate(polygon) if index in poly_slope], facecolors = poly_color, alpha = 0.5))
        else:
            pass
               
    ### Line on Top of Polygon
    ax.plot(xs, ys, zs, color = "black", alpha = 1)
    
    ### X, Y, Z Limits
    ax.set_xlim([0.9999 * min(xs), 1.0001 * max(xs)])
    ax.set_ylim([0.9999 * min(ys), 1.0001 * max(ys)])
    ax.set_zlim([min(zs), max(zs)])
           
    ### Axes and Grid Off
    plt.axis("off")
    plt.grid(b = None)

    ### Zoom 
    ax.dist = 50
    
    ### Plot Show
    plt.show()
    
###############################################################################
###############################################################################

### Plot
plot3d()

标签: pythonmatplotlibpolygoncolormapgpx

解决方案


推荐阅读