获得两个轮廓线的单个图Matplotlib

发布于 2025-01-31 19:32:04 字数 3017 浏览 3 评论 0原文

我正在制作一个可以插值某种级别曲线点的程序,但是当涉及到图形时,我获得了两个级别曲线的两个单独的图形,而不是一个图。

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

我想获得此输出:

”在此处输入图像说明”

I am making a program that interpolates the points of some level curves, but when it comes to graphing, I am obtaining two individual graphs of the two level curves and not a single graph.

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

I would like to get this output:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滿滿的愛 2025-02-07 19:32:04

您需要声明plt.figure()仅在循环的之外一次。在循环的内部,您将元素添加到图中。最后,在循环外,您设置了轴标签并显示图。

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

You need to declare plt.figure() only once, outside of the for loop. Inside the for loop you add elements to the plot. Finally, outside of the loop you set axis labels and show the plot.

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文