Create video from python plots to look like a moving probability distribution - python

I generate i plots with the code:
for i in range(len(hlist)):
p1 = np.array(hlist[i])
kde = gaussian_kde(p1[:,0], bw_method=.4, weights=p1[:,1])
x = np.linspace(0, 100, 1000)
plt.plot(x, kde(x), color='crimson')
plt.ylim([minmin, maxmax])
plt.show()
The x-axis and the y-axis of all plots are the same
So I want to make a video from all plots and if its possible with transition between each plots so it looks like a moving probability distribution
Full hlist:
hlist = np.array([[[0.0, 0.9003079895267837],
[10.0, 0.000654274453636099],
[15.0, 4.8062543883990662e-05],
[20.0, 0.0078540048399245721],
[25.0, 0.00030775066073687057],
[30.0, 0.0069461270425554486],
[35.0, 0.00038747043199781708],
[40.0, 0.0040570022317705538],
[45.0, 0.01242208947573863],
[50.0, 0.00042730445196067818],
[55.0, 9.2094566445921082e-05],
[60.0, 2.3533619494819096e-17],
[65.0, 6.8535505965721361e-15],
[70.0, 2.3163309687227612e-12],
[75.0, 4.7244481305730235e-14],
[80.0, 3.946900273532476e-15],
[85.0, 1.387482842660183e-12],
[90.0, 1.169953999346581e-10],
[95.0, 9.405541221977168e-06],
[100.0, 0.00020815494180476587]],
[[0.0, 0.093092478749998001],
[10.0, 1.4053195886715078e-07],
[15.0, 2.4433977582117203e-05],
[20.0, 0.00082219857285037044],
[25.0, 2.6948063300386935e-05],
[30.0, 0.093959576222220995],
[35.0, 0.0001656191073324495],
[40.0, 0.0029118221893314833],
[45.0, 0.01564108273403109],
[50.0, 1.6648778174359671e-05],
[55.0, 2.8100410030581244e-07],
[60.0, 6.0753841480616879e-17],
[65.0, 9.8080692203972863e-15],
[70.0, 6.9694730549144334e-17],
[75.0, 5.8326312613461052e-15],
[80.0, 2.4139075497471945e-16],
[85.0, 5.9084558298527006e-13],
[90.0, 1.6711579730178305e-10],
[95.0, 1.5864098433225844e-08],
[100.0, 2.2572886039178069e-08]],
[[0.0, 4.7418107222805056e-05],
[10.0, 0.72289205649751387],
[15.0, 3.308651897500352e-07],
[20.0, 0.076673642863552008],
[25.0, 0.0001501767582823212],
[30.0, 0.03027984877348755],
[35.0, 0.00011292381988867554],
[40.0, 0.00016702076039256828],
[45.0, 0.00081579620071354846],
[50.0, 1.0458962769769357e-05],
[55.0, 5.5802811319507686e-08],
[60.0, 4.1253613398572781e-19],
[65.0, 9.4051858824518925e-18],
[70.0, 1.3368786287741004e-14],
[75.0, 5.9931111040137393e-14],
[80.0, 3.2539929633588511e-15],
[85.0, 1.047255336132784e-15],
[90.0, 1.4017813965595768e-12],
[95.0, 2.8511569193363785e-09],
[100.0, 1.0183773063292469e-10]],
[[0.0, 0.0045609821258978814],
[10.0, 0.0001917787950273313],
[15.0, 0.99771505193318455],
[20.0, 0.0055938427174407005],
[25.0, 0.013201240944903663],
[30.0, 0.018570016510629072],
[35.0, 0.012700626279918454],
[40.0, 0.019933344753975846],
[45.0, 0.15619609747558014],
[50.0, 0.031265415297255877],
[55.0, 0.42528804082092103],
[60.0, 1.3054822476679266e-18],
[65.0, 8.152405640596557e-18],
[70.0, 1.4328231368499457e-17],
[75.0, 1.3158371543509022e-15],
[80.0, 2.0369465623715189e-18],
[85.0, 1.1462339774892004e-13],
[90.0, 4.7664892870840778e-13],
[95.0, 7.2181814960747638e-06],
[100.0, 0.0011762750538733889]],
[[0.0, 1.4364539307817039e-05],
[10.0, 0.0002382809395285733],
[15.0, 1.1335942314786911e-05],
[20.0, 0.52821149082545871],
[25.0, 0.1355189607672023],
[30.0, 0.0013765391762318477],
[35.0, 0.01044957461521267],
[40.0, 0.00056647346484793787],
[45.0, 0.00076442899303849577],
[50.0, 0.032497831121074787],
[55.0, 0.00032304336212375635],
[60.0, 8.7317718742465198e-16],
[65.0, 6.539726198592529e-12],
[70.0, 9.2846001586370999e-08],
[75.0, 1.1275715667447136e-16],
[80.0, 9.3449145004368205e-16],
[85.0, 7.6700176188571133e-13],
[90.0, 3.6113266426134806e-09],
[95.0, 8.8657633159259348e-07],
[100.0, 1.0088131613242799e-08]],
[[0.0, 0.00010259026745556786],
[10.0, 2.8416604131363134e-05],
[15.0, 1.5265724585429075e-08],
[20.0, 0.00035572734341432835],
[25.0, 0.74774814405543055],
[30.0, 0.20170120434943195],
[35.0, 0.31688948032690523],
[40.0, 0.016495873067229414],
[45.0, 0.019452385467953772],
[50.0, 0.0063096344918544449],
[55.0, 1.4586177114635856e-07],
[60.0, 1.2622740425026807e-15],
[65.0, 1.6230117141451513e-13],
[70.0, 2.7328268483555935e-15],
[75.0, 5.9099377394502244e-17],
[80.0, 1.0947896137035609e-17],
[85.0, 2.2319783866749845e-12],
[90.0, 7.3069777072490827e-10],
[95.0, 1.2982219252486507e-07],
[100.0, 9.2703590554295812e-09]],
[[0.0, 5.6467274191670147e-06],
[10.0, 1.9311914301356571e-15],
[15.0, 0.00030373592016130291],
[20.0, 0.37222022990454917],
[25.0, 7.1237862206873578e-07],
[30.0, 0.016227799829528728],
[35.0, 0.33096651567634761],
[40.0, 0.00014104696271763757],
[45.0, 0.001730151916132917],
[50.0, 0.00019647094586097279],
[55.0, 1.61437439554684e-14],
[60.0, 7.1139359284635841e-65],
[65.0, 8.3871906097721264e-63],
[70.0, 1.2336993844192023e-53],
[75.0, 5.0051598531858931e-64],
[80.0, 2.9758606051614185e-62],
[85.0, 2.8460978574131379e-51],
[90.0, 2.8677742500204446e-37],
[95.0, 3.7194535849027315e-27],
[100.0, 2.3439805162091607e-24]],
[[0.0, 0.0001430560406790707],
[10.0, 6.2797052001508247e-13],
[15.0, 4.8114669550502021e-06],
[20.0, 0.0007443231772534647],
[25.0, 0.00061070912573869406],
[30.0, 0.48116582167944905],
[35.0, 0.24698643991977953],
[40.0, 0.016407283121225951],
[45.0, 0.2557158314329116],
[50.0, 1.1252231121357235e-05],
[55.0, 0.064666668633158647],
[60.0, 1.7631447655837744e-17],
[65.0, 1.1294722466816786e-14],
[70.0, 2.9419020411134367e-16],
[75.0, 3.0887653014525822e-17],
[80.0, 4.4973693062706866e-17],
[85.0, 9.0975358174005147e-15],
[90.0, 1.0758266454985257e-10],
[95.0, 7.2923752473657924e-08],
[100.0, 1.8065366882584036e-08]],
[[0.0, 8.4950625072720679e-07],
[10.0, 1.1421138441091606e-09],
[15.0, 4.5866299179754217e-05],
[20.0, 1.5172948619703533e-06],
[25.0, 0.00052404930089682738],
[30.0, 0.0013085043037665261],
[35.0, 0.0011664239519834339],
[40.0, 0.82750281205997822],
[45.0, 0.10132991062898908],
[50.0, 0.0085433968529198406],
[55.0, 1.4344253354768514e-09],
[60.0, 4.1465759737420333e-63],
[65.0, 8.8542318068147446e-61],
[70.0, 6.5220929006588264e-57],
[75.0, 4.8147063172017478e-62],
[80.0, 1.6649888561929801e-54],
[85.0, 5.8101309745184615e-51],
[90.0, 1.0489469511522901e-34],
[95.0, 1.4172891722901137e-26],
[100.0, 7.7737342215176175e-21]],
[[0.0, 3.1632028979839915e-05],
[10.0, 0.22111326362785799],
[15.0, 5.0749184816502276e-05],
[20.0, 0.0037647084552139601],
[25.0, 0.06595171605012351],
[30.0, 0.020353996100110065],
[35.0, 0.064862755938758426],
[40.0, 0.063464515818315706],
[45.0, 0.28662321979050187],
[50.0, 0.29871027240136533],
[55.0, 0.0080037249999781925],
[60.0, 4.7989170718538567e-08],
[65.0, 7.1337119197448125e-13],
[70.0, 2.1156334756020827e-14],
[75.0, 5.2206045634456785e-13],
[80.0, 3.4493997391226592e-14],
[85.0, 1.0500710333351341e-12],
[90.0, 3.6230960591527929e-10],
[95.0, 5.4508034466239748e-07],
[100.0, 1.113734841284331e-08]],
[[0.0, 2.1072826422141414e-05],
[10.0, 5.2005089026009156e-06],
[15.0, 0.00035632479168291582],
[20.0, 0.00020117324964893726],
[25.0, 0.025321662490265816],
[30.0, 0.019375041125499575],
[35.0, 0.0072489358231882196],
[40.0, 0.027646703948782582],
[45.0, 0.061781297188577035],
[50.0, 0.61650443678873623],
[55.0, 6.2548100028575296e-10],
[60.0, 4.3624321149810504e-14],
[65.0, 2.1028732991018519e-15],
[70.0, 7.8258766740408378e-09],
[75.0, 2.3541542464960154e-15],
[80.0, 7.1311782140494435e-18],
[85.0, 7.9730756880702952e-14],
[90.0, 8.2168425453267622e-12],
[95.0, 2.7060634831745863e-07],
[100.0, 2.5445845858147043e-06]],
[[0.0, 5.2908114997246604e-05],
[10.0, 0.01017193185840104],
[15.0, 1.9357672782766133e-07],
[20.0, 0.00033902736145621922],
[25.0, 2.6389276490704271e-05],
[30.0, 0.018976734061801625],
[35.0, 0.00019847993445041843],
[40.0, 0.0003987651813445142],
[45.0, 0.00077280097799644102],
[50.0, 7.0623545425863526e-06],
[55.0, 0.49987468099653093],
[60.0, 2.2781104904754321e-10],
[65.0, 3.6325273589160318e-08],
[70.0, 1.3537824953350784e-07],
[75.0, 1.6173509221250697e-06],
[80.0, 1.4416343176395238e-08],
[85.0, 4.1563319348145668e-08],
[90.0, 3.889471307678879e-07],
[95.0, 5.5104492670400779e-05],
[100.0, 2.7151502910109198e-05]],
[[0.0, 6.7973224793022393e-05],
[10.0, 2.4433844497448081e-05],
[15.0, 3.1104921306578064e-06],
[20.0, 0.00034408444849371377],
[25.0, 0.00023308492218191118],
[30.0, 0.011131477320772989],
[35.0, 0.00065907967432373565],
[40.0, 0.0021975971207933783],
[45.0, 0.0064663180932303744],
[50.0, 0.00022182805626473468],
[55.0, 3.0364808672527216e-05],
[60.0, 0.5088867111766856],
[65.0, 0.022878262885332335],
[70.0, 4.5920971324139236e-06],
[75.0, 0.0022218172448998821],
[80.0, 6.2738973247353222e-07],
[85.0, 0.00029845009911656876],
[90.0, 0.00010489918264551884],
[95.0, 0.00060753276898116491],
[100.0, 0.00022654293893088778]],
[[0.0, 3.9706684562645418e-06],
[10.0, 5.1270388737533639e-06],
[15.0, 5.262507305695827e-08],
[20.0, 2.5999077691490454e-05],
[25.0, 2.7813624200874083e-05],
[30.0, 0.0031991625978026266],
[35.0, 7.9249804143628584e-05],
[40.0, 0.00041177406702603777],
[45.0, 0.0011392701023576817],
[50.0, 8.0323465327795955e-06],
[55.0, 4.8154250431354482e-08],
[60.0, 0.49105543193823659],
[65.0, 0.7287168056069141],
[70.0, 0.001517224119720642],
[75.0, 0.011333832180200976],
[80.0, 1.72411085405819e-06],
[85.0, 0.0024773347817556406],
[90.0, 0.0051538392104878905],
[95.0, 0.0012157783163112316],
[100.0, 6.9627245730982824e-06]],
[[0.0, 0.0002912083712195997],
[10.0, 0.00010000827323660753],
[15.0, 1.3045847050921363e-05],
[20.0, 0.00076214254175277312],
[25.0, 0.00069053586992726265],
[30.0, 0.019496501720614564],
[35.0, 0.0015575117915095839],
[40.0, 0.0061672295672820353],
[45.0, 0.024249792592964857],
[50.0, 0.00032644214446402552],
[55.0, 7.7266514916862301e-06],
[60.0, 7.7205272501591181e-06],
[65.0, 0.017897081479520592],
[70.0, 0.99798778384755971],
[75.0, 1.8624562046616415e-05],
[80.0, 0.0011212071784075478],
[85.0, 1.4365766090813676e-05],
[90.0, 0.0032064950461695454],
[95.0, 0.0012746996733909491],
[100.0, 0.002184674454638652]],
[[0.0, 1.4875831960944082e-05],
[10.0, 0.044356141901610378],
[15.0, 6.2599114973306784e-07],
[20.0, 0.0001137512258409283],
[25.0, 0.00074658171939148493],
[30.0, 0.0026392679644018989],
[35.0, 0.00016209963633384973],
[40.0, 0.00078042928094971609],
[45.0, 0.0036289420011120584],
[50.0, 0.0015766136061278139],
[55.0, 1.0905709141111546e-05],
[60.0, 2.7323842104187985e-06],
[65.0, 3.3133452546150147e-05],
[70.0, 9.4042250154063172e-07],
[75.0, 0.97971055505602644],
[80.0, 0.072870311800762033],
[85.0, 0.00053021457261792214],
[90.0, 0.022423571257812192],
[95.0, 0.00070111679082283159],
[100.0, 3.6497263003914267e-05]],
[[0.0, 3.5864031562522839e-05],
[10.0, 8.3611261574776709e-07],
[15.0, 3.1900347488304889e-06],
[20.0, 0.00018123337202990332],
[25.0, 0.0002136705754060659],
[30.0, 0.0057251370200655845],
[35.0, 0.0012906924788080578],
[40.0, 0.0025502956186607153],
[45.0, 0.01424309136060831],
[50.0, 6.8216937833192329e-05],
[55.0, 5.6413951413567533e-06],
[60.0, 3.395689693128361e-05],
[65.0, 0.0032457520420234795],
[70.0, 8.3915675399849457e-09],
[75.0, 0.00013330080558625364],
[80.0, 0.91501828269209085],
[85.0, 0.0020937770133090659],
[90.0, 0.0012871409503378462],
[95.0, 0.00014758805743257718],
[100.0, 0.00086787665688324784]],
[[0.0, 4.1652247577331996e-06],
[10.0, 1.2212829713673957e-06],
[15.0, 6.5906857192417344e-08],
[20.0, 0.00016745946587138236],
[25.0, 0.0054431111796765554],
[30.0, 0.0067575214586160616],
[35.0, 0.00011856110316632124],
[40.0, 0.00032181662132509944],
[45.0, 0.001397981055516994],
[50.0, 0.0027058954834684062],
[55.0, 2.553142406703067e-06],
[60.0, 1.1514033594755017e-08],
[65.0, 0.21961568282994792],
[70.0, 2.4658349829099807e-08],
[75.0, 0.0022850986575076743],
[80.0, 3.5603047823624507e-06],
[85.0, 0.99406392082894734],
[90.0, 0.24399923235645221],
[95.0, 0.0013470125217945798],
[100.0, 0.042582366972883985]],
[[0.0, 5.5827483149344828e-06],
[10.0, 4.2094534384536198e-07],
[15.0, 5.723201102401161e-08],
[20.0, 4.1809740042541515e-05],
[25.0, 0.00049057556903029373],
[30.0, 0.0073861106939193556],
[35.0, 0.00025382321327730041],
[40.0, 0.0013088484764716428],
[45.0, 0.0026974167667276621],
[50.0, 3.069464890989831e-05],
[55.0, 5.8148299885777761e-06],
[60.0, 7.4621165800794304e-07],
[65.0, 0.002374761247356731],
[70.0, 7.989880181871685e-05],
[75.0, 0.0042580794657908335],
[80.0, 0.010965726532792501],
[85.0, 0.00032799281228739028],
[90.0, 0.72335894920682331],
[95.0, 0.0028560935416300647],
[100.0, 0.00023222597525614469]],
[[0.0, 1.1150563179140846e-05],
[10.0, 3.9137754880943888e-06],
[15.0, 2.6540828280675093e-07],
[20.0, 0.00030085951063170473],
[25.0, 0.00099029668428149789],
[30.0, 0.0040087490203686207],
[35.0, 0.00086288885669378408],
[40.0, 0.0012383336640490626],
[45.0, 0.0024569535179785368],
[50.0, 5.3078640889812177e-05],
[55.0, 3.655855476404959e-05],
[60.0, 1.135303263695363e-05],
[65.0, 0.0052337161196729541],
[70.0, 0.00038799120104653003],
[75.0, 6.6611573170762462e-07],
[80.0, 1.551926769812043e-05],
[85.0, 3.0383118936603649e-05],
[90.0, 0.00045596510625991982],
[95.0, 0.97142691361436007],
[100.0, 0.10797398637691005]],
[[0.0, 0.0011842207743420531],
[10.0, 0.00021255186566113124],
[15.0, 0.0014186746952926585],
[20.0, 0.0012807740120210007],
[25.0, 0.0017758699839103857],
[30.0, 0.02941486302872582],
[35.0, 0.0028808476159808511],
[40.0, 0.0053310120235299015],
[45.0, 0.030475142227338822],
[50.0, 0.00050901345787313437],
[55.0, 0.0016516486463798513],
[60.0, 1.288101329664901e-06],
[65.0, 4.7680039665889616e-06],
[70.0, 2.1300407821392148e-05],
[75.0, 3.6408560648746925e-05],
[80.0, 3.0263064939939249e-06],
[85.0, 0.00016351943738745334],
[90.0, 9.5136297575506478e-06],
[95.0, 0.020349612775663113],
[100.0, 0.84447466931781623]]])

With plt.savefig you can create an individual .png file from a plot. To get a smooth transition, you can interpolate the plots in a number of steps. To convert the .png-files to an animation, ffmpeg is a very useful tool.
Here is some code starting from the data of your previous question:
from matplotlib import pyplot as plt
import numpy as np
from scipy.stats import gaussian_kde
p1 = np.array([[5.0, 0.0001430560406790707], [10.0, 6.2797052001508247e-13], [15.0, 4.8114669550502021e-06],
[20.0, 0.0007443231772534647], [25.0, 0.00061070912573869406], [30.0, 0.48116582167944905],
[35.0, 0.24698643991977953], [40.0, 0.016407283121225951], [45.0, 0.2557158314329116],
[50.0, 1.1252231121357235e-05], [55.0, 0.064666668633158647], [60.0, 1.7631447655837744e-17],
[65.0, 1.1294722466816786e-14], [70.0, 2.9419020411134367e-16], [75.0, 3.0887653014525822e-17],
[80.0, 4.4973693062706866e-17], [85.0, 9.0975358174005147e-15], [90.0, 1.0758266454985257e-10],
[95.0, 7.2923752473657924e-08], [100.0, 1.8065366882584036e-08]])
p2 = np.array([[5.0, 4.1652247577331996e-06], [10.0, 1.2212829713673957e-06], [15.0, 6.5906857192417344e-08],
[20.0, 0.00016745946587138236], [25.0, 0.0054431111796765554], [30.0, 0.0067575214586160616],
[35.0, 0.00011856110316632124], [40.0, 0.00032181662132509944], [45.0, 0.001397981055516994],
[50.0, 0.0027058954834684062], [55.0, 2.553142406703067e-06], [60.0, 1.1514033594755017e-08],
[65.0, 0.21961568282994792], [70.0, 2.4658349829099807e-08], [75.0, 0.0022850986575076743],
[80.0, 3.5603047823624507e-06], [85.0, 0.99406392082894734], [90.0, 0.24399923235645221],
[95.0, 0.0013470125217945798], [100.0, 0.042582366972883985]])
x = np.linspace(0, 100, 1000)
hlist = [p1, p2, p1]
kdelist = [gaussian_kde(p[:, 0], bw_method=.4, weights=p[:, 1]) for p in hlist]
steps = 20
fig, ax = plt.subplots()
for i in range(len(kdelist) - 1):
for s in range(steps + 1):
plt.plot(x, kdelist[i](x) * s / steps + kdelist[i + 1](x) * (1 - s / steps), color='crimson')
plt.ylim(0, 0.065)
plt.savefig(f'kde_{i*(steps+1)+s:04d}.png')
plt.cla() # needed to remove the plot because savefig doesn't clear it
An example way to convert the .png files to a .mp4 animation and then to an animated gif:
ffmpeg -i kde_%04d.png -c:v libx264 -r 25 -pix_fmt yuv420p out.mp4
ffmpeg -i out.mp4 out.gif

Related

Generate probability distribution or smoothing plot from points containing probabilities

I have points which include the probability on the y-axis and values on the x-axis, like:
p1 =
[[0.0, 0.0001430560406790707],
[10.0, 6.2797052001508247e-13],
[15.0, 4.8114669550502021e-06],
[20.0, 0.0007443231772534647],
[25.0, 0.00061070912573869406],
[30.0, 0.48116582167944905],
[35.0, 0.24698643991977953],
[40.0, 0.016407283121225951],
[45.0, 0.2557158314329116],
[50.0, 1.1252231121357235e-05],
[55.0, 0.064666668633158647],
[60.0, 1.7631447655837744e-17],
[65.0, 1.1294722466816786e-14],
[70.0, 2.9419020411134367e-16],
[75.0, 3.0887653014525822e-17],
[80.0, 4.4973693062706866e-17],
[85.0, 9.0975358174005147e-15],
[90.0, 1.0758266454985257e-10],
[95.0, 7.2923752473657924e-08],
[100.0, 1.8065366882584036e-08]]
p2 =
[[0.0, 4.1652247577331996e-06],
[10.0, 1.2212829713673957e-06],
[15.0, 6.5906857192417344e-08],
[20.0, 0.00016745946587138236],
[25.0, 0.0054431111796765554],
[30.0, 0.0067575214586160616],
[35.0, 0.00011856110316632124],
[40.0, 0.00032181662132509944],
[45.0, 0.001397981055516994],
[50.0, 0.0027058954834684062],
[55.0, 2.553142406703067e-06],
[60.0, 1.1514033594755017e-08],
[65.0, 0.21961568282994792],
[70.0, 2.4658349829099807e-08],
[75.0, 0.0022850986575076743],
[80.0, 3.5603047823624507e-06],
[85.0, 0.99406392082894734],
[90.0, 0.24399923235645221],
[95.0, 0.0013470125217945798],
[100.0, 0.042582366972883985]]
Now I want to generate a probability distribution from the points, where the x-axis values are (0,10,15,20,...,100) and the y-axis values contain the probabilities (0.00014,....)
When using the plt.plot fuction I get:
plt.plot([item[0] for item in p1],[item[1] for item in p1])
And for p2:
plt.plot([item[0] for item in p2],[item[1] for item in p2])
I want to get a more smooth visualization, like a probability distribution:
And if a probability distribution is not possible, then a smoothing spline:
Scipy's gaussian_kde is often used to smoothly approximate a probability distribution. It sums a gaussian kernel for each input point. Usually individual measurements are used as inputs, but the weights parameter allows working with binned data. The function is normalized to have its integral equal to one.
This approach assumes the values of p1 and p2 are meant as a mean for the segment around each x-value, similar to a histogram. I.e. a step function where the x-values identify the end of each step.
from matplotlib import pyplot as plt
import numpy as np
from scipy.stats import gaussian_kde
p1 = np.array([[0.0, 0.0001430560406790707],
[10.0, 6.2797052001508247e-13],
[15.0, 4.8114669550502021e-06],
[20.0, 0.0007443231772534647],
[25.0, 0.00061070912573869406],
[30.0, 0.48116582167944905],
[35.0, 0.24698643991977953],
[40.0, 0.016407283121225951],
[45.0, 0.2557158314329116],
[50.0, 1.1252231121357235e-05],
[55.0, 0.064666668633158647],
[60.0, 1.7631447655837744e-17],
[65.0, 1.1294722466816786e-14],
[70.0, 2.9419020411134367e-16],
[75.0, 3.0887653014525822e-17],
[80.0, 4.4973693062706866e-17],
[85.0, 9.0975358174005147e-15],
[90.0, 1.0758266454985257e-10],
[95.0, 7.2923752473657924e-08],
[100.0, 1.8065366882584036e-08]])
p2 = np.array([[0.0, 4.1652247577331996e-06],
[10.0, 1.2212829713673957e-06],
[15.0, 6.5906857192417344e-08],
[20.0, 0.00016745946587138236],
[25.0, 0.0054431111796765554],
[30.0, 0.0067575214586160616],
[35.0, 0.00011856110316632124],
[40.0, 0.00032181662132509944],
[45.0, 0.001397981055516994],
[50.0, 0.0027058954834684062],
[55.0, 2.553142406703067e-06],
[60.0, 1.1514033594755017e-08],
[65.0, 0.21961568282994792],
[70.0, 2.4658349829099807e-08],
[75.0, 0.0022850986575076743],
[80.0, 3.5603047823624507e-06],
[85.0, 0.99406392082894734],
[90.0, 0.24399923235645221],
[95.0, 0.0013470125217945798],
[100.0, 0.042582366972883985]])
x = np.linspace(0, 100, 1000)
fig, axes = plt.subplots(ncols=2)
for ax, p in zip(axes, [p1, p2]):
p[0, 0] = 5.0 # let each x-value be the end of a segment
ax.step(p[:,0], p[:,1], color='dodgerblue', lw=1, ls=':', where='pre')
ax2 = ax.twinx()
kde = gaussian_kde(p[:,0]-2.5, bw_method=.25, weights=p[:,1])
ax2.plot(x, kde(x), color='crimson')
plt.show()

Construct list of consecutive pairs from list of lists

I have an input of the form:
[[41.0, 42.0], [42.0, 17.0], [17.0, 46.0], [46.0, 47.0], [33.0, 34.0], [34.0, 35.0], [35.0, 36.0], [36.0, 7.0], [36.0, 6.0], [6.0, 7.0], [7.0, 12.0], [12.0, 17.0], [17.0, 29.0], [29.0, 30.0]]
How can I sort these pairs and keep just the consecutive ones (by consecutive I mean [a, b] where b is the next element, larger than a, but not necessarily in an increment of 1.).
EDIT: My attempt:
test_lst = [[41.0, 42.0], [42.0, 17.0], [17.0, 46.0], [46.0, 47.0], [33.0, 34.0], [34.0, 35.0], [35.0, 36.0], [36.0, 7.0], [36.0, 6.0], [6.0, 7.0], [7.0, 12.0], [12.0, 17.0], [17.0, 29.0], [29.0, 30.0]]
new_lst = []
for elem in test_lst:
if elem[1] > elem[0]:
new_lst.append(elem)
where new_lst looks like:
[[41.0, 42.0], [17.0, 46.0], [46.0, 47.0], [33.0, 34.0], [34.0, 35.0], [35.0, 36.0], [6.0, 7.0], [7.0, 12.0], [12.0, 17.0], [17.0, 29.0], [29.0, 30.0]]
Now, for example, for the groups [17.0, 46.0] and [17.0, 29.0], I want to keep just the second one in my list, so the one with the smallest difference between elem[1] and elem[0].
EDIT 2 :
test_lst = [[41.0, 42.0], [42.0, 17.0], [17.0, 46.0], [46.0, 47.0], [33.0, 34.0], [34.0, 35.0], [35.0, 36.0], [36.0, 7.0], [36.0, 6.0], [6.0, 7.0], [7.0, 12.0], [12.0, 17.0], [17.0, 29.0], [29.0, 30.0]]
sorted_lst = sorted(test_lst, key = lambda x: int(x[0]))
which gives:
[[6.0, 7.0], [7.0, 12.0], [12.0, 17.0], [17.0, 46.0], [17.0, 29.0], [29.0, 30.0], [33.0, 34.0], [34.0, 35.0], [35.0, 36.0], [36.0, 7.0], [36.0, 6.0], [41.0, 42.0], [42.0, 17.0], [46.0, 47.0]]
Now all I need to do is, in the case of repeating first element, keep the pair with the smallest second element. How can I do that?
Since there are floats involved you should take some precautions when checking for exact equality:
e = 0.0000001 # some epsilon value accounting for imprecision
sorted(x for x in lst if abs(x[1]-x[0]-1) <= e)
# [[6.0, 7.0], [29.0, 30.0], [33.0, 34.0], [34.0, 35.0], [35.0, 36.0], [41.0, 42.0], [46.0, 47.0]]
Use list comprehension:
>>> [x for x in a if x[0] + 1 == x[1]]
[[41.0, 42.0], [46.0, 47.0], [33.0, 34.0], [34.0, 35.0], [35.0, 36.0], [6.0, 7.0], [29.0, 30.0]]
Then sort it:
>>> sorted([x for x in a if x[0] + 1 == x[1]], key=lambda x: x[0])
[[6.0, 7.0], [29.0, 30.0], [33.0, 34.0], [34.0, 35.0], [35.0, 36.0], [41.0, 42.0], [46.0, 47.0]]
outer_list = [[41.0, 42.0], [42.0, 17.0], [17.0, 46.0], [46.0, 47.0], [33.0, 34.0], [34.0, 35.0], [35.0, 36.0], [36.0, 7.0], [36.0, 6.0], [6.0, 7.0], [7.0, 12.0], [12.0, 17.0], [17.0, 29.0], [29.0, 30.0]]
new_outer_list = []
for inner_list in outer_list:
if inner_list[0]+1 == inner_list[1]:
new_outer_list.append(inner_list)
You can do something like:
values = [[41.0, 42.0], [42.0, 17.0], [17.0, 46.0], [46.0, 47.0], [33.0, 34.0], [34.0, 35.0], [35.0, 36.0], [36.0, 7.0], [36.0, 6.0], [6.0, 7.0], [7.0, 12.0], [12.0, 17.0], [17.0, 29.0], [29.0, 30.0]]
sortedValues = []
for value in values:
if value[1] > value[0]:
sortedValues.append(value)
print(sortedValues)
You can pair the adjacent items in the list by zipping the list with itself but with one index apart, and since you apparently want the last item despite it not having a next item to compare to, you can manually add it to the list after reversing it so that it will always match. The following example assumes that your list is stored in variable lst:
[[a, b] for (a, b), (n, _) in zip(lst, lst[1:] + [lst[-1][::-1]]) if b == n and b > a]
This returns:
[[41.0, 42.0], [17.0, 46.0], [33.0, 34.0], [34.0, 35.0], [35.0, 36.0], [6.0, 7.0], [7.0, 12.0], [12.0, 17.0], [17.0, 29.0], [29.0, 30.0]]
Note that the third item [46.0, 47.0] in your expected output is incorrect because it is actually followed by [33.0, 34.0] in your original list, whose first element is not 47.0.
And if you want to keep the items with the smallest second item when the first items duplicate, you can sort the list by the first item first, and the second item in reverse order, so that you can use the dict constructor to convert the list of two-item sublists into a dict to eliminate duplicates, and then convert the dict items to a list of sublists by mapping them to the list constructor:
list(map(list, dict(sorted([(a, b) for (a, b), (n, _) in zip(lst, lst[1:] + [lst[-1][::-1]]) if b == n and b > a], key=lambda t: (t[0], -t[1]))).items()))
This returns:
[[6.0, 7.0], [7.0, 12.0], [12.0, 17.0], [17.0, 29.0], [29.0, 30.0], [33.0, 34.0], [34.0, 35.0], [35.0, 36.0], [41.0, 42.0]]

Histogram hide empty bins

I want to illustrate nicely how often (y-axis) a certain output (x-axis) occurs...
My code produces following plot:
It's not good, because the values are rounded to integers apparently, e.g., there are not over a 100 outputs with 100%, but actually most of them are 99% I think.
The code:
#!/usr/bin/env python3
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
trajectoryIds = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0, 60.0, 61.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0, 70.0, 71.0, 72.0, 73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0, 80.0, 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0, 89.0, 90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0, 100.0, 101.0, 102.0, 103.0, 104.0, 105.0, 106.0, 107.0, 108.0, 109.0, 110.0, 111.0, 112.0, 113.0, 114.0, 115.0, 116.0, 117.0, 118.0, 119.0, 120.0, 121.0, 122.0, 123.0, 124.0, 125.0, 126.0, 127.0, 128.0, 129.0, 130.0, 131.0, 132.0, 133.0, 134.0, 135.0, 136.0, 137.0, 138.0, 139.0, 140.0, 141.0, 142.0, 143.0, 144.0, 145.0, 146.0, 147.0, 148.0, 149.0, 150.0, 151.0, 152.0, 153.0, 154.0, 155.0, 156.0, 157.0, 158.0, 159.0, 160.0, 161.0, 162.0, 163.0, 164.0, 165.0, 166.0, 167.0, 168.0, 169.0, 170.0, 171.0, 172.0, 173.0, 174.0, 175.0, 176.0, 177.0, 178.0, 179.0, 180.0, 181.0, 182.0, 183.0, 184.0, 185.0, 186.0, 187.0, 188.0, 189.0, 190.0, 191.0, 192.0, 193.0, 194.0, 195.0, 196.0, 197.0, 198.0]
avgSolutionPercentages = [20.6256, 99.1448, 15.6764, 21.8231, 16.3733, 17.7502, 20.0055, 86.6873, 11.3105, 15.6693, 10.3449, 81.8921, 11.6745, 92.6031, 11.8787, 23.0229, 37.9636, 2.3903, 15.1727, 14.7088, 10.0426, 59.6758, 8.0042, 12.4174, 10.0585, 46.0567, 90.2376, 98.3273, 52.8645, 49.3027, 62.4136, 32.6199, 19.0642, 10.3319, 74.6157, 22.5771, 22.4118, 11.2017, 16.5053, 11.2021, 30.8376, 24.5255, 83.1072, 10.1529, 14.3991, 46.3459, 16.2137, 4.5773, 44.9549, 1.0719, 76.5605, 42.6589, 13.6209, 34.2856, 1.3574, 29.0465, 66.8146, 16.4796, 32.9564, 62.0732, 3.7047, 13.8828, 31.6088, 60.1141, 3.3247, 45.0796, 13.7862, 26.4498, 93.6806, 10.3245, 62.5157, 10.9833, 42.5908, 37.3208, 27.4115, 84.1648, 13.9058, 13.9065, 67.8918, 27.9075, 3.6116, 10.9091, 41.0988, 24.2177, 50.2762, 61.3869, 15.5915, 27.6536, 0.7993, 22.9483, 22.3393, 88.1832, 25.1604, 18.3625, 15.7212, 56.9646, 4.0434, 11.8431, 56.0613, 32.5472, 97.8757, 21.8233, 14.8162, 38.8259, 20.5676, 72.7201, 17.7987, 35.8117, 15.1699, 17.0359, 14.0621, 35.9655, 11.9095, 10.5691, 23.3259, 16.1746, 10.1936, 12.5084, 24.1494, 16.4727, 21.0687, 15.7495, 28.8929, 11.0135, 13.3133, 14.6639, 50.1304, 21.0346, 5.1604, 53.5107, 20.0712, 41.5111, 12.1633, 74.3263, 17.7904, 17.1684, 25.3977, 21.5871, 21.9332, 22.6674, 36.6634, 99.1179, 15.3213, 16.3999, 12.0147, 57.5163, 4.2062, 17.3874, 10.7132, 17.4919, 17.8457, 29.3538, 26.1468, 75.1234, 16.4368, 21.6191, 61.1394, 12.9972, 73.5746, 72.5788, 41.6835, 39.9912, 20.1648, 11.7097, 11.5203, 36.7387, 5.0694, 30.8129, 12.0922, 22.5419, 12.3569, 54.6776, 28.3561, 26.1219, 44.7455, 1.3281, 46.5064, 13.6016, 23.5483, 11.7151, 44.3669, 3.2577, 75.0943, 10.8634, 14.8226, 45.7661, 19.7319, 30.7981, 3.5965, 47.8161, 14.5996, 39.4484, 13.0693, 24.9947, 97.4253, 76.7901, 73.1183, 4.0922]
solutionPercentages = [99.2537, 99.8467, 96.4718, 99.6637, 99.6633, 97.1289, 9.7373, 99.5126, 97.3251, 96.0545, 99.6756, 75.6587, 61.1496, 96.7575, 97.1969, 96.5258, 99.7409, 99.8641, 99.8821, 98.5401, 99.7833, 99.6314, 99.7899, 99.9117, 99.5754, 99.5868, 99.7919, 99.9127, 0.0001, 99.7297, 40.8438, 99.8559, 99.6591, 99.8917, 99.3622, 0.0001, 0.0001, 99.4828, 0.0001, 99.8559, 0.0001, 0.0001, 99.6714, 9.9635, 99.8744, 93.8854, 67.3692, 96.3229, 98.4899, 66.9173, 98.2533, 99.8318, 73.9904, 99.8431, 6.2614, 97.2776, 96.0938, 71.9457, 99.9211, 96.1596, 99.8405, 99.6314, 95.4566, 98.4786, 99.8217, 96.1014, 99.0391, 94.6034, 99.8403, 99.9093, 9.8096, 97.8549, 98.7041, 19.9098, 86.3154, 21.5302, 99.2769, 99.0496, 99.7266, 99.8602, 86.7925, 96.3197, 99.9226, 9.4447, 97.9722, 50.4884, 92.2358, 87.4311, 74.2156, 97.8819, 93.2483, 96.3186, 77.9828, 80.2446, 47.1835, 40.8011, 90.5123, 85.7852, 9.8074, 95.9032, 98.5906, 12.5081, 97.0264, 9.9166, 73.6486, 97.8634, 8.4403, 97.7592, 97.9933, 95.8486, 49.7977, 95.1031, 76.1712, 96.1552, 89.0059, 79.6172, 96.7383, 90.8518, 95.8096, 98.2061, 96.3314, 97.5753, 97.9857, 9.0739, 66.9977, 86.5744, 76.8124, 8.6195, 81.3285, 91.0891, 87.3345, 65.3729, 86.7354, 89.9558, 3.1401, 83.4993, 75.1529, 83.5419, 78.3002, 89.8564, 82.2419, 19.3794, 88.2163, 87.9032, 97.8686, 95.0742, 12.3542, 84.7324, 99.4753, 76.1753, 99.5386, 99.8664, 85.7785, 9.9933, 99.7167, 99.9328, 74.4693, 99.7531, 99.0579, 99.5994, 99.7785, 19.2743, 54.7251, 91.7269, 99.5033, 98.9247, 97.6214, 0.0001, 97.7027, 98.6832, 98.4691, 98.9759, 99.7087, 99.9244, 99.4908, 82.1103, 67.6125, 78.2363, 93.5725, 91.5612, 99.8865, 68.5426, 79.0635, 76.8951, 99.3555, 98.9196, 6.1157, 75.8655, 83.8525, 86.1269, 83.3388, 96.1854, 87.1961, 81.7453, 9.2689, 95.2765, 9.0809, 99.8599]
avgSuccess = sum(avgSolutionPercentages)/len(trajectoryIds)
y = solutionPercentages
#Plot
fig, ax = plt.subplots()
ax.hist(y)
ax.set_ylabel('Number of Motions (Total: '+ str(len(trajectoryIds)) + ')')
ax.set_xlabel('Planning Solution (%)')
ax.set_title('Planning Success Rate (Avg: ' + str(round(avgSuccess,2)) + '%)')
plt.legend(loc='upper left')
plt.show()
So I found out how to make the values on the x axis more precise: I changed ax.hist(y) to ax.hist(y, bins = 1000). But that didn't really work out well either:
So now I need to:
get rid of the empty space between my bars (is there a way to get rid of these empty x values?)
while keeping all bars at the same width
change the precision anytime, e.g., from 1 to 0,01 step for each bar
Just any suggestions on how to make the plot (and code) look better are much appreciated :) Maybe it's not the .hist function that's best for this...but I don't know any better - failed doing this with a bar chart so far :(
How about something like
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import numpy as np
trajectoryIds = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0, 60.0, 61.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0, 70.0, 71.0, 72.0, 73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0, 80.0, 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0, 89.0, 90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0, 100.0, 101.0, 102.0, 103.0, 104.0, 105.0, 106.0, 107.0, 108.0, 109.0, 110.0, 111.0, 112.0, 113.0, 114.0, 115.0, 116.0, 117.0, 118.0, 119.0, 120.0, 121.0, 122.0, 123.0, 124.0, 125.0, 126.0, 127.0, 128.0, 129.0, 130.0, 131.0, 132.0, 133.0, 134.0, 135.0, 136.0, 137.0, 138.0, 139.0, 140.0, 141.0, 142.0, 143.0, 144.0, 145.0, 146.0, 147.0, 148.0, 149.0, 150.0, 151.0, 152.0, 153.0, 154.0, 155.0, 156.0, 157.0, 158.0, 159.0, 160.0, 161.0, 162.0, 163.0, 164.0, 165.0, 166.0, 167.0, 168.0, 169.0, 170.0, 171.0, 172.0, 173.0, 174.0, 175.0, 176.0, 177.0, 178.0, 179.0, 180.0, 181.0, 182.0, 183.0, 184.0, 185.0, 186.0, 187.0, 188.0, 189.0, 190.0, 191.0, 192.0, 193.0, 194.0, 195.0, 196.0, 197.0, 198.0]
avgSolutionPercentages = [20.6256, 99.1448, 15.6764, 21.8231, 16.3733, 17.7502, 20.0055, 86.6873, 11.3105, 15.6693, 10.3449, 81.8921, 11.6745, 92.6031, 11.8787, 23.0229, 37.9636, 2.3903, 15.1727, 14.7088, 10.0426, 59.6758, 8.0042, 12.4174, 10.0585, 46.0567, 90.2376, 98.3273, 52.8645, 49.3027, 62.4136, 32.6199, 19.0642, 10.3319, 74.6157, 22.5771, 22.4118, 11.2017, 16.5053, 11.2021, 30.8376, 24.5255, 83.1072, 10.1529, 14.3991, 46.3459, 16.2137, 4.5773, 44.9549, 1.0719, 76.5605, 42.6589, 13.6209, 34.2856, 1.3574, 29.0465, 66.8146, 16.4796, 32.9564, 62.0732, 3.7047, 13.8828, 31.6088, 60.1141, 3.3247, 45.0796, 13.7862, 26.4498, 93.6806, 10.3245, 62.5157, 10.9833, 42.5908, 37.3208, 27.4115, 84.1648, 13.9058, 13.9065, 67.8918, 27.9075, 3.6116, 10.9091, 41.0988, 24.2177, 50.2762, 61.3869, 15.5915, 27.6536, 0.7993, 22.9483, 22.3393, 88.1832, 25.1604, 18.3625, 15.7212, 56.9646, 4.0434, 11.8431, 56.0613, 32.5472, 97.8757, 21.8233, 14.8162, 38.8259, 20.5676, 72.7201, 17.7987, 35.8117, 15.1699, 17.0359, 14.0621, 35.9655, 11.9095, 10.5691, 23.3259, 16.1746, 10.1936, 12.5084, 24.1494, 16.4727, 21.0687, 15.7495, 28.8929, 11.0135, 13.3133, 14.6639, 50.1304, 21.0346, 5.1604, 53.5107, 20.0712, 41.5111, 12.1633, 74.3263, 17.7904, 17.1684, 25.3977, 21.5871, 21.9332, 22.6674, 36.6634, 99.1179, 15.3213, 16.3999, 12.0147, 57.5163, 4.2062, 17.3874, 10.7132, 17.4919, 17.8457, 29.3538, 26.1468, 75.1234, 16.4368, 21.6191, 61.1394, 12.9972, 73.5746, 72.5788, 41.6835, 39.9912, 20.1648, 11.7097, 11.5203, 36.7387, 5.0694, 30.8129, 12.0922, 22.5419, 12.3569, 54.6776, 28.3561, 26.1219, 44.7455, 1.3281, 46.5064, 13.6016, 23.5483, 11.7151, 44.3669, 3.2577, 75.0943, 10.8634, 14.8226, 45.7661, 19.7319, 30.7981, 3.5965, 47.8161, 14.5996, 39.4484, 13.0693, 24.9947, 97.4253, 76.7901, 73.1183, 4.0922]
solutionPercentages = [99.2537, 99.8467, 96.4718, 99.6637, 99.6633, 97.1289, 9.7373, 99.5126, 97.3251, 96.0545, 99.6756, 75.6587, 61.1496, 96.7575, 97.1969, 96.5258, 99.7409, 99.8641, 99.8821, 98.5401, 99.7833, 99.6314, 99.7899, 99.9117, 99.5754, 99.5868, 99.7919, 99.9127, 0.0001, 99.7297, 40.8438, 99.8559, 99.6591, 99.8917, 99.3622, 0.0001, 0.0001, 99.4828, 0.0001, 99.8559, 0.0001, 0.0001, 99.6714, 9.9635, 99.8744, 93.8854, 67.3692, 96.3229, 98.4899, 66.9173, 98.2533, 99.8318, 73.9904, 99.8431, 6.2614, 97.2776, 96.0938, 71.9457, 99.9211, 96.1596, 99.8405, 99.6314, 95.4566, 98.4786, 99.8217, 96.1014, 99.0391, 94.6034, 99.8403, 99.9093, 9.8096, 97.8549, 98.7041, 19.9098, 86.3154, 21.5302, 99.2769, 99.0496, 99.7266, 99.8602, 86.7925, 96.3197, 99.9226, 9.4447, 97.9722, 50.4884, 92.2358, 87.4311, 74.2156, 97.8819, 93.2483, 96.3186, 77.9828, 80.2446, 47.1835, 40.8011, 90.5123, 85.7852, 9.8074, 95.9032, 98.5906, 12.5081, 97.0264, 9.9166, 73.6486, 97.8634, 8.4403, 97.7592, 97.9933, 95.8486, 49.7977, 95.1031, 76.1712, 96.1552, 89.0059, 79.6172, 96.7383, 90.8518, 95.8096, 98.2061, 96.3314, 97.5753, 97.9857, 9.0739, 66.9977, 86.5744, 76.8124, 8.6195, 81.3285, 91.0891, 87.3345, 65.3729, 86.7354, 89.9558, 3.1401, 83.4993, 75.1529, 83.5419, 78.3002, 89.8564, 82.2419, 19.3794, 88.2163, 87.9032, 97.8686, 95.0742, 12.3542, 84.7324, 99.4753, 76.1753, 99.5386, 99.8664, 85.7785, 9.9933, 99.7167, 99.9328, 74.4693, 99.7531, 99.0579, 99.5994, 99.7785, 19.2743, 54.7251, 91.7269, 99.5033, 98.9247, 97.6214, 0.0001, 97.7027, 98.6832, 98.4691, 98.9759, 99.7087, 99.9244, 99.4908, 82.1103, 67.6125, 78.2363, 93.5725, 91.5612, 99.8865, 68.5426, 79.0635, 76.8951, 99.3555, 98.9196, 6.1157, 75.8655, 83.8525, 86.1269, 83.3388, 96.1854, 87.1961, 81.7453, 9.2689, 95.2765, 9.0809, 99.8599]
avgSuccess = sum(avgSolutionPercentages)/len(trajectoryIds)
y = solutionPercentages
BIN_COUNT = 15
BAR_WIDTH = 0.75
fig, ax = plt.subplots()
# use numpy histogram so we can perform filtering
hist, bin_edges = np.histogram(y, bins=BIN_COUNT)
# so we can remove bins with zero entries
non_zero = np.nonzero(hist)
# take only entries where bin is non-zero
hist = hist[non_zero]
bin_edges = bin_edges[non_zero]
# generate labels based on bin edge values (maybe use centers?)
x_ticks = [str(int(edge)) for edge in bin_edges]
indices = np.arange(len(bin_edges))
plt.bar(indices, hist, BAR_WIDTH, align='center')
plt.xticks(indices, x_ticks)
ax.set_ylabel('Number of Motions (Total: '+ str(len(trajectoryIds)) + ')')
ax.set_xlabel('Planning Solution (%)')
ax.set_title('Planning Success Rate (Avg: ' + str(round(avgSuccess,2)) + '%)')
plt.show()
which produces the plot
You may use some nonlinear dependence of the bin width, e.g.
b = 5
bins = (np.linspace(np.min(y)**b, np.max(y)**b))**(1/b)
fig, ax = plt.subplots()
ax.hist(y, bins=bins, edgecolor="k")
Or you may define the bins completely customized, e.g. use a bin width of 10 up to 60 and then use a bin width of 5 till 90, finally use a bin with of 1 till 100.
bins = np.concatenate((np.linspace(0,60,7),
np.linspace(60,90,7),
np.linspace(90,100,11)))
fig, ax = plt.subplots()
ax.hist(y, bins=bins, edgecolor="k")

Python: Linregress slope and y-intercept

I'm working on a program that can calculate the slope using the linregress native scipyy function, but I'm getting two errors (depending on how I try to fix it). The two lists should be two-dimensional, basically x and y values.
from __future__ import division
from scipy.stats import linregress
import matplotlib.pyplot as mplot
import numpy as np
xs=[[20.0, 80.0, 45.0, 42.0, 93.0, 98.0, 65.0, 43.0, 72.0, 36.0, 9.0, 60.0, 47.0, 84.0, 31.0, 46.0, 57.0, 76.0, 27.0, 85.0, 0.0, 39.0, 2.0, 56.0, 68.0, 6.0, 41.0, 28.0, 61.0, 12.0, 32.0, 1.0, 54.0, 77.0, 18.0, 86.0, 62.0, 23.0, 30.0, 69.0, 4.0, 71.0, 64.0, 92.0, 24.0, 79.0, 8.0, 35.0, 49.0, 53.0, 7.0, 59.0, 70.0, 37.0, 13.0, 15.0, 73.0, 89.0, 96.0, 83.0, 22.0, 95.0, 19.0, 67.0, 5.0, 88.0, 38.0, 50.0, 55.0, 52.0, 81.0, 58.0, 11.0, 51.0, 99.0, 78.0, 25.0, 33.0, 40.0, 75.0, 3.0, 91.0, 48.0, 90.0, 82.0, 26.0, 10.0, 16.0, 21.0, 66.0, 14.0, 87.0, 74.0, 97.0, 94.0, 44.0, 29.0, 17.0, 63.0, 34.0], [87.0, 17.0, 69.0, 72.0, 76.0, 62.0, 20.0, 77.0, 5.0, 49.0, 81.0, 3.0, 24.0, 36.0, 44.0, 91.0, 99.0, 35.0, 43.0, 50.0, 12.0, 54.0, 46.0, 30.0, 37.0, 45.0, 90.0, 85.0, 70.0, 83.0, 38.0, 22.0, 23.0, 0.0, 60.0, 47.0, 26.0, 1.0, 95.0, 73.0, 65.0, 94.0, 84.0, 8.0, 34.0, 56.0, 66.0, 13.0, 75.0, 52.0, 19.0, 55.0, 67.0, 39.0, 21.0, 80.0, 98.0, 33.0, 11.0, 68.0, 40.0, 32.0, 2.0, 79.0, 82.0, 93.0, 96.0, 88.0, 14.0, 92.0, 41.0, 89.0, 28.0, 29.0, 42.0, 6.0, 86.0, 74.0, 58.0, 16.0, 31.0, 64.0, 15.0, 53.0, 25.0, 59.0, 61.0, 78.0, 51.0, 7.0, 57.0, 9.0, 97.0, 63.0, 48.0, 71.0, 18.0, 10.0, 4.0, 27.0]]
ys=[[155.506, 50.592, 104.447, 111.318, 36.148, 36.87, 74.266, 106.413, 58.341, 122.563, 180.555, 85.202, 96.84, 50.726, 126.56, 100.686, 88.303, 54.797, 138.487, 44.946, 200.9, 116.524, 193.652, 82.8, 65.823, 184.436, 113.738, 133.458, 83.765, 167.408, 129.491, 200.469, 89.238, 51.799, 159.217, 49.382, 78.443, 146.051, 129.045, 63.805, 185.564, 65.614, 74.243, 43.408, 140.863, 53.446, 182.767, 127.373, 94.494, 91.079, 187.194, 81.254, 68.702, 121.368, 164.756, 169.696, 59.483, 45.978, 33.057, 47.12, 154.755, 33.872, 160.754, 70.256, 190.393, 38.398, 113.188, 100.493, 84.511, 88.635, 49.353, 81.821, 178.876, 95.307, 32.2, 54.715, 141.389, 132.337, 109.673, 57.611, 189.251, 39.283, 97.31, 41.173, 47.529, 140.03, 173.058, 160.288, 154.773, 67.903, 164.718, 42.032, 60.739, 28.656, 34.302, 107.022, 137.344, 160.195, 73.636, 123.797], [14.138, 100.87, 30.287, 28.675, 21.826, 42.445, 97.938, 29.574, 125.976, 59.404, 26.609, 125.743, 95.329, 75.467, 59.497, 15.342, 9.834, 77.402, 65.019, 54.468, 112.64, 45.466, 55.197, 79.992, 71.146, 55.39, 14.795, 15.971, 28.535, 25.862, 73.239, 92.455, 87.635, 137.6, 38.59, 53.718, 86.26, 130.567, 11.274, 33.867, 40.035, 11.07, 16.109, 114.732, 76.552, 45.85, 31.827, 110.877, 26.292, 55.738, 101.801, 48.601, 33.632, 66.647, 98.39, 23.904, 11.172, 78.215, 109.417, 31.653, 68.368, 79.593, 124.548, 21.513, 19.828, 13.48, 9.993, 22.043, 108.229, 16.904, 66.704, 12.262, 79.947, 85.012, 66.754, 124.114, 17.548, 25.872, 45.392, 101.775, 78.085, 36.358, 101.795, 52.045, 87.637, 42.784, 37.011, 26.036, 50.146, 119.666, 42.514, 113.313, 9.125, 42.394, 51.954, 26.898, 96.678, 112.108, 125.252, 86.296]]
slope, intercept, r_value, std_err = linregress(xs,ys)
print(slope)
My error is:
in linregress
ssxm, ssxym, ssyxm, ssym = np.cov(x, y, bias=1).flat
ValueError: too many values to unpack (expected 4)
I've tried changing my code to something like this:
slope, intercept, r_value, std_err = linregress(xs[:,0], ys[:,0])
But then my error becomes a TypeError:
TypeError: list indices must be integers or slices, not tuple
Does anyone have any suggestions? Perhaps there's something I don't understand about the use of the linregress function. I'm sure my first error has to do with my lists being 2D. For the second error, I'm lost.
You have two problems:
When interpreted as arrays, your variables xs and ys are two-dimensional with shape (2, 100). When linregress is given both arguments x and y, it expects them to be one-dimensional arrays.
As you can see in the "Returns" section of the docstring, linregress returns five values, not four.
You'll have to call linregress twice, and handle the five return values. For example,
In [144]: slope, intercept, rvalue, pvalue, stderr = linregress(xs[0], ys[0])
In [145]: slope, intercept, rvalue
Out[145]: (-1.7059670627062702, 187.5658196039604, -0.9912859597363385)
In [146]: slope, intercept, rvalue, pvalue, stderr = linregress(xs[1], ys[1])
In [147]: slope, intercept, rvalue
Out[147]: (-1.2455432103210327, 121.51968891089112, -0.9871123119133126)

How to generate interpolators for moving object coordinates with missing data?

I have (x,y) pixel coordinates for six points, in a subset of frames from a video clip. I would like to generate a smooth interpolator for (x, y) pixel values for frames (time) in-between these data points.
They are shown below both as trajectories in x, y, and individually versus time.
In scipy.interpolate there is a very generous selection of possible choices (many different kinds of interpolation methods). My question is: where should I start?? I'm looking for a recommendation from someone with a bit of experience with interpolation. Of all the available interpolation methods in SciPy, which ones should I try to learn first in order to interpolate this and similar data?
Some of the available methods can fit x and y together - e.g. MultivariateSplines but not sure if there is an advantage to treating them together, or independently.
note: I am working with NumPy and SciPy only, please don't recommend other packages. Thanks!
If you would like to see the data, it's all here in the paths = np.array... line:
import numpy as np
import matplotlib.pyplot as plt
# this line is very long - has all the data for the plot!
paths = np.array([[[45.0, 135.0], [42.0, 130.0], [41.0, 123.0], [41.0, 120.0], [38.0, 119.0], [35.0, 124.0], [34.0, 124.0], [36.0, 122.0], [36.0, 118.0], [37.0, 113.0], [41.0, 107.0], [45.0, 97.0], [49.0, 82.0], [54.0, 72.0], [56.0, 66.0], [57.0, 61.0], [58.0, 59.0], [57.0, 55.0], [-1, -1], [53.0, 47.0], [47.0, 41.0], [42.0, 35.0], [37.0, 27.0], [34.0, 23.0], [33.0, 21.0], [35.0, 23.0], [38.0, 23.0], [43.0, 21.0], [46.0, 17.0], [50.0, 16.0], [50.0, 19.0], [50.0, 26.0], [47.0, 35.0], [46.0, 41.0], [47.0, 44.0], [48.0, 48.0], [47.0, 51.0], [43.0, 55.0], [39.0, 56.0], [36.0, 52.0]], [[50.0, 139.0], [48.0, 135.0], [46.0, 128.0], [46.0, 125.0], [43.0, 124.0], [40.0, 128.0], [40.0, 130.0], [41.0, 127.0], [42.0, 124.0], [43.0, 120.0], [45.0, 112.0], [51.0, 102.0], [54.0, 91.0], [60.0, 79.0], [63.0, 70.0], [62.0, 67.0], [68.0, 62.0], [62.0, 62.0], [-1, -1], [63.0, 52.0], [53.0, 48.0], [49.0, 41.0], [44.0, 33.0], [40.0, 30.0], [40.0, 27.0], [41.0, 29.0], [44.0, 30.0], [48.0, 27.0], [52.0, 24.0], [56.0, 22.0], [58.0, 27.0], [56.0, 32.0], [54.0, 42.0], [52.0, 48.0], [53.0, 52.0], [55.0, 55.0], [54.0, 59.0], [50.0, 62.0], [45.0, 63.0], [42.0, 58.0]], [[113.0, 134.0], [110.0, 130.0], [110.0, 123.0], [110.0, 121.0], [107.0, 120.0], [105.0, 123.0], [105.0, 123.0], [107.0, 121.0], [108.0, 119.0], [110.0, 115.0], [113.0, 108.0], [118.0, 96.0], [-1, -1], [128.0, 72.0], [122.0, 68.0], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [127.0, 40.0], [123.0, 35.0], [118.0, 29.0], [116.0, 24.0], [115.0, 21.0], [117.0, 22.0], [122.0, 23.0], [128.0, 19.0], [-1, -1], [136.0, 15.0], [-1, -1], [138.0, 26.0], [137.0, 35.0], [136.0, 41.0], [138.0, 43.0], [140.0, 48.0], [140.0, 50.0], [137.0, 54.0], [134.0, 55.0], [133.0, 51.0]], [[192.0, 154.0], [190.0, 148.0], [191.0, 140.0], [192.0, 137.0], [191.0, 137.0], [188.0, 142.0], [189.0, 143.0], [193.0, 139.0], [193.0, 137.0], [197.0, 134.0], [203.0, 128.0], [208.0, 117.0], [217.0, 104.0], [219.0, 93.0], [223.0, 85.0], [226.0, 81.0], [228.0, 78.0], [228.0, 76.0], [228.0, 73.0], [226.0, 68.0], [224.0, 63.0], [220.0, 58.0], [217.0, 52.0], [216.0, 46.0], [216.0, 42.0], [219.0, 42.0], [223.0, 45.0], [230.0, 46.0], [236.0, 48.0], [244.0, 50.0], [247.0, 50.0], [248.0, 50.0], [246.0, 55.0], [245.0, 61.0], [249.0, 71.0], [254.0, 79.0], [257.0, 87.0], [256.0, 89.0], [253.0, 87.0], [251.0, 80.0]], [[216.0, 144.0], [214.0, 139.0], [215.0, 131.0], [216.0, 129.0], [217.0, 127.0], [213.0, 133.0], [214.0, 133.0], [217.0, 130.0], [219.0, 128.0], [222.0, 125.0], [227.0, 118.0], [234.0, 107.0], [239.0, 94.0], [245.0, 82.0], [249.0, 76.0], [253.0, 72.0], [255.0, 69.0], [255.0, 67.0], [255.0, 62.0], [254.0, 57.0], [251.0, 52.0], [248.0, 48.0], [245.0, 42.0], [244.0, 36.0], [245.0, 31.0], [249.0, 32.0], [252.0, 33.0], [259.0, 34.0], [267.0, 38.0], [273.0, 38.0], [277.0, 38.0], [278.0, 39.0], [276.0, 43.0], [276.0, 50.0], [280.0, 58.0], [286.0, 67.0], [288.0, 74.0], [288.0, 76.0], [286.0, 74.0], [286.0, 67.0]], [[252.0, 149.0], [250.0, 143.0], [252.0, 137.0], [253.0, 133.0], [252.0, 132.0], [250.0, 137.0], [252.0, 137.0], [255.0, 134.0], [257.0, 132.0], [262.0, 130.0], [266.0, 123.0], [273.0, 111.0], [277.0, 97.0], [286.0, 86.0], [289.0, 80.0], [293.0, 77.0], [295.0, 75.0], [296.0, 71.0], [296.0, 66.0], [296.0, 61.0], [294.0, 57.0], [290.0, 52.0], [288.0, 48.0], [289.0, 42.0], [289.0, 37.0], [293.0, 35.0], [298.0, 37.0], [305.0, 39.0], [313.0, 42.0], [319.0, 43.0], [324.0, 44.0], [325.0, 44.0], [324.0, 49.0], [325.0, 55.0], [329.0, 63.0], [336.0, 71.0], [339.0, 78.0], [339.0, 80.0], [337.0, 79.0], [338.0, 73.0]]])
paths[paths<0] = np.nan # turn the -1's back into np.nan
plt.figure()
for path in paths:
x, y = path.T
plt.plot(x, y, linewidth=3)
plt.plot(x, y, '.k')
plt.show()
plt.figure()
for i, path in enumerate(paths):
plt.subplot(6, 1, i+1)
x, y = path.T
plt.plot(x)
plt.plot(x, '.k')
plt.plot(y)
plt.plot(y, '.k')
plt.show()
I'm using scipy.interpolate.InterpolatedUnivariateSpline.
One thing I had to deal with is that some of the methods tend to take an inordinate amount of memory space - the function above works well in that regard.

Categories

Resources