获取两条等高线的单个图 matplotlib

obtaining a single plot of two contour lines matplotlib

我正在制作一个程序,用于对一些水平曲线的点进行插值,但是在绘图时,我得到的是两条水平曲线的两个单独的图表,而不是一个图表。

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d

pts1 = np.array([[19.02678991587782, -98.62426964439068] ,[19.02642477902292, -98.62396923697386],[19.02614078313657, -98.62409798300963],[19.025207650377993, -98.62439839042645],
[19.02378765569075, -98.62461296715276],[19.022692222926803, -98.62452713646223],[19.021393922893306, -98.62422672904542],[19.020866485607627, -98.6230680147234],
[19.020978059006985, -98.6220595041113],[19.020795484294528, -98.62195221574815],[19.02058248020984, -98.6220595041113],[19.019923180101493, -98.6228427091539],
[19.019923180101493, -98.62287489566285],[19.019426167537492, -98.6239799658033],[19.01909144395283, -98.62516013779798],[19.018533569789643, -98.62622229253545],
[19.01849299705195, -98.62694112456855],[19.019243591116275, -98.62830368671746],[19.019750747335433, -98.62919418013162],[19.019659459330185, -98.63011686005473],
[19.019618886877918, -98.63087860733337],[19.020136185037668, -98.63175837191123],[19.02097805899266, -98.632090965837],[19.02212421792218, -98.63189784679251],
[19.024102084177514, -98.63043872507744],[19.02554236171496, -98.62930146843671],[19.0258770723203, -98.62851826341256],[19.026232067679466, -98.6269303956773],
[19.02672905989373, -98.62547127397141]])

pts2 = np.array([[19.024832367299116, -98.62688748111249],[19.024548368691026, -98.62624375101424],[19.023899227192743, -98.62615792033446],[19.02260093658879, -98.62590042829517],
[19.0217489278678, -98.62568585159576],[19.02101863120187, -98.6252996135368],[19.020754912182237, -98.62528888442091],[19.020572337215178, -98.62560002091598],
[19.02024775901759, -98.62611500499459],[19.020085469681103, -98.62684456577261],[19.0204100481956, -98.62774578791017],[19.020815770447378, -98.62856117936796],
[19.021262063780405, -98.62911907878645],[19.021262063780405, -98.62976280888472],[19.021434494983918, -98.63030997918734],[19.022022788299633, -98.63035289452722],
[19.022692222987843, -98.62996665646827],[19.023665941356825, -98.62932292637001],[19.024477368972605, -98.62816421219316],[19.024680225257438, -98.6276277704446]])

for lst in pts1, pts2:

    ######## level curve interpolation #######################
    pad = 3       
    lst = np.pad(lst, [(pad,pad), (0,0)], mode='wrap')  
    y,x = lst.T                                                      
    i = np.arange(0, len(lst))
    interp_i = np.linspace(pad, i.max() - pad + 1, 5 * (i.size - 2*pad))
    xi = interp1d(i, x, kind='cubic')(interp_i)
    yi = interp1d(i, y, kind='cubic')(interp_i)

    #grafico de la interpolación
    plt.figure(figsize = (8,8))
    plt.plot(xi, yi, "k")
    plt.title("level curves")
    plt.xlabel("x")
    plt.ylabel("y")
    plt.show()

我想得到这个输出:

您只需在 for 循环之外声明一次 plt.figure()。在 for 循环中,您可以向图中添加元素。最后,在循环外设置轴标签并显示绘图。

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d



pts1 = np.array([[19.02678991587782, -98.62426964439068] ,[19.02642477902292, -98.62396923697386],[19.02614078313657, -98.62409798300963],[19.025207650377993, -98.62439839042645],
[19.02378765569075, -98.62461296715276],[19.022692222926803, -98.62452713646223],[19.021393922893306, -98.62422672904542],[19.020866485607627, -98.6230680147234],
[19.020978059006985, -98.6220595041113],[19.020795484294528, -98.62195221574815],[19.02058248020984, -98.6220595041113],[19.019923180101493, -98.6228427091539],
[19.019923180101493, -98.62287489566285],[19.019426167537492, -98.6239799658033],[19.01909144395283, -98.62516013779798],[19.018533569789643, -98.62622229253545],
[19.01849299705195, -98.62694112456855],[19.019243591116275, -98.62830368671746],[19.019750747335433, -98.62919418013162],[19.019659459330185, -98.63011686005473],
[19.019618886877918, -98.63087860733337],[19.020136185037668, -98.63175837191123],[19.02097805899266, -98.632090965837],[19.02212421792218, -98.63189784679251],
[19.024102084177514, -98.63043872507744],[19.02554236171496, -98.62930146843671],[19.0258770723203, -98.62851826341256],[19.026232067679466, -98.6269303956773],
[19.02672905989373, -98.62547127397141]])

pts2 = np.array([[19.024832367299116, -98.62688748111249],[19.024548368691026, -98.62624375101424],[19.023899227192743, -98.62615792033446],[19.02260093658879, -98.62590042829517],
[19.0217489278678, -98.62568585159576],[19.02101863120187, -98.6252996135368],[19.020754912182237, -98.62528888442091],[19.020572337215178, -98.62560002091598],
[19.02024775901759, -98.62611500499459],[19.020085469681103, -98.62684456577261],[19.0204100481956, -98.62774578791017],[19.020815770447378, -98.62856117936796],
[19.021262063780405, -98.62911907878645],[19.021262063780405, -98.62976280888472],[19.021434494983918, -98.63030997918734],[19.022022788299633, -98.63035289452722],
[19.022692222987843, -98.62996665646827],[19.023665941356825, -98.62932292637001],[19.024477368972605, -98.62816421219316],[19.024680225257438, -98.6276277704446]])


plt.figure(figsize = (8,8))
for lst in pts1, pts2:
    ######## level curve interpolation #######################
    pad = 3       
    lst = np.pad(lst, [(pad,pad), (0,0)], mode='wrap')  
    y,x = lst.T                                                      
    i = np.arange(0, len(lst))
    interp_i = np.linspace(pad, i.max() - pad + 1, 5 * (i.size - 2*pad))
    xi = interp1d(i, x, kind='cubic')(interp_i)
    yi = interp1d(i, y, kind='cubic')(interp_i)

    #grafico de la interpolación
    plt.plot(xi, yi, "k")
    
plt.title("level curves")
plt.xlabel("x")
plt.ylabel("y")
plt.show()