Cartopy incorrectly filling outside of Shapely polygon - python

The Shapely polygon listed below as "mywkt" plots incorrectly in Cartopy; it fills the outside rather than the inside, despite being a valid polygon. In GIS software it plots correctly. The issue is occurring in many projections tested, including the one below. How can I correct it to fill the inside, as would be expected?
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.feature import ShapelyFeature
import shapely
import shapely.wkt as wkt
# Main plot setup, issue occurs regardless of projection used.
fig = plt.figure(figsize=[5, 5])
crs = ccrs.NearsidePerspective(central_longitude=-100, central_latitude=65, satellite_height=6200000)
ax = fig.add_subplot(1, 1, 1, projection=crs)
# Build and plot the polygon, valid
mywkt = "POLYGON ((-66.64127406774401 63.2679483443407, -66.48652697503979 63.1988718046901, -66.4817891045834 63.1004449790526, -66.379143168922 63.0115892305586, -66.1973803825985 62.9848443902565, -66.0559008189676 62.9537393506905, -65.97118398525259 62.9850694644843, -65.850509788396 62.9360773051236, -65.69642646101229 62.9192773820456, -65.6182098640938 62.8587266887857, -65.473922680887 62.8270608886491, -65.3890303678572 62.8197557451316, -65.31233201541011 62.7191354685271, -65.25006848777041 62.6268846390149, -65.12107061571631 62.5688402034127, -64.96662902810711 62.6103134144326, -64.52292746513029 60.3009837190234, -64.678588195289 60.3290634023542, -64.7726142595404 60.3596734442741, -64.8751520004644 60.2840499669325, -64.9515074450125 60.2243558221635, -65.0198404413832 60.1228099129471, -65.095375384966 60.0444670754952, -65.1483835964257 59.944148956709, -65.2920146315699 59.8405379513654, -65.453168021487 59.7919610620534, -65.47048148623151 59.6270483926692, -65.4267596751189 59.4696754279787, -65.49367684990671 59.3679086749455, -65.6342827626394 59.2531493848363, -65.6786190418353 59.1322782877129, -65.7005535515051 59.0241583127559, -65.8285595801605 58.9623716488326, -65.9420170341587 58.8275171137239, -66.0506960726655 58.7706855047323, -66.149750409254 58.7371845390353, -66.256134042452 58.7978006318937, -66.3347385322574 58.8311493351738, -66.45372349384409 58.800538690708, -66.49451082791229 58.7198367831411, -66.5676193169778 58.6466901412371, -66.6218795870732 58.5770604645689, -66.7814218532359 58.4949434629705, -66.88831340553919 58.4688908893032, -67.0380290656889 58.4144607966286, -67.1342663928796 58.3776258669403, -67.2353212911446 58.3398129542046, -67.33057432114791 58.3205410987927, -67.47232732814371 58.2854808444655, -67.5925217519772 58.1990972702229, -67.6903910465095 58.2706693512096, -67.7234163041191 58.3415155704872, -67.81171229568589 58.4379109661182, -67.9250695391279 58.4466397966894, -68.0271575596735 58.5408090366078, -68.1713938511453 58.5239214763951, -68.3308705567307 58.6276111553306, -68.37026229227089 58.7457949055105, -68.4149256971631 58.8196035851374, -68.5609516754794 58.8743733607421, -68.67261845427809 58.9022758569453, -68.8273157758023 58.9054559910013, -68.9202407883817 58.878506525256, -69.07570399414401 58.9052582049023, -69.2647591176391 58.8920751275651, -69.3652429760824 58.869404278185, -69.4899762315179 58.7899739103677, -69.6132537210823 58.7566270839728, -69.72686138794261 58.6796452157541, -69.842988449757 58.6732594077955, -69.8395047468465 58.8392757000136, -69.6917509226669 58.8574368306453, -69.5327113793267 58.8471842391614, -69.46260960175159 58.9405896192156, -69.4404513848235 59.0222669548561, -69.3474281644782 59.1017357623955, -69.3028958231836 59.2196576443848, -69.4281941172646 59.3568617410766, -69.5182813676909 59.3316304588787, -69.6520868841024 59.4400386197865, -69.65458094065789 59.5726276160716, -69.5578593654174 59.6813767639266, -69.5928206493534 59.798270974426, -69.70748724543991 59.9355825212164, -69.737201347664 60.0551376260007, -69.616491617694 60.15187842425, -69.6398101732009 60.2406957887797, -69.7022901640597 60.3436465118771, -69.7505086681404 60.4372696447783, -69.7298120033212 60.5385315013278, -69.6576941750461 60.635072446836, -69.58624424267749 60.736179235983, -69.5121017580896 60.7615279039897, -69.4052339964517 60.8147459704502, -69.4564300739886 60.9777373363091, -69.5537354306456 61.0800360797014, -69.67305901935821 60.9755718380699, -69.74723982315921 60.8975333096147, -69.84465196801381 60.847667578496, -69.99184594083739 60.836894867704, -70.1006539538168 60.9605688827478, -70.1916436091651 61.0764643475517, -70.3686452388866 61.0577015965515, -70.5324451896937 61.0420612242462, -70.66397847542621 61.043868155591, -70.8089575088819 61.0657154903776, -70.9218584021435 61.0859015570113, -71.1063331804318 61.1279448078067, -71.2168983247977 61.13527297217, -71.39589387470249 61.1518817695281, -71.5711726138249 61.2143165838433, -71.67622634576909 61.2783583103878, -71.7435230974129 61.4217185605632, -71.7156399546202 61.5457269912077, -71.745408594431 61.6792414618241, -71.84647644023541 61.7009009129468, -72.0093717180302 61.7027090727656, -72.1138114630285 61.768727955231, -72.22976980123519 61.8808668843993, -72.3646309526563 61.8955630162203, -72.4438507145388 61.911258570027, -72.5975236614877 62.0116845813784, -72.65332864939759 62.1237777818192, -72.751459880469 62.1445197697716, -72.903374167184 62.1482523240856, -73.0173601185745 62.183594747, -73.0963776310228 62.2126581636434, -73.1944329188441 62.2876760141368, -73.28965616150251 62.3318380343978, -73.42240349692371 62.3685538308947, -73.56754929616309 62.4005830290836, -73.73324404207889 62.4774707715589, -73.8655810825009 62.4501908903973, -73.9502095218519 62.3978956398719, -74.1022008669764 62.3374917528422, -74.2265877898945 62.3260057582592, -74.29980367420011 62.2777694125284, -74.42241445316439 62.2427918022999, -74.511257930409 62.2428908122612, -74.62888748866651 62.2598035705784, -74.7267889348615 62.2046664902664, -74.8163182386957 62.2027195416736, -74.9442040738588 62.2578310929307, -75.03847038480041 62.2674159350974, -75.1831341600134 62.2847549100112, -75.3442277576048 62.3174456539938, -75.5153162981086 62.2713303206378, -75.6416712506276 62.2500114445726, -75.8110834269585 62.3090148398631, -75.93677284069391 62.3500193062461, -76.10534289276021 62.3775115497183, -76.2125175210667 62.3952569876199, -76.3685719204577 62.4278825055732, -76.48766814167131 62.4468325304969, -76.6064638391348 62.4572315361066, -76.7061760154107 62.4969147689015, -76.8642309126265 62.5259033871317, -76.9682144998992 62.5369445167761, -77.0828220366076 62.5335578426204, -77.25888406544721 62.5470384120931, -77.3441128508273 62.5540247837169, -77.47945886273619 62.5781576537528, -77.5586235987 62.541063735503, -77.6682454270847 62.5150911061697, -77.81984122339369 62.4436013059556, -77.9415896901121 62.405578487857, -78.0408993361354 62.3642756102313, -78.1703715205291 62.2442638953625, -78.1610545162529 62.1223761632295, -78.14890860634701 62.0181106241061, -78.07967792379711 61.8977229845282, -78.05323718115091 61.8092322177927, -77.99494302809821 61.7128768347981, -77.9110333772885 61.6805678431315, -77.7752377331347 61.6529479256063, -77.68604436295691 61.5902604530575, -77.7464299002197 61.4447707602625, -77.7713878361796 61.2809443639912, -81.1208442837367 61.6343186366958, -85.9087502533215 61.4403863269675, -87.7835499471722 60.796390048462, -88.8519416879585 60.1195386784673, -89.8256929935368 59.1331428536598, -90.28917688060849 58.6636404579409, -90.67532027770859 57.2245717427917, -90.79091050271749 57.2407170808287, -90.9217394124439 57.2601567677207, -91.01806055105349 57.2568265287214, -91.1159991221617 57.249242901139, -91.2779177991671 57.2192479316468, -91.4525318115937 57.1780529852571, -91.5799427372674 57.1525440913368, -91.7268544003103 57.1197453081995, -91.8391362308523 57.0876751359988, -91.9249059071818 57.0659466097483, -92.0507985856861 57.049360294542, -92.13512631242941 57.04835702076, -92.25875306989821 57.0716991447998, -92.3518469706611 57.0584964685373, -92.4861090971571 57.0399379517752, -92.4857200468426 57.1999797751466, -92.4425224316648 57.3074512552713, -92.4497627547472 57.3881817001254, -92.5112404799642 57.5276470395793, -92.59639221412939 57.6298542247927, -92.65817509550421 57.7008037788714, -92.7342173331634 57.8130054717252, -92.77113601131779 57.8940105722302, -92.8201015390547 58.0647373586422, -92.874468654005 58.1538677627871, -92.97732059736759 58.2803554957096, -93.0559415825379 58.4189625249007, -93.1315794841059 58.5865955877163, -93.1591292778938 58.7012920988514, -93.2823669526727 58.7624760209517, -93.3608810776995 58.7592754812785, -93.4738800798018 58.7538242685752, -93.62835249617071 58.7846698986969, -93.71609056722581 58.7966098944312, -93.89549583566421 58.7730426754791, -94.0091280782981 58.7725887156967, -94.14890625412031 58.7425517739453, -94.24527317181941 58.7465228653381, -94.4130130857852 58.709989490023, -94.496623682337 58.803209247309, -94.605174776095 58.8845767350284, -94.668979477321 58.9607562380101, -94.7854498526285 59.019945075305, -94.8760642082965 59.0353373705233, -95.0288505271301 59.0589655922883, -94.9498328680923 59.0871371413661, -94.78415853948491 59.1531909238221, -94.75664135421501 59.3040141602254, -94.7311897255254 59.4165820732042, -94.8055229949902 59.5517024667955, -94.8188439536855 59.6357478828585, -94.7946054061145 59.7556094972806, -94.82640478525261 59.8905277160813, -94.7793129086637 60.072941390333, -94.73645838124919 60.2213475638391, -94.6762507910411 60.3269215005269, -94.6734126925416 60.423391387003, -94.6149944348452 60.513830240873, -94.5353359405012 60.6169071910141, -94.4341091386257 60.7222386291095, -94.37695735929471 60.8089810418417, -94.2591747562099 60.8856145118004, -94.2088665424568 60.964075215299, -94.117840250527 61.0820447451689, -94.04385803502031 61.2004110028656, -93.9886975318655 61.2938901704951, -93.9522824524732 61.3760549440805, -93.9426923501717 61.491502950301, -93.804676572166 61.5421430732443, -93.69641544681529 61.598573904762, -93.5824475313284 61.6549169984208, -93.4946942149659 61.6810438713776, -93.4563641740674 61.8020346349905, -93.4010892366789 61.9084074808207, -93.32190375965079 61.9974730456446, -93.1770367754268 62.0318396699648, -93.113697555198 62.1166098847385, -93.0654263904156 62.1899207446467, -92.9374432540029 62.2031119824321, -92.7967037820538 62.2611297306526, -92.68520018074371 62.3574088907078, -92.6355176019556 62.4841178883299, -92.5609097442893 62.5284408665528, -92.3973812421505 62.5429100292745, -92.29000511624299 62.5757202526876, -92.19575159456041 62.695326287878, -92.0960278026409 62.834230877553, -92.00510827732511 62.8504166473105, -91.8950160639401 62.8354172685921, -91.723606384527 62.8273682724279, -91.6037180224687 62.8120141273502, -91.51237887258679 62.8096223192147, -91.3764153861611 62.8267961003598, -91.195743643854 62.8759637552232, -91.0767021706686 62.9177041853999, -90.945301069089 62.9480195986257, -90.8332252075772 62.9270273293478, -90.7121848562461 63.0189462910386, -90.6908760112189 63.1564279904595, -90.75826653966899 63.2864176159803, -90.83001343447241 63.3706194286438, -90.8963892209857 63.4225221952432, -91.04234724652819 63.4645029278679, -91.0056193874247 63.5928255403664, -90.84331157107211 63.5712992167855, -90.734394298558 63.5932146644253, -90.57693117953031 63.6349169525554, -90.4958383024956 63.6178995949904, -90.3111839970437 63.6364504873002, -90.1911506903521 63.6513583348194, -90.0785332049434 63.7674586633867, -90.09151085913879 63.8706767468345, -89.987567986873 63.956850772745, -89.8510933464807 64.0146358231847, -89.6954002542183 64.07326773821561, -89.6150550465531 64.034685895238, -89.476774932617 64.0453088761038, -89.3606832879034 64.02033502574091, -89.2708623726004 63.986059977586, -89.12059375799549 63.9970920969228, -89.00106399340849 64.0190990964462, -88.819049435583 63.9913967794379, -88.66757537690209 64.0033367967558, -88.5629534490424 64.0129498434605, -88.4285540041639 64.0731839294948, -88.3423570797154 64.0983151648131, -88.1679795916847 64.1600140962736, -88.03858503766931 64.235651929493, -87.8977998100131 64.3457898877932, -87.8045304874919 64.486819262499, -87.67589122909681 64.52863598846019, -87.5598252258534 64.5852384789727, -87.4955704786946 64.6759901377348, -87.403468411438 64.72561935974581, -87.30076911360371 64.7830381954564, -87.2086212942372 64.8866224981298, -87.13297583926349 64.9438811151214, -87.04295660816079 65.01542958800511, -86.9738190351204 65.0813780841314, -87.0019258024338 65.214266882011, -87.1682314416277 65.2549620607506, -87.2687563954912 65.2654753576017, -87.3695407435104 65.2699766928702, -87.5550724563287 65.2932921070451, -87.672191344455 65.29336076218949, -87.8180730562265 65.28059677162381, -87.98862825109281 65.2838468774315, -88.10628882606839 65.2803831223832, -88.21958538595671 65.2826414135705, -88.3569145495667 65.28509043657439, -88.481838319183 65.29619119576709, -88.5829431100365 65.3128995606663, -88.7499735227598 65.3185834441707, -88.821048993616 65.3223828774679, -88.9447825770542 65.3325909965773, -89.03952443690309 65.32480137568299, -89.12854429728149 65.4086865640338, -89.2718930900939 65.47440615841749, -89.3944288907806 65.5483960190611, -89.47274469730991 65.61487855869861, -89.5859955023904 65.65442933124871, -89.6893509905557 65.7181347698038, -89.8548555487445 65.76428496358029, -89.892712677548 65.8680752478745, -89.8472491674151 65.9957913234109, -89.7289553357201 65.9677914549118, -89.58537003200129 65.908060935264, -89.4675644692248 65.8565320132419, -89.33667450902141 65.8462018255539, -89.1824996027743 65.7857999206299, -89.0757032607967 65.7328366707758, -88.9438901303252 65.6937665516851, -88.7899365049656 65.6880445198388, -88.6762889810516 65.6670560636864, -88.57616072353849 65.5965070586539, -88.4880335311225 65.56534098929529, -88.361385490448 65.50024700199759, -88.2690468664197 65.46194744811631, -88.1663932793262 65.3914518484241, -88.07439798857619 65.3724699218495, -87.9584234895524 65.33616164144669, -87.8748434303799 65.3235044831454, -87.7509648942145 65.3238249295769, -87.6514622896529 65.3308592395623, -87.5082279661476 65.3211317739123, -87.4191700041923 65.3224364078141, -87.3157687765078 65.3291045084663, -87.1970172123744 65.36031637310219, -87.0824203887553 65.391353511131, -86.19775771305351 64.85291030109521, -86.2442882170275 64.7571059717632, -86.32390094037601 64.66191400482749, -86.3809077265102 64.4934798466177, -86.35802712735 64.413348313374, -86.3106332449282 64.2786666072782, -86.22036216947841 64.187564008772, -86.2779869616289 64.0708571495379, -86.3801827380429 64.05316457378611, -86.47285701873631 64.0198469978407, -86.6226068279427 63.9899779037573, -86.724581347751 63.9622259702524, -86.8859964979399 63.916262666389, -86.9900538332177 63.8612318328829, -87.10030622305371 63.7731084958605, -87.1830012486087 63.7357053790001, -87.173403404943 63.5933752024037, -87.0755182139943 63.5522069946571, -86.9497552186285 63.5508222707313, -86.8146919967382 63.5675344721382, -86.71865315352009 63.6085844397202, -86.59764329460231 63.6556501865069, -86.5092567233191 63.6640196407939, -86.3792594076954 63.6473265355912, -86.2353155541694 63.6404715360327, -86.1552068852894 63.6621123217263, -85.9680806546985 63.6859389384313, -85.79514510006619 63.7071448542517, -85.6993351253158 63.7117873507057, -85.6090106328344 63.5890723529068, -85.63833798161259 63.4528800204966, -85.645616358608 63.2824965547723, -85.6180055296314 63.1828566588078, -85.509271323113 63.1111022070013, -85.3826309500583 63.1142798589284, -85.2088561543879 63.1131240196579, -85.05288839028699 63.1557685364888, -84.9375547940184 63.1815177526422, -84.80197278111891 63.2255660770129, -84.7200406927237 63.2648765408198, -84.6114133391543 63.2991477911377, -84.46032077738199 63.3875457892069, -84.42369971007351 63.4929153675502, -84.3627942522529 63.5631820996742, -84.2592480835322 63.6143906076405, -84.1496136358606 63.6134407571852, -84.0352337022735 63.6136810911095, -83.9035960978815 63.6852906027792, -83.7422038462304 63.7682183252156, -83.6065913472128 63.8946145224424, -83.584580587624 64.0302842417244, -83.49345508968059 64.1090348641677, -83.3827448918632 64.1013444420924, -83.1991129159284 64.1116136224708, -83.07296084596631 64.1778520384409, -83.0066231907077 64.0764802712476, -82.94601520883479 63.9640840289548, -82.7939841901181 63.9521097051988, -82.63841313855541 63.9574464782724, -82.5287557986411 63.9509996477156, -82.4869618965743 63.7920373903133, -82.3810886897912 63.6545404410886, -82.26029243334899 63.6605257089943, -82.1756061226244 63.6781000274944, -82.01801326829749 63.651164459872, -81.94321464463999 63.6374239242748, -81.80551926450239 63.6250948323607, -81.6371918333364 63.5614771325637, -81.5207443331844 63.5585512674271, -81.435592609811 63.5258631305791, -81.31211079517701 63.5023188263528, -81.1656187099354 63.4575228434045, -81.0769574713101 63.445498922937, -80.9414593750501 63.48022030912, -80.8146130266698 63.550197129494, -80.6725231372329 63.6020388779919, -80.5647197569111 63.6408458032913, -80.4219737436933 63.7144236988946, -80.4587932625989 63.8407779229629, -80.55911225372751 63.9453616638098, -80.6660459140877 63.9920268507587, -80.71445474596069 64.033819580696, -80.3046263074668 64.09006107806491, -79.0904559802441 64.2550682373605, -79.09042986435421 64.2550718099133, -77.9650089573083 64.4100325023033, -77.8272233003151 64.3721120512303, -77.6572174185489 64.3663442384959, -77.559309350646 64.3563802538457, -77.4368040945332 64.3257787581832, -77.2720472210024 64.2663305222359, -77.1466580675157 64.2855947513967, -76.9916744612073 64.2493932816747, -76.87635611024621 64.24878293698301, -76.70648755675521 64.2424047714631, -76.6052073099693 64.2889974937394, -76.4764535822669 64.27468475549711, -76.354238259488 64.2964437966439, -76.2682702053057 64.317554338462, -76.14462056818461 64.3744238565758, -76.0178810062034 64.3816412703284, -75.91278605023589 64.3963583797355, -75.7954459270877 64.4687918628542, -75.6769690338937 64.5719794368016, -75.50846613600331 64.54076759105151, -75.4066975852252 64.5199240885259, -75.26134990617309 64.47008125864321, -75.0765277296709 64.4542960505224, -74.94823938673829 64.4170035736561, -74.7806367614773 64.40159220471649, -74.6101197140535 64.4119147850394, -74.5589798963051 64.5428886124196, -74.4577606865064 64.6090049553145, -74.3456237853093 64.5845451205609, -74.213337660005 64.6295814464267, -74.0764433536777 64.5839271541149, -73.95660072664219 64.4317054692321, -73.8716550343523 64.5373344480264, -73.72642944636181 64.5669365082134, -73.6222652536385 64.4982948456307, -73.51404985072 64.42818834031971, -73.4140741991649 64.3656654517992, -73.2567178440532 64.2992439464511, -73.17885220036879 64.27727129293589, -73.0262184425271 64.2150154365133, -72.9183387403406 64.1730079913258, -72.89667121780241 64.0405388054216, -72.7271002005138 63.988319419006, -72.60951589254429 63.8962059231649, -72.4569584295878 63.8165321574332, -72.3577304779791 63.8243980678166, -72.210933226687 63.8205108975599, -72.1250872168016 63.7320747691354, -71.9750016879774 63.6924095506603, -71.8269303419616 63.7619553070918, -71.67220613160769 63.6913147447797, -71.51773368466171 63.5926056372404, -71.57384024313259 63.4262543043927, -71.6588241080461 63.417434720536, -71.7607070705748 63.3189354095415, -71.6622038781064 63.1676788411057, -71.558505089938 63.1224060151256, -71.4669980851935 63.0994415406946, -71.3256787294896 63.0382804991458, -71.1630733697401 63.0500068870083, -70.82220713335251 63.1167564776294, -70.92711897566021 63.0537643732273, -70.8385797831304 62.9195671310183, -70.7098413133258 62.8894119518002, -70.5622811212766 62.8754272763478, -70.4596046797122 62.8648834565064, -70.33521496080169 62.7985802021147, -70.171114228139 62.7537231812798, -70.0178777689438 62.7597580369039, -69.83246051104371 62.7897911583353, -69.6793842642724 62.7753449040441, -69.52267640938869 62.6744728622328, -69.41952693072319 62.583488513784, -69.27991656569679 62.5175133248801, -69.15217753766311 62.4323425763489, -69.08314674022979 62.4139709953652, -68.9529586846955 62.3789711493991, -68.7664275265091 62.3391762306986, -68.6182493455308 62.2785416181013, -68.50950753254649 62.2540474481313, -68.38235598124341 62.2433777438878, -68.3025677345132 62.2349472643049, -68.15387839597111 62.2275353119052, -68.04071152911369 62.2018738444024, -67.9512263199386 62.2076073381082, -67.7808847464339 62.1904335740502, -67.667031235503 62.1683731808777, -67.5475243460717 62.1769142932601, -67.4579933542098 62.1444474058232, -67.32158734378081 62.1284294945256, -67.2217414050418 62.0822869144096, -67.0966879360993 62.047626576905, -67.00144470690761 62.0415908840121, -66.8809755518802 62.0379386886197, -66.7458287362071 62.010521850603, -66.56848938137981 61.9408073255976, -66.38661301427911 61.8848849311947, -66.2605215757101 61.8666706344423, -66.1703828831634 61.8680953889911, -66.05410316772689 61.8776442043167, -66.00581322420081 61.9763169972317, -66.0284912942377 62.1202253086511, -66.0977332048235 62.22617411964, -66.15206509962459 62.3074199749519, -66.2784021682254 62.3385812068573, -66.39642556209 62.3532548951973, -66.49938548075529 62.4683787713633, -66.6196345500943 62.606103143845, -66.72430870248451 62.6434864261577, -66.8661701200549 62.6626958686195, -66.9795012193058 62.7005728337693, -67.11189343043741 62.8094941613265, -67.2228401454563 62.860798026208, -67.2971047148107 62.9185220279693, -67.40728086282149 62.9699250745826, -67.4872827372085 63.0039674330507, -67.6298914518289 63.0330010920268, -67.7365657036442 63.0789834534237, -67.8876354280325 63.1482202090954, -67.9837276999733 63.1604730142183, -68.1048443992495 63.1794435023158, -68.1727079036468 63.2291223048505, -68.2685407877272 63.3055345101096, -68.3723692770204 63.3671151690299, -68.49981075536201 63.4394761622952, -68.6105057015768 63.4779244946198, -68.7273270597959 63.5467111180237, -68.7948930207962 63.6070787039191, -68.6855256420946 63.7386590718588, -68.49940644894841 63.7373620803958, -68.35683587007929 63.6653329657439, -68.1920713792907 63.6187211814374, -68.06613289831461 63.5909959640925, -67.992333722723 63.5498315649748, -67.8937161140074 63.5086557228833, -67.7660381436453 63.558918182728, -67.5598460266726 63.5650407771102, -67.4963312776663 63.502723880103, -67.3908853549751 63.4287416394152, -67.27697079896311 63.3634721665073, -67.18624961642659 63.3173906233786, -67.0267875607972 63.2847291874022, -66.93208383220301 63.2137033357404, -66.85270756310349 63.1551935072746, -66.7301641604476 63.0855638369016, -66.5590520172367 63.0848581227043, -66.5560689390303 63.2054750444379, -66.64127406774401 63.2679483443407))"
p = shapely.geometry.Polygon(wkt.loads(mywkt))
print(p.is_valid)
shape_feature = ShapelyFeature([p], ccrs.Geodetic(), edgecolor='black', facecolor="red")
ax.add_feature(shape_feature)
plt.show()

The polygon does not have ccw sequence. To get it right, you need to reverse the sequence. Here is the relevant code that does the trick for you.
# Your code is OK up to this line:
p = shapely.geometry.Polygon(wkt.loads(mywkt))
# Add these lines to reverse vertices sequence
px = p.exterior
if px.is_ccw==False:
px.coords = list(px.coords)[::-1]
# Continue your remaining code.
Hope this is useful.

Related

Dynamic plotting of multiple circles in python (to see the evolution of a certain trajectory)

I am new to python and was wondering how to dynamically plot multiple circles (one by one) to see the evolution of a certain trajectory for example. All I have as information given to me are the coordinates of each circle (it's center) and the radius of the circles (always 110 mm).
I wrote a very simple code to show you what I would like the final product to look like and the Matrix I use for the coordinates of the center of each circles. I don't know how to animate it though. I looked at matplotlib.animation, but can't seem to find a way to print on the same graph multiple circles one by one and follow that evolution.
Thanks to anyone who can help me :)
Here is the simple code I wrote: (you can copy/paste to see output)
import matplotlib.pyplot as plt
M=[[-353.367, 1033.919], [-332.2289, 1022.62], [-308.9693, 1012.15], [-275.6987, 1009.794], [-255.759, 1012.831], [-227.1466, 1022.136], [-205.0762, 1040.001], [-186.8659, 1063.798], [-169.1376, 1095.61], [-164.1178, 1115.52], [-164.6858, 1145.322], [-172.5163, 1173.053], [-190.4497, 1203.225], [-217.9684, 1218.559], [-243.6059, 1226.065], [-268.9684, 1229.198], [-284.552, 1236.618], [-313.0649, 1245.422], [-341.8054, 1240.937], [-373.7917, 1232.477], [-390.5454, 1213.948], [-414.7616, 1177.002], [-431.7343, 1131.7], [-430.5874, 1111.886], [-422.6885, 1080.612], [-406.6531, 1057.562], [-386.0578, 1045.479], [-363.1557, 1032.198], [-334.0824, 1029.815], [-306.1595, 1034.832], [-275.797, 1050.248], [-251.0765, 1063.588], [-234.0385, 1088.162], [-218.9364, 1116.517], [-215.2409, 1142.459], [-216.5757, 1162.321], [-224.0054, 1193.865], [-239.5592, 1210.065], [-256.5946, 1226.277], [-269.817, 1233.805], [-282.3394, 1244.219], [-310.8614, 1250.199], [-325.9461, 1258.774], [-352.6251, 1273.536], [-378.2938, 1278.17], [-392.2101, 1273.354], [-419.7631, 1259.556], [-443.834, 1239.024], [-462.2394, 1213.344], [-473.6183, 1184.354], [-476.9157, 1148.379], [-471.1018, 1132.955], [-464.7671, 1108.116], [-447.4711, 1086.088], [-424.2345, 1070.545], [-394.7679, 1063.421], [-365.0302, 1062.413], [-337.4447, 1070.366], [-313.6294, 1079.923], [-294.4435, 1100.018], [-273.5304, 1131.008], [-262.598, 1146.377], [-259.7155, 1184.271], [-263.7368, 1213.217], [-275.8035, 1238.906], [-294.8008, 1253.269], [-310.6419, 1267.708], [-337.7829, 1278.309], [-363.7509, 1287.402], [-381.0431, 1308.681], [-401.0663, 1319.924], [-422.0269, 1317.162], [-448.6474, 1304.716], [-473.7899, 1285.203], [-496.8754, 1260.192], [-508.5398, 1227.624], [-512.2774, 1187.758], [-507.8042, 1168.757], [-495.4691, 1143.232], [-476.2492, 1122.938], [-447.8833, 1111.69], [-432.8667, 1105.275], [-403.2355, 1104.286], [-383.3333, 1108.894], [-353.2146, 1119.441], [-328.9186, 1138.472], [-314.5862, 1163.125], [-302.3018, 1181.773], [-296.0374, 1201.452], [-295.3168, 1231.432], [-302.4509, 1259.988], [-313.687, 1280.337], [-329.2202, 1301.075], [-357.1423, 1315.207], [-381.2958, 1324.296], [-407.1058, 1341.854], [-419.993, 1363.024], [-430.4856, 1369.453], [-446.1629, 1365.734], [-475.8101, 1352.226], [-498.8694, 1339.29], [-517.2361, 1311.655], [-530.3865, 1294.905], [-533.8939, 1267.289], [-535.2554, 1237.41], [-528.8127, 1214.766], [-516.8326, 1189.832], [-496.7888, 1170.367], [-465.8856, 1158.17], [-438.7599, 1153.918], [-414.804, 1158.549], [-380.3575, 1171.258], [-352.4823, 1191.146], [-332.798, 1216.418], [-321.6086, 1249.139], [-318.3846, 1279.136], [-323.3752, 1307.856], [-338.4404, 1337.587], [-354.3026, 1350.77], [-380.7487, 1364.942], [-406.5152, 1379.089], [-426.5342, 1393.325], [-434.4213, 1411.274], [-440.8358, 1423.097], [-457.6952, 1421.923], [-477.868, 1411.24], [-506.8693, 1392.765], [-528.1245, 1364.994], [-539.1038, 1336.496], [-546.2713, 1316.544], [-545.8773, 1290.688], [-535.0356, 1262.777], [-518.2655, 1229.677], [-486.7253, 1212.485], [-454.5135, 1207.969], [-418.6611, 1212.69], [-390.2291, 1225.6], [-358.2794, 1253.604], [-341.0657, 1280.357], [-329.5321, 1319.843], [-331.4965, 1358.81], [-347.1193, 1387.638], [-374.7163, 1415.354], [-413.9228, 1428.634], [-434.3252, 1449.438], [-436.9276, 1468.692], [-439.6375, 1479.155], [-455.6206, 1474.209], [-489.7198, 1453.96], [-521.986, 1422.663], [-539.7686, 1384.55], [-543.0701, 1354.541], [-537.5444, 1325.794], [-520.8219, 1296.02], [-499.6737, 1277.328], [-471.653, 1263.555], [-428.4604, 1263.644], [-399.12, 1271.29], [-367.6371, 1293.805], [-341.3217, 1326.764], [-327.3464, 1365.63], [-327.6788, 1410.848], [-342.2673, 1444.753], [-364.3698, 1464.017], [-395.0403, 1480.398], [-421.46, 1499.156], [-426.0284, 1523.588], [-415.4504, 1532.763], [-397.0308, 1529.162], [-373.9081, 1516.305], [-341.4985, 1495.441], [-325.3263, 1468.436], [-312.1562, 1423.641], [-311.4977, 1393.881], [-317.5511, 1362.964], [-339.4044, 1336.628], [-371.6758, 1317.971], [-416.1536, 1315.724], [-455.3117, 1327.009], [-483.2677, 1351.7], [-508.9474, 1376.833], [-523.6409, 1411.456], [-526.3406, 1441.475], [-519.9404, 1482.489], [-493.0933, 1511.768], [-469.0362, 1526.164], [-432.756, 1543.481], [-403.2769, 1563.808], [-391.6133, 1579.361], [-366.1702, 1574.822], [-342.1183, 1555.277], [-310.115, 1533.289], [-288.2865, 1496.987], [-280.5991, 1467.129], [-282.1186, 1428.055], [-299.5267, 1394.554], [-319.8267, 1375.856], [-350.9147, 1362.371], [-400.0623, 1364.378], [-430.872, 1380.399], [-465.416, 1413.822], [-491.4677, 1451.262], [-496.9905, 1481.268], [-492.8149, 1514.172], [-471.2688, 1547.293], [-450.6965, 1568.428], [-424.2299, 1581.129], [-384.9122, 1598.29], [-356.1093, 1614.251], [-338.9459, 1617.087], [-314.669, 1606.542], [-284.1367, 1579.615], [-254.9558, 1554.324], [-243.4746, 1525.626], [-240.235, 1485.667], [-250.3822, 1448.694], [-273.6103, 1419.559], [-297.7935, 1405.516], [-329.8026, 1400.002], [-388.7009, 1413.653], [-414.6778, 1432.243], [-436.7974, 1456.532], [-453.1871, 1494.635], [-456.7498, 1538.398], [-445.8698, 1574.179], [-419.0897, 1601.206], [-388.5554, 1615.067], [-354.7166, 1632.194], [-314.4724, 1643.525], [-280.2189, 1641.387], [-251.6241, 1629.778], [-230.1354, 1603.68], [-208.1479, 1577.854], [-193.7817, 1538.851], [-193.3607, 1508.953], [-201.3903, 1477.549], [-223.9322, 1444.292], [-256.7153, 1430.09], [-295.3913, 1426.766], [-334.7743, 1436.521], [-366.4014, 1460.237], [-389.7171, 1484.86], [-406.069, 1523.158], [-408.3097, 1562.998], [-400.4853, 1590.734], [-382.6054, 1620.927], [-351.6727, 1638.374], [-314.2585, 1647.47], [-280.8991, 1656.951], [-235.3465, 1655.062], [-197.5304, 1637.869], [-173.212, 1616.99], [-154.4203, 1587.273], [-139.5245, 1547.907], [-142.0162, 1495.963], [-158.593, 1473.332], [-186.9524, 1450.806], [-217.6703, 1440.713], [-257.2566, 1443.088], [-295.5097, 1459.153], [-327.2386, 1487.681], [-344.2355, 1514.735], [-355.4432, 1554.364], [-354.3744, 1583.898], [-340.4795, 1610.833], [-321.8054, 1639.23], [-288.5625, 1652.806], [-259.8545, 1659.756], [-213.7623, 1660.159], [-173.7876, 1654.228], [-136.6984, 1635.506], [-106.9547, 1605.329], [-90.45823, 1567.705], [-84.31964, 1531.705], [-89.45423, 1502.995], [-102.8442, 1467.743], [-142.8958, 1444.217], [-181.0074, 1440.788], [-218.7369, 1447.717], [-254.862, 1468.821], [-281.443, 1492.04], [-294.3479, 1527.382], [-300.8553, 1549.361], [-298.0456, 1584.913], [-289.9435, 1611.776], [-267.7791, 1633.974], [-242.0123, 1650.594], [-217.7545, 1658.31], [-184.2669, 1654.097], [-144.8474, 1644.531], [-109.7208, 1637.203], [-82.43204, 1621.707], [-59.36412, 1591.363], [-38.09873, 1563.542], [-30.89796, 1533.667], [-33.99216, 1485.436], [-53.55411, 1456.685], [-79.92442, 1435.82], [-107.0308, 1428.931], [-144.5399, 1428.12], [-175.7949, 1438.916], [-202.2502, 1456.225], [-223.8597, 1479.606], [-242.5855, 1517.033], [-247.6185, 1553.075], [-241.5967, 1576.318], [-227.9895, 1610.275], [-204.6221, 1634.386], [-163.3819, 1644.437], [-130.5885, 1633.278], [-98.96542, 1621.423], [-72.5339, 1615.433], [-43.11806, 1597.279], [-5.99537, 1570.966], [10.92261, 1534.209], [18.19289, 1494.239], [10.92241, 1449.578], [-8.584769, 1432.486], [-31.02674, 1410.048], [-69.68215, 1401.698], [-113.4156, 1404.994], [-143.2605, 1419.77], [-162.732, 1440.016], [-185.1952, 1478.715], [-198.0868, 1518.472], [-194.5775, 1547.854], [-180.9837, 1589.557], [-153.4026, 1607.14], [-123.1798, 1617.854], [-87.55124, 1607.125], [-66.58879, 1591.694], [-47.51177, 1581.649], [-15.81822, 1567.318], [21.46709, 1544.121], [46.09593, 1509.497], [57.37705, 1480.045], [57.36242, 1440.343], [44.0697, 1404.997], [18.25326, 1381.899], [-7.351728, 1370.322], [-32.2257, 1364.652], [-66.11478, 1368.577], [-100.2056, 1380.902], [-124.5261, 1403.595], [-145.2667, 1438.634], [-157.1636, 1474.231], [-157.4587, 1513.443], [-145.1665, 1539.583], [-119.9591, 1566.394], [-89.40922, 1580.139], [-53.67052, 1570.516], [-32.64999, 1548.108], [-16.66615, 1536.516], [6.490777, 1529.025], [31.97713, 1514.448], [60.08655, 1484.993], [81.14851, 1453.727], [88.46942, 1423.841], [81.74537, 1375.701], [67.44641, 1351.281], [46.61972, 1332.588], [15.0952, 1320.228], [-12.26343, 1318.346], [-42.129, 1324.324], [-75.54681, 1338.252], [-101.5572, 1367.278], [-118.0977, 1394.486], [-128.0707, 1438.201], [-123.8414, 1472.884], [-108.7894, 1501.175], [-78.72721, 1525.078], [-46.19279, 1533.898], [-23.59591, 1521.328], [-7.722545, 1495.371], [2.110508, 1483.757], [27.42411, 1475.468], [61.11173, 1453.39], [88.5411, 1429.751], [100.7274, 1398.062], [106.1155, 1368.119], [103.9678, 1333.036], [86.48449, 1299.491], [58.09462, 1276.048], [31.18663, 1267.566], [-4.258423, 1266.381], [-33.82413, 1275.022], [-69.1487, 1297.679], [-89.99754, 1330.714], [-107.3487, 1359.621], [-111.1309, 1389.672], [-103.752, 1441.02], [-68.02649, 1469.898], [-34.99534, 1481.813], [-8.461586, 1473.875], [-0.1402668, 1452.983], [2.639243, 1434.012], [9.501756, 1428.734], [35.01265, 1418.973], [60.25186, 1402.637], [82.93935, 1380.076], [103.5159, 1343.29], [110.4929, 1299.416], [104.37, 1265.672], [88.31962, 1249.234], [71.85352, 1226.983], [44.76006, 1218.187], [25.78514, 1211.846], [-4.032246, 1212.962], [-35.28857, 1220.177], [-60.11924, 1236.0], [-78.1687, 1258.27], [-99.41018, 1281.373], [-100.1344, 1281.373], [-102.1007, 1324.693], [-106.4406, 1367.235], [-86.26932, 1399.474], [-60.85463, 1410.725], [-44.8115, 1425.372], [-15.77799, 1422.786], [-3.165829, 1402.704], [-4.200755, 1382.766], [-14.88467, 1374.684], [-37.12067, 1369.416], [-55.75563, 1356.069], [-83.39506, 1335.855], [-102.8612, 1310.411], [-113.7677, 1277.62], [-116.6935, 1247.634], [-111.4152, 1226.374], [-102.5447, 1205.037], [-89.81829, 1182.588], [-62.18055, 1164.957], [-34.44635, 1157.941], [-4.697551, 1158.479], [24.80031, 1166.713], [55.11592, 1189.81], [82.74968, 1214.07], [95.09119, 1242.216], [100.8731, 1278.122], [97.81202, 1307.189], [86.77965, 1333.134], [68.61958, 1354.043], [48.2424, 1369.461], [19.12447, 1372.624], [-0.8152943, 1361.971], [-17.21025, 1340.192], [-32.79333, 1326.46], [-52.63164, 1321.488], [-71.75038, 1307.899], [-102.1292, 1288.402], [-120.6276, 1267.054], [-131.8435, 1248.589], [-137.8415, 1219.025], [-138.2087, 1197.053], [-135.7063, 1170.291], [-122.9438, 1144.966], [-103.781, 1124.957], [-79.13057, 1112.104], [-44.85416, 1107.323], [-20.89322, 1110.864], [11.95125, 1117.725], [32.82501, 1131.874], [47.87254, 1146.108], [64.46723, 1171.654], [76.13985, 1210.502], [76.94341, 1244.263], [69.95406, 1271.886], [53.59194, 1295.287], [28.41202, 1312.713], [5.259316, 1322.112], [-23.90935, 1314.675], [-49.2878, 1295.755], [-65.5571, 1284.006], [-77.68121, 1280.748], [-98.63795, 1271.883], [-128.6358, 1251.447], [-148.9674, 1236.27], [-165.4682, 1209.712], [-172.2538, 1186.354], [-173.92, 1150.461], [-169.078, 1131.317], [-156.4, 1105.401], [-138.3503, 1088.234], [-120.7542, 1072.757], [-87.36267, 1065.241], [-63.5072, 1064.928], [-39.72315, 1073.128], [-3.400684, 1088.865], [16.01539, 1107.792], [30.89093, 1128.555], [40.31864, 1153.436], [43.63892, 1183.407], [38.95707, 1212.601], [27.39531, 1233.896], [15.34975, 1256.269], [-7.563136, 1272.373], [-38.67617, 1279.378], [-66.80437, 1268.137], [-93.26956, 1258.181], [-111.3685, 1247.295], [-145.3048, 1237.571], [-168.5001, 1226.962], [-189.6831, 1208.908], [-198.3916, 1197.501], [-210.5756, 1160.37], [-217.9469, 1130.447], [-214.9319, 1095.652], [-204.0248, 1078.16], [-190.0907, 1059.948], [-170.6554, 1042.757], [-143.9037, 1033.326], [-114.5352, 1031.72], [-84.7283, 1039.733], [-53.30964, 1055.187], [-30.04264, 1077.168], [-12.66869, 1103.878], [-1.486558, 1137.312], [-1.064299, 1157.3], [-5.341586, 1185.994], [-18.21387, 1215.658], [-40.97787, 1228.778], [-54.11655, 1236.161], [-76.08755, 1246.099], [-105.6179, 1244.681], [-135.4744, 1236.043], [-174.7149, 1232.536], [-184.5176, 1225.9], [-220.0076, 1220.869], [-249.2242, 1210.986], [-276.3006, 1191.326], [-301.6652, 1173.228], [-316.6139, 1150.25], [-323.868, 1117.092], [-326.3884, 1097.087], [-322.1166, 1059.155], [-304.9708, 1034.98], [-285.515, 1018.597], [-263.4151, 1006.624], [-234.8072, 1002.626], [-200.9287, 1007.067], [-170.5251, 1019.518], [-150.2951, 1036.233], [-124.2688, 1064.341], [-113.8737, 1093.0], [-109.5216, 1132.898], [-115.8234, 1161.196], [-129.8293, 1185.697], [-153.6055, 1207.544], [-179.8143, 1217.768], [-214.8842, 1221.203], [-244.943, 1224.325], [-269.0096, 1226.253], [-304.297, 1221.33], [-317.3503, 1210.094], [-345.5856, 1190.689], [-362.5714, 1165.841], [-378.0464, 1137.345], [-381.3854, 1113.39], [-379.4114, 1083.903], [-366.9059, 1048.939], [-341.1812, 1028.518], [-317.4363, 1013.372], [-290.7552, 1009.945], [-262.8416, 1011.018], [-239.3678, 1021.605], [-207.5487, 1032.593], [-190.1064, 1054.388], [-173.9324, 1086.723], [-165.1843, 1106.384], [-164.0149, 1146.167], [-172.0801, 1177.211], [-199.4012, 1203.769], [-216.9979, 1220.357], [-260.0022, 1228.974], [-277.6455, 1236.919], [-311.9639, 1245.999], [-346.5475, 1241.313], [-374.5607, 1227.686], [-398.7542, 1207.302], [-417.3307, 1181.762], [-429.835, 1154.858], [-432.4515, 1136.847], [-431.9319, 1112.987], [-423.1529, 1088.577], [-413.3953, 1064.005], [-388.5888, 1045.024], [-362.3439, 1032.438], [-333.2143, 1029.841], [-309.2604, 1033.416], [-284.1591, 1040.317], [-261.3084, 1053.24], [-243.3964, 1075.171], [-225.6251, 1107.614], [-216.5621, 1131.302], [-216.7274, 1167.062], [-225.1822, 1194.614], [-241.3986, 1213.666], [-257.2806, 1232.254], [-283.1432, 1243.899], [-313.6137, 1251.092], [-339.1638, 1264.362], [-357.9759, 1276.749], [-389.4101, 1274.932], [-415.205, 1267.437], [-432.2913, 1245.894], [-457.3329, 1212.539], [-476.3467, 1179.487], [-476.8618, 1149.49], [-470.8127, 1115.837], [-458.1615, 1099.495], [-441.1498, 1079.599], [-409.1117, 1066.836], [-382.1244, 1062.366], [-368.1576, 1063.508], [-329.0669, 1072.368], [-306.1739, 1086.485], [-284.2929, 1108.712], [-271.2411, 1135.612]]
fig, ax = plt.subplots()
for [x,y] in M:
circle = plt.Circle((-x,y), 110, color='r',alpha=0.015)
ax.add_patch(circle)
plt.tight_layout()
plt.grid()
plt.axis([-300, 800, 800, 1800])
plt.show()

Plot wont display in python

I'm using the following bit of code to plot two arrays of the same length -
import matplotlib.pyplot as plt
from scipy import stats
from scipy.stats import linregress
G_mag_values = [11.436, 11.341, 11.822, 11.646, 11.924, 12.057, 11.884, 11.805, 12.347, 12.662, 12.362, 12.555, 12.794, 12.819, 12.945, 12.733, 12.789, 12.878, 12.963, 13.094, 13.031, 12.962, 13.018, 12.906, 13.016, 13.088, 13.04, 13.035, 13.094, 13.032, 13.216, 13.062, 13.083, 13.126, 13.101, 13.089, 13.073, 13.182, 13.116, 13.145, 13.235, 13.161, 13.154, 13.383, 13.315, 13.429, 13.461, 13.287, 13.494, 13.459, 13.478, 13.534, 13.536, 13.536, 13.483, 13.544, 13.564, 13.544, 13.608, 13.655, 13.665, 13.668, 13.697, 13.649, 13.742, 13.756, 13.671, 13.701, 13.788, 13.723, 13.697, 13.713, 13.708, 13.765, 13.847, 13.992, 13.706, 13.79, 13.783, 13.844, 13.945, 13.928, 13.936, 13.956, 13.898, 14.059, 13.945, 14.039, 13.999, 14.087, 14.05, 14.083, 14.136, 14.124, 14.189, 14.149, 14.182, 14.281, 14.177, 14.297, 14.268, 14.454, 14.295]
G_cal_values = [-8.553610547563503, -8.085853602272588, -7.98491731861732, -7.852060056526794, -7.550944423333883, -7.569289687795749, -7.547088847268468, -7.544445036682168, -7.480698829329534, -7.184407435874912, -7.382606680295108, -7.2231275160942054, -7.093385973539046, -7.0473097125206685, -6.775012624594927, -6.814667514017907, -6.719898703328146, -6.741699011193633, -6.483121454948265, -6.320533066162438, -6.216044707275117, -6.037365656714626, -6.058593802250578, -6.0203190345840865, -6.036176430437363, -5.817887798572345, -5.838439347527171, -5.864922270102037, -5.755152671040021, -5.7709095683554725, -5.729226240967218, -5.606533007538604, -5.5817719334376905, -5.578993138005095, -5.62616747769538, -5.648413591916503, -5.611676700504294, -5.557722166623976, -5.5584623064502825, -5.425878164810264, -5.582204334985258, -5.529395790688368, -5.560750195967957, -5.433224654816512, -5.4751198268734385, -5.4592032005417215, -5.514591770369543, -5.580278698184566, -5.520695348050357, -5.501615700174841, -5.578645415877418, -5.692203332547151, -5.569497861450115, -5.335209902666812, -5.470963349023013, -5.44265375533589, -5.538541653702721, -5.355732832969871, -5.318164588926453, -5.376154615199398, -5.372133774215322, -5.361689907587619, -5.37608154921679, -5.412657572197508, -5.454613589602333, -5.339384591430104, -5.367511403407703, -5.258069473329993, -5.347580031901464, -4.9905279263992, -5.445096880253789, -5.192885553786512, -5.2983352094538505, -5.3930571447307365, -5.057910469318639, -5.32585105504838, -5.238649399637653, -5.122431894813153, -5.084559296025157, -5.139042420486851, -4.9919273140342915, -5.103619454431522, -5.017946144298159, -4.98136832081144, -5.084355565584671, -5.048634391386494, -4.887073481359435, -5.074683293771264, -5.050703776716202, -5.104772289705188, -4.9597601680524415, -4.971489935988787, -4.895283369236485, -4.9859511256778974, -4.840717539517584, -4.815665714699117, -4.937635861879118, -4.887219819695687, -4.813729758415283, -4.82667464608015, -4.865176481168528, -4.885105289124561, -4.887072278243732]
fig, ax = plt.subplots()
plt.scatter(G_mag_values,G_cal_values)
ax.minorticks_on()
ax.grid(which='major', linestyle='-', linewidth='0.5')
ax.grid(which='minor', linestyle='-', linewidth='0.5')
fig.set_size_inches(10,7)
best_fit_Y_G = []
slope_G, intercept_G, r_value_G, p_value_G, std_err_G = stats.linregress(G_mag_values,G_cal_values)
for value_G in G_mag_values:
best_fit_Y_G.append(intercept_G + slope_G*value_G)
plt.plot(G_mag_values, best_fit_Y_G, 'r', label = 'Best fit')
plt.title('M67 Calibration graph for G filter')
plt.xlabel('Real magnitude')
plt.ylabel('Measured magnitude')
plt.show()
curve_G = np.polyfit(G_mag_values,G_cal_values,1)
print('G filter polyfit line: slope {}; intercept = {}'.format(curve_G[0],curve_G[1]))
print('G filter linregress: slope {}; intercept = {}'.format(slope_G,intercept_G))
When I run this, it prints the values for slope and intercept from the best_fit_Y_G and curve_G, but it doesnt display the plot at all. Where am I going wrong?
I copy/pasted and run your code.
curve_G = np.polyfit(G_mag_values,G_cal_values,1)
That line gives me error. Then I imported numpy as np and problem solved.
output figure

Matplotlib and cartopy contours coming out completely wrong

I'm trying to plot latency maps of the world (for the moment, from London).
I have a database with a series of latencies between GPS points. I'd like to plot these on a map of the world with contour lines at various levels of latency, eg 50ms, 100ms, 200ms+ etc.
Right now I'm working with some sample code from the cartopy project for drawing labelled contours.
#!/usr/bin/env python3
from collections import defaultdict
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import numpy as np
from cartopy.examples.waves import sample_data
def main():
# lat, long and then latency from london
zs = [
(55.30927, 25.07725, 123.822),
(19.81889, 41.3275, 61.154),
(-58.37723, -34.61315, 250.024),
(16.37208, 48.20849, 26.987),
(15.45, 47.06667, 49.47),
(115.8614, -31.95224, 273.459),
(138.59863, -34.92866, 257.056),
(151.20732, -33.86785, 258.382),
(144.96332, -37.814, 259.542),
(149.12807, -35.28346, 291.766),
(153.02809, -27.46794, 283.675),
(49.89201, 40.37767, 89.569),
(90.40744, 23.7104, 232.381),
(5.56749, 50.63373, 11.093),
(4.34878, 50.85045, 9.644),
(3.22424, 51.20892, 10.713),
(4.40026, 51.22047, 10.905),
(27.91667, 43.21667, 47.367),
(23.32415, 42.69751, 52.414),
(-34.86306, -7.115, 208.491),
(-46.63611, -23.5475, 180.227),
(-47.92972, -15.77972, 227.893),
(-113.46871, 53.55014, 138.725),
(-73.58781, 45.50884, 84.456),
(-75.69812, 45.41117, 83.592),
(-106.66892, 52.13238, 108.197),
(-79.4163, 43.70011, 90.885),
(-123.11934, 49.24966, 130.308),
(-97.14704, 49.8844, 159.187),
(-63.57291, 44.6464, 74.226),
(-71.21454, 46.81228, 85.994),
(8.55, 47.36667, 22.503),
(8.96004, 46.01008, 33.557),
(6.63282, 46.516, 27.375),
(7.44744, 46.94809, 18.838),
(7.57327, 47.55839, 27.376),
(-70.64827, -33.45694, 195.647),
(114.0683, 22.54554, 233.257),
(121.45806, 31.22222, 191.371),
(120.16142, 30.29365, 237.881),
(114.87944, 40.81, 206.936),
(-75.56359, 6.25184, 150.812),
(-74.08175, 4.60971, 153.555),
(-84.11651, 10.00236, 140.832),
(33.03794, 34.68406, 64.037),
(14.42076, 50.08804, 27.247),
(16.60796, 49.19522, 29.384),
(11.07752, 49.45421, 17.739),
(11.57549, 48.13743, 20.171),
(9.99302, 53.55073, 43.907),
(8.68417, 50.11552, 13.733),
(6.77616, 51.22172, 12.371),
(12.10466, 47.7044, 19.85),
(12.56553, 55.67594, 16.867),
(3.08746, 36.73225, 40.629),
(-78.52495, -0.22985, 161.671),
(24.75353, 59.43696, 34.33),
(31.24967, 30.06263, 63.373),
(-0.37739, 39.46975, 32.652),
(-3.70256, 40.4165, 23.838),
(2.15899, 41.38879, 29.685),
(24.93545, 60.16952, 33.84),
(7.74553, 48.58392, 13.785),
(3.17456, 50.69421, 4.4),
(2.3488, 48.85341, 8.154),
(4.84671, 45.74846, 21.794),
(-1.61396, 54.97328, 5.601),
(-2.23743, 53.48095, 7.15),
(0.51667, 51.26667, 1.404),
(-1.12902, 50.79509, 3.552),
(-3.19648, 55.95206, 10.398),
(-1.51217, 52.40656, 6.462),
(-2.07972, 51.90006, 4.167),
(-3.18, 51.48, 6.116),
(-2.59665, 51.45523, 4.497),
(-5.92541, 54.59682, 10.33),
(44.83368, 41.69411, 88.784),
(-0.1969, 5.55602, 97.088),
(23.72784, 37.98376, 56.074),
(22.93086, 40.64361, 61.766),
(-90.51327, 14.64072, 342.363),
(114.17469, 22.27832, 219.061),
(15.97798, 45.81444, 31.988),
(19.04045, 47.49835, 28.312),
(106.84513, -6.21462, 191.802),
(-6.24889, 53.33306, 11.926),
(-6.92611, 52.84083, 12.585),
(35.21633, 31.76904, 63.795),
(34.78057, 32.08088, 111.049),
(-4.48333, 54.15, 14.732),
(78.15538, 11.65376, 70.362),
(73.85535, 18.51957, 274.087),
(77.22445, 28.63576, 150.969),
(80.27847, 13.08784, 144.037),
(75.8333, 22.71792, 137.159),
(78.45636, 17.38405, 187.213),
(72.88261, 19.07283, 187.64),
(77.59369, 12.97194, 131.413),
(-21.89541, 64.13548, 38.428),
(13.33561, 38.13205, 52.313),
(12.33265, 45.43713, 38.471),
(12.51133, 41.89193, 31.075),
(9.18951, 45.46427, 30.8),
(13.34109, 41.63976, 41.74),
(11.88068, 43.46276, 34.63),
(139.69171, 35.6895, 215.214),
(135.50218, 34.69374, 233.671),
(141.35, 43.06667, 242.554),
(36.81667, -1.28333, 174.557),
(104.91601, 11.56245, 208.175),
(126.9784, 37.566, 266.233),
(73.10211, 49.80187, 123.06),
(35.50157, 33.89332, 64.207),
(25.2798, 54.68916, 38.825),
(23.31667, 55.93333, 42.406),
(6.13, 49.61167, 15.731),
(24.10589, 56.946, 38.292),
(-5.00028, 34.03313, 77.605),
(28.8575, 47.00556, 51.354),
(14.5148, 35.89968, 48.426),
(-103.39182, 20.66682, 132.319),
(3.39467, 6.45407, 96.877),
(4.29861, 52.07667, 8.78),
(4.47917, 51.9225, 6.952),
(6.56667, 53.21917, 8.917),
(5.47778, 51.44083, 8.77),
(5.71806, 52.525, 7.052),
(4.88969, 52.37403, 6.88),
(4.66111, 51.86583, 9.274),
(10.74609, 59.91273, 28.073),
(5.32415, 60.39299, 28.998),
(174.77557, -41.28664, 282.141),
(172.63333, -43.53333, 273.846),
(174.76349, -36.84853, 279.023),
(-79.51973, 8.9936, 150.216),
(-77.02824, -12.04318, 163.241),
(120.9822, 14.6042, 220.693),
(120.33325, 16.04313, 245.787),
(74.35071, 31.558, 150.221),
(21.01178, 52.22977, 31.504),
(18.64912, 54.35227, 38.029),
(-66.10572, 18.46633, 106.983),
(-9.13333, 38.71667, 50.867),
(26.10626, 44.43225, 47.648),
(21.90333, 43.32472, 48.541),
(20.46513, 44.80401, 38.377),
(30.31413, 59.93863, 36.871),
(37.61556, 55.75222, 49.041),
(49.12214, 55.78874, 58.591),
(82.9346, 55.0415, 127.4),
(131.87353, 43.10562, 147.576),
(46.72185, 24.68773, 88.155),
(18.06871, 59.32938, 26.838),
(11.96679, 57.70716, 56.335),
(103.85007, 1.28967, 251.641),
(14.50513, 46.05108, 36.656),
(17.10674, 48.14816, 27.031),
(-55.16682, 5.86638, 166.626),
(100.50144, 13.75398, 265.619),
(10.16579, 36.81897, 52.035),
(27.13838, 38.41273, 67.678),
(32.85427, 39.91987, 60.337),
(28.94966, 41.01384, 51.519),
(29.06013, 40.19559, 72.622),
(121.53185, 25.04776, 261.82),
(39.26951, -6.82349, 150.264),
(30.5238, 50.45466, 41.29),
(36.25272, 49.98081, 58.363),
(32.58219, 0.31628, 159.655),
(-77.03637, 38.89511, 78.045),
(-81.65565, 30.33218, 94.374),
(-80.19366, 25.77427, 99.16),
(-81.37924, 28.53834, 97.119),
(-82.45843, 27.94752, 101.628),
(-84.38798, 33.749, 87.471),
(-81.09983, 32.08354, 119.433),
(-86.15804, 39.76838, 91.394),
(-90.07507, 29.95465, 116.938),
(-76.61219, 39.29038, 77.512),
(-94.57857, 39.09973, 98.802),
(-90.19789, 38.62727, 90.391),
(-90.18481, 32.29876, 105.299),
(-82.55402, 35.60095, 87.855),
(-80.84313, 35.22709, 92.297),
(-78.63861, 35.7721, 83.355),
(-84.51439, 39.12711, 96.657),
(-82.99879, 39.96118, 85.065),
(-97.51643, 35.46756, 108.011),
(-75.16379, 39.95233, 70.659),
(-83.92074, 35.96064, 111.042),
(-90.04898, 35.14953, 106.578),
(-97.74306, 30.26715, 117.634),
(-96.80667, 32.78306, 113.133),
(-95.36327, 29.76328, 105.161),
(-98.49363, 29.42412, 117.229),
(-93.60911, 41.60054, 101.56),
(-87.65005, 41.85003, 89.144),
(-88.30535, 41.88753, 26.006),
(-86.25001, 41.68338, 89.071),
(-71.05977, 42.35843, 75.733),
(-83.04575, 42.33143, 92.257),
(-84.55553, 42.73253, 97.811),
(-93.26384, 44.97997, 97.597),
(-96.66696, 40.8, 97.95),
(-74.39904, 40.49927, 69.437),
(-74.05653, 40.78955, 71.265),
(-73.75623, 42.65258, 74.285),
(-78.87837, 42.88645, 79.13),
(-73.96625, 40.78343, 68.356),
(-74.00597, 40.71427, 71.41),
(-76.14742, 43.04812, 76.038),
(-81.8418, 41.23811, 83.061),
(-81.69541, 41.4995, 93.716),
(-83.55521, 41.66394, 97.349),
(-79.99589, 40.44062, 87.723),
(-75.6649, 41.40916, 73.833),
(-88.01983, 44.51916, 102.133),
(-112.07404, 33.44838, 130.966),
(-121.98857, 37.54827, 134.523),
(-118.24368, 34.05223, 136.396),
(-121.4944, 38.58157, 136.928),
(-117.16472, 32.71571, 146.006),
(-122.41942, 37.77493, 141.373),
(-121.89496, 37.33939, 133.461),
(-104.82136, 38.83388, 108.739),
(-104.9847, 39.73915, 121.039),
(-106.65114, 35.08449, 164.648),
(-115.13722, 36.17497, 146.236),
(-122.39168, 40.58654, 156.6),
(-122.67621, 45.52345, 130.65),
(-123.34174, 43.2165, 147.008),
(-111.89105, 40.76078, 151.18),
(-122.33207, 47.60621, 129.576),
(-157.85833, 21.30694, 186.991),
(-56.18816, -34.90328, 228.291),
(-66.87919, 10.48801, 148.292),
(106.62965, 10.82302, 305.353),
(105.84117, 21.0245, 240.46),
(21.16688, 42.67272, 48.995),
(28.04363, -26.20227, 165.392),
(18.42322, -33.92584, 143.62),
]
z = np.array(zs, dtype="float")
breakpoint()
fig = plt.figure()
# Setup a global EckertIII map with faint coastlines.
ax = fig.add_subplot(1, 1, 1, projection=ccrs.EckertIII())
ax.set_global()
ax.coastlines("110m", alpha=0.1)
# Add colourful filled contours.
filled_c = ax.contourf(z, transform=ccrs.PlateCarree())
# And black line contours.
line_c = ax.contour(
z, levels=filled_c.levels, colors=["black"], transform=ccrs.PlateCarree()
)
# Uncomment to make the line contours invisible.
# plt.setp(line_c.collections, visible=False)
# Add a colorbar for the filled contour.
fig.colorbar(filled_c, orientation="horizontal")
# Use the line contours to place contour labels.
ax.clabel(
line_c, # Typically best results when labelling line contours.
colors=["black"],
manual=False, # Automatic placement vs manual placement.
inline=True, # Cut the line where the label will be placed.
fmt=" {:.0f} ".format, # Labes as integers, with some extra space.
)
plt.show()
if __name__ == "__main__":
main()
Instead of getting nice contours, I get this image:
What's my problem?
Am I passing the wrong args to the matplotlib functions?
Is numpy somehow interpreting my floats wrong?
You need to separate data into lat, long, and height. Then use tricontour type functions to generate the plot. Here is the relevant part of the modified code that handles the data and generate the plot.
# more code before this (see the question)
zs = np.array(zs)
# get `lat`, `long`, and `height` separately
lats, lons, zzz = zs[:,0], zs[:,1], zs[:,2]
fig = plt.figure()
# Setup a global EckertIII map with faint coastlines.
ax = fig.add_subplot(1, 1, 1, projection=ccrs.EckertIII())
ax.set_global()
ax.coastlines("110m", alpha=0.1)
# Add colourful filled contours.
filled_c = ax.tricontourf(lats, lons, zzz, transform=ccrs.PlateCarree())
# And black line contours.
line_c = ax.tricontour(
lats, lons, zzz, levels=filled_c.levels, colors=["black"], transform=ccrs.PlateCarree()
)
# Uncomment to make the line contours invisible.
# plt.setp(line_c.collections, visible=False)
# Add a colorbar for the filled contour.
fig.colorbar(filled_c, orientation="horizontal")
# Use the line contours to place contour labels.
ax.clabel(
line_c, # Typically best results when labelling line contours.
colors=["black"],
manual=False, # Automatic placement vs manual placement.
inline=True, # Cut the line where the label will be placed.
fmt=" {:.0f} ".format, # Labes as integers, with some extra space.
)
plt.show()

guess function when using built-in defined models in lmfit

I am having a problem with the guess function of lmfit. I am trying to fit some experimental data and I want to use different built in models of lmfit, but I cannot run the built in modules, only if I define the function directly.
The following code does not work, but if I comment the guess function it works.
P.S. It would be more interesting for me that the index is the first column because I will put this in a loop that will use all the same first column of the data and therefore i could put each new second column of the data as a new column in the DataFrame.
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from lmfit import Model
from lmfit.models import GaussianModel
minvalue = 3.25
maxvalue = 3.45
rawdata = pd.read_csv('datafile.txt', delim_whitespace = True, names=['XX','YY'])
#Section the data
data = rawdata[(rawdata['XX']>minvalue) & (rawdata['XX'] < maxvalue)]
#Create a DataFrame with the data
dataDataframe = pd.DataFrame()
dataDataframe[0] = data['YY']
dataDataframe = dataDataframe.set_index(data['XX'])
# Gaussian curve
def gaussian(x, amp, cen, wid):
"1-d gaussian: gaussian(x, amp, cen, wid)"
return (amp/(np.sqrt(2*np.pi)*wid)) * np.exp(-(x-cen)**2 /(2*wid**2))
result_gaussian = Model(gaussian).fit(dataDataframe[0], x=dataDataframe.index.values, amp=5, cen=5, wid=1)
mod = GaussianModel()
pars = mod.guess(dataDataframe[0], x = np.float32(dataDataframe.index.values))
out = mod.fit(dataDataframe[0], pars , x = np.float32(dataDataframe.index.values))
plt.plot(dataDataframe.index.values, dataDataframe[0],'bo')
plt.plot(dataDataframe.index.values, result_gaussian.best_fit, 'r-', label = 'Gaussian')
plt.plot(dataDataframe.index.values, out.best_fit, 'b-', label = 'Gaussian2')
plt.legend()
plt.show()
Error message I am having if I uncomment the built in modules:
File "/Users/johndoe/anaconda2/lib/python2.7/site-packages/lmfit/models.py", line 52, in guess_from_peak
cen = x[imaxy]
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
I have tried to run the guess_from_peak from models.py and i did not have a problem it resulted in an integer.
Raw data:
1.1661899e+000 7.3414581e+002
1.1730889e+000 7.4060590e+002
1.1799880e+000 7.3778076e+002
1.1868871e+000 7.2950366e+002
1.1937861e+000 7.0154932e+002
1.2006853e+000 7.0399518e+002
1.2075844e+000 7.3814081e+002
1.2144834e+000 7.5750049e+002
1.2213825e+000 7.6613043e+002
1.2282816e+000 7.4348322e+002
1.2351807e+000 7.2836584e+002
1.2420797e+000 7.0964618e+002
1.2489789e+000 7.1938611e+002
1.2558780e+000 7.0620062e+002
1.2627770e+000 7.2354883e+002
1.2696761e+000 7.1347961e+002
1.2765752e+000 7.1027679e+002
1.2834742e+000 7.4422925e+002
1.2903733e+000 7.5596112e+002
1.2972724e+000 7.2770599e+002
1.3041714e+000 7.2000342e+002
1.3110706e+000 7.4451556e+002
1.3179697e+000 7.4411346e+002
1.3248687e+000 6.9408307e+002
1.3317678e+000 6.8662170e+002
1.3386669e+000 7.0951758e+002
1.3455659e+000 6.7616663e+002
1.3524650e+000 6.7230786e+002
1.3593642e+000 7.1053870e+002
1.3662632e+000 7.2593860e+002
1.3731623e+000 7.1484381e+002
1.3800614e+000 7.3073920e+002
1.3869605e+000 7.2766406e+002
1.3938595e+000 7.1958862e+002
1.4007586e+000 7.0147577e+002
1.4076577e+000 6.9747528e+002
1.4145567e+000 6.9634515e+002
1.4214559e+000 6.6082648e+002
1.4283550e+000 6.4877466e+002
1.4352540e+000 6.6942896e+002
1.4421531e+000 6.8172211e+002
1.4490522e+000 6.5540350e+002
1.4559512e+000 6.4846545e+002
1.4628503e+000 6.6383038e+002
1.4697495e+000 6.4449670e+002
1.4766484e+000 6.3950043e+002
1.4835476e+000 6.4479529e+002
1.4904467e+000 6.4849249e+002
1.4973457e+000 6.4100800e+002
1.5042448e+000 6.6731049e+002
1.5111439e+000 6.8118671e+002
1.5180429e+000 6.5618878e+002
1.5249420e+000 6.3446680e+002
1.5318412e+000 6.3301892e+002
1.5387402e+000 6.5466571e+002
1.5456393e+000 6.5982983e+002
1.5525384e+000 6.3588879e+002
1.5594375e+000 6.1257922e+002
1.5663365e+000 6.2805811e+002
1.5732356e+000 6.1877094e+002
1.5801347e+000 6.0427368e+002
1.5870337e+000 6.3391718e+002
1.5939329e+000 6.4173145e+002
1.6008320e+000 6.2423242e+002
1.6077310e+000 6.0993829e+002
1.6146301e+000 6.0605164e+002
1.6215292e+000 6.2812646e+002
1.6284282e+000 6.4028595e+002
1.6353273e+000 6.2281421e+002
1.6422265e+000 6.0742285e+002
1.6491255e+000 5.9783905e+002
1.6560246e+000 5.8637256e+002
1.6629237e+000 6.0021320e+002
1.6698227e+000 6.1169287e+002
1.6767218e+000 6.1003906e+002
1.6836209e+000 5.9548285e+002
1.6905199e+000 5.8961163e+002
1.6974190e+000 5.9599597e+002
1.7043182e+000 5.9016595e+002
1.7112173e+000 5.7669794e+002
1.7181163e+000 5.6394800e+002
1.7250154e+000 5.5043781e+002
1.7319145e+000 5.6813892e+002
1.7388135e+000 5.8987500e+002
1.7457126e+000 5.9018683e+002
1.7526118e+000 5.8595575e+002
1.7595108e+000 5.8304041e+002
1.7664099e+000 5.9360785e+002
1.7733090e+000 5.9706018e+002
1.7802080e+000 5.7838733e+002
1.7871071e+000 5.7011194e+002
1.7940062e+000 5.8080725e+002
1.8009052e+000 5.7853046e+002
1.8078043e+000 5.7998969e+002
1.8147035e+000 5.4928967e+002
1.8216025e+000 5.2888440e+002
1.8285016e+000 5.4854303e+002
1.8354007e+000 5.5585767e+002
1.8422997e+000 5.5588806e+002
1.8491988e+000 5.5359229e+002
1.8560979e+000 5.5033203e+002
1.8629971e+000 5.2563916e+002
1.8698961e+000 5.3607788e+002
1.8767952e+000 5.7113812e+002
1.8836943e+000 5.5775525e+002
1.8905933e+000 5.2081384e+002
1.8974924e+000 5.1039877e+002
1.9043915e+000 5.3863855e+002
1.9112905e+000 5.6284332e+002
1.9181896e+000 5.5691626e+002
1.9250888e+000 5.3292615e+002
1.9319878e+000 5.4550836e+002
1.9388869e+000 5.6732916e+002
1.9457860e+000 5.4372571e+002
1.9526850e+000 5.1244263e+002
1.9595841e+000 5.1212933e+002
1.9664832e+000 5.1553162e+002
1.9733822e+000 5.2064484e+002
1.9802814e+000 5.3102246e+002
1.9871805e+000 5.2069739e+002
1.9940795e+000 5.0833780e+002
2.0009787e+000 5.1853204e+002
2.0078776e+000 5.2843738e+002
2.0147767e+000 5.2046942e+002
2.0216758e+000 5.4993433e+002
2.0285749e+000 5.4103894e+002
2.0354741e+000 5.0149301e+002
2.0423732e+000 5.0521149e+002
2.0492721e+000 5.2875800e+002
2.0561712e+000 5.1962280e+002
2.0630703e+000 4.9481357e+002
2.0699694e+000 4.9459094e+002
2.0768685e+000 4.9837778e+002
2.0837677e+000 4.9990302e+002
2.0906668e+000 4.9616635e+002
2.0975657e+000 4.9398682e+002
2.1044648e+000 4.9411301e+002
2.1113639e+000 5.0085464e+002
2.1182630e+000 5.1741498e+002
2.1251621e+000 5.1049081e+002
2.1320612e+000 4.9854333e+002
2.1389601e+000 4.9250342e+002
2.1458592e+000 4.8195938e+002
2.1527584e+000 4.9623288e+002
2.1596575e+000 5.0226831e+002
2.1665566e+000 5.1108215e+002
2.1734557e+000 5.0001602e+002
2.1803546e+000 4.8078720e+002
2.1872537e+000 4.9371985e+002
2.1941528e+000 4.9578796e+002
2.2010520e+000 5.0061276e+002
2.2079511e+000 4.9850949e+002
2.2148502e+000 4.9680969e+002
2.2217491e+000 5.0683179e+002
2.2286482e+000 5.0175012e+002
2.2355473e+000 4.8996030e+002
2.2424464e+000 4.8759747e+002
2.2493455e+000 4.7695905e+002
2.2562447e+000 4.7682187e+002
2.2631438e+000 4.8609653e+002
2.2700427e+000 4.8575693e+002
2.2769418e+000 4.9476901e+002
2.2838409e+000 4.8241449e+002
2.2907400e+000 4.7581494e+002
2.2976391e+000 5.0079959e+002
2.3045382e+000 5.0975296e+002
2.3114371e+000 4.9256650e+002
2.3183362e+000 4.8954599e+002
2.3252354e+000 4.9478619e+002
2.3321345e+000 5.1234747e+002
2.3390336e+000 5.4276178e+002
2.3459327e+000 5.4188184e+002
2.3528316e+000 5.4555566e+002
2.3597307e+000 5.4856274e+002
2.3666298e+000 5.2246918e+002
2.3735290e+000 4.9281882e+002
2.3804281e+000 4.8422125e+002
2.3873272e+000 5.0562274e+002
2.3942261e+000 5.0024243e+002
2.4011252e+000 4.8827591e+002
2.4080243e+000 4.8137762e+002
2.4149234e+000 4.7244000e+002
2.4218225e+000 4.7699164e+002
2.4287217e+000 4.7515668e+002
2.4356208e+000 4.6413528e+002
2.4425197e+000 4.6328885e+002
2.4494188e+000 4.6013199e+002
2.4563179e+000 4.6177853e+002
2.4632170e+000 4.5766202e+002
2.4701161e+000 4.4741263e+002
2.4770153e+000 4.4859024e+002
2.4839141e+000 4.6913116e+002
2.4908133e+000 5.0019971e+002
2.4977124e+000 4.8486560e+002
2.5046115e+000 4.6070554e+002
2.5115106e+000 4.3163672e+002
2.5184097e+000 4.4147137e+002
2.5253086e+000 4.3510056e+002
2.5322077e+000 4.4211298e+002
2.5391068e+000 4.6599957e+002
2.5460060e+000 4.5878577e+002
2.5529051e+000 4.4981293e+002
2.5598042e+000 4.6061084e+002
2.5667033e+000 4.6963638e+002
2.5736022e+000 4.7663760e+002
2.5805013e+000 4.6380307e+002
2.5874004e+000 4.5866577e+002
2.5942996e+000 4.5507098e+002
2.6011987e+000 4.4790939e+002
2.6080978e+000 4.6447559e+002
2.6149967e+000 4.5061194e+002
2.6218958e+000 4.2355850e+002
2.6287949e+000 4.2002722e+002
2.6356940e+000 4.2429697e+002
2.6425931e+000 4.2280334e+002
2.6494923e+000 4.3304733e+002
2.6563911e+000 4.5999661e+002
2.6632903e+000 4.7144125e+002
2.6701894e+000 4.6819211e+002
2.6770885e+000 4.6265125e+002
2.6839876e+000 4.6332251e+002
2.6908867e+000 4.5123907e+002
2.6977856e+000 4.6259286e+002
2.7046847e+000 4.6975299e+002
2.7115839e+000 4.4647833e+002
2.7184830e+000 4.4722562e+002
2.7253821e+000 4.6617062e+002
2.7322812e+000 4.6656949e+002
2.7391803e+000 4.4081876e+002
2.7460792e+000 4.5200452e+002
2.7529783e+000 4.5094382e+002
2.7598774e+000 4.4421115e+002
2.7667766e+000 4.5470145e+002
2.7736757e+000 4.5202261e+002
2.7805748e+000 4.4788058e+002
2.7874737e+000 4.3493640e+002
2.7943728e+000 4.4102286e+002
2.8012719e+000 4.3156961e+002
2.8081710e+000 4.2983533e+002
2.8150702e+000 4.4627554e+002
2.8219693e+000 4.4581104e+002
2.8288682e+000 4.2150226e+002
2.8357673e+000 4.1737479e+002
2.8426664e+000 4.5602731e+002
2.8495655e+000 4.6227423e+002
2.8564646e+000 4.5953806e+002
2.8633637e+000 4.5829834e+002
2.8702629e+000 4.5450616e+002
2.8771617e+000 4.5531360e+002
2.8840609e+000 4.4464761e+002
2.8909600e+000 4.6128970e+002
2.8978591e+000 4.4664514e+002
2.9047582e+000 4.4719708e+002
2.9116573e+000 4.4492749e+002
2.9185562e+000 4.4260013e+002
2.9254553e+000 4.5593594e+002
2.9323545e+000 4.6237164e+002
2.9392536e+000 4.7034845e+002
2.9461527e+000 4.7368185e+002
2.9530518e+000 4.7302234e+002
2.9599507e+000 4.7327332e+002
2.9668498e+000 4.4960791e+002
2.9737489e+000 4.4319986e+002
2.9806480e+000 4.5416092e+002
2.9875472e+000 4.6674429e+002
2.9944463e+000 4.6089871e+002
3.0013452e+000 4.6334650e+002
3.0082443e+000 4.6833719e+002
3.0151434e+000 4.8842966e+002
3.0220425e+000 4.8455182e+002
3.0289416e+000 4.6504678e+002
3.0358407e+000 4.6673508e+002
3.0427399e+000 4.6887064e+002
3.0496387e+000 4.6799823e+002
3.0565379e+000 4.5299500e+002
3.0634370e+000 4.5381485e+002
3.0703361e+000 4.5956931e+002
3.0772352e+000 4.6477676e+002
3.0841343e+000 4.6114374e+002
3.0910332e+000 4.6816293e+002
3.0979323e+000 4.6245181e+002
3.1048315e+000 4.6533044e+002
3.1117306e+000 4.7819165e+002
3.1186297e+000 4.9699246e+002
3.1255288e+000 4.8907956e+002
3.1324277e+000 4.9116394e+002
3.1393268e+000 5.0308936e+002
3.1462259e+000 5.0668982e+002
3.1531250e+000 5.0537222e+002
3.1600242e+000 4.9574966e+002
3.1669233e+000 4.9894128e+002
3.1738222e+000 4.9885315e+002
3.1807213e+000 5.1417163e+002
3.1876204e+000 5.2202740e+002
3.1945195e+000 5.2219598e+002
3.2014186e+000 5.4433679e+002
3.2083178e+000 5.6957477e+002
3.2152169e+000 5.9891089e+002
3.2221158e+000 6.0682019e+002
3.2290149e+000 6.0779541e+002
3.2359140e+000 6.1212280e+002
3.2428131e+000 6.5589185e+002
3.2497122e+000 7.1807507e+002
3.2566113e+000 7.5950916e+002
3.2635102e+000 8.1842242e+002
3.2704093e+000 9.1277783e+002
3.2773085e+000 1.0486207e+003
3.2842076e+000 1.3214080e+003
3.2911067e+000 1.7085295e+003
3.2980058e+000 2.4946370e+003
3.3049047e+000 4.1229609e+003
3.3118038e+000 7.1944038e+003
3.3187029e+000 1.1714122e+004
3.3256021e+000 1.5338923e+004
3.3325012e+000 1.5092694e+004
3.3394003e+000 1.1227008e+004
3.3462994e+000 6.9070176e+003
3.3531983e+000 4.0318586e+003
3.3600974e+000 2.5069387e+003
3.3669965e+000 1.7313556e+003
3.3738956e+000 1.3203175e+003
3.3807948e+000 1.0810967e+003
3.3876939e+000 9.2702356e+002
3.3945928e+000 8.2453217e+002
3.4014919e+000 7.5468195e+002
3.4083910e+000 7.1011224e+002
3.4152901e+000 6.7312701e+002
3.4221892e+000 6.2927734e+002
3.4290884e+000 6.0679126e+002
3.4359872e+000 5.8445929e+002
3.4428864e+000 5.5084033e+002
3.4497855e+000 5.2990625e+002
3.4566846e+000 5.3244171e+002
3.4635837e+000 5.3299860e+002
3.4704828e+000 5.2270801e+002
3.4773817e+000 5.0838147e+002
3.4842808e+000 4.9768036e+002
3.4911799e+000 4.9974271e+002
3.4980791e+000 5.1852539e+002
3.5049782e+000 5.2486890e+002
3.5118773e+000 5.3554919e+002
3.5187764e+000 5.4363098e+002
3.5256753e+000 5.2134320e+002
3.5325744e+000 4.9386557e+002
3.5394735e+000 4.7175720e+002
3.5463727e+000 4.6334061e+002
3.5532718e+000 4.4633063e+002
3.5601709e+000 4.4021204e+002
3.5670698e+000 4.4216010e+002
3.5739689e+000 4.3208749e+002
3.5808680e+000 4.3210999e+002
3.5877671e+000 4.3717999e+002
3.5946662e+000 4.3084845e+002
3.6015654e+000 4.1379028e+002
3.6084642e+000 4.1567856e+002
3.6153634e+000 4.2414615e+002
3.6222625e+000 4.2964746e+002
3.6291616e+000 4.1986203e+002
3.6360607e+000 4.0300714e+002
3.6429598e+000 4.1156561e+002
3.6498590e+000 4.1897156e+002
3.6567578e+000 4.1506668e+002
3.6636569e+000 4.2337305e+002
3.6705561e+000 4.2956845e+002
3.6774552e+000 4.1608209e+002
3.6843543e+000 4.1159943e+002
3.6912534e+000 4.0408707e+002
3.6981523e+000 3.8742813e+002
3.7050514e+000 3.8193686e+002
3.7119505e+000 3.8675006e+002
3.7188497e+000 3.8995547e+002
3.7257488e+000 3.9189124e+002
3.7326479e+000 3.9534134e+002
3.7395468e+000 4.0249893e+002
3.7464459e+000 4.0382443e+002
3.7533450e+000 3.9881796e+002
3.7602441e+000 4.0283856e+002
3.7671432e+000 4.0544543e+002
3.7740424e+000 3.9527063e+002
3.7809412e+000 3.9659631e+002
3.7878404e+000 4.0054132e+002
3.7947395e+000 3.9123737e+002
3.8016386e+000 3.8058502e+002
3.8085377e+000 3.7388980e+002
3.8154368e+000 3.7337103e+002
3.8223360e+000 3.6008588e+002
3.8292348e+000 3.5135416e+002
3.8361340e+000 3.5958188e+002
3.8430331e+000 3.5756583e+002
3.8499322e+000 3.5956232e+002
3.8568313e+000 3.7803802e+002
3.8637304e+000 3.9012396e+002
3.8706293e+000 3.8674255e+002
3.8775284e+000 3.7771600e+002
3.8844275e+000 3.7648160e+002
3.8913267e+000 3.7692780e+002
3.8982258e+000 3.6927103e+002
3.9051249e+000 3.7007745e+002
3.9120238e+000 3.7482629e+002
3.9189229e+000 3.7230219e+002
3.9258220e+000 3.6110025e+002
3.9327211e+000 3.6490872e+002
3.9396203e+000 3.7283734e+002
3.9465194e+000 3.7933209e+002
3.9534183e+000 3.6968182e+002
3.9603174e+000 3.5532330e+002
3.9672165e+000 3.5889478e+002
3.9741156e+000 3.6407483e+002
3.9810147e+000 3.6295535e+002
3.9879138e+000 3.6387720e+002
3.9948130e+000 3.6416183e+002
4.0017118e+000 3.6089911e+002
4.0086112e+000 3.6826599e+002
4.0155101e+000 3.7570581e+002
4.0224090e+000 3.6361679e+002
4.0293083e+000 3.6003177e+002
4.0362072e+000 3.7528265e+002
4.0431066e+000 3.7368362e+002
4.0500054e+000 3.8174683e+002
4.0569048e+000 4.0386084e+002
4.0638037e+000 4.2738324e+002
4.0707026e+000 4.4587668e+002
4.0776019e+000 4.5433987e+002
4.0845008e+000 4.4404083e+002
4.0914001e+000 4.2589066e+002
4.0982990e+000 3.9662262e+002
4.1051979e+000 3.7311325e+002
4.1120973e+000 3.5790594e+002
4.1189961e+000 3.4554794e+002
4.1258955e+000 3.5435367e+002
4.1327944e+000 3.7766489e+002
4.1396937e+000 3.7425708e+002
4.1465926e+000 3.5805182e+002
4.1534915e+000 3.5078519e+002
4.1603909e+000 3.5888739e+002
4.1672897e+000 3.7242688e+002
4.1741891e+000 3.7792575e+002
4.1810880e+000 3.7338031e+002
4.1879873e+000 3.6538324e+002
4.1948862e+000 3.5872525e+002
4.2017851e+000 3.4688391e+002
4.2086844e+000 3.4881918e+002
4.2155833e+000 3.4818274e+002
4.2224827e+000 3.4055273e+002
4.2293816e+000 3.3977536e+002
4.2362804e+000 3.3322891e+002
4.2431798e+000 3.3594962e+002
4.2500787e+000 3.4658536e+002
4.2569780e+000 3.4479083e+002
4.2638769e+000 3.4267456e+002
4.2707763e+000 3.4828876e+002
4.2776752e+000 3.4845041e+002
4.2845740e+000 3.3986469e+002
4.2914734e+000 3.3093433e+002
4.2983723e+000 3.3255331e+002
4.3052716e+000 3.4089511e+002
4.3121705e+000 3.4742932e+002
4.3190699e+000 3.3570422e+002
4.3259687e+000 3.2636673e+002
4.3328676e+000 3.3228806e+002
4.3397670e+000 3.5141977e+002
4.3466659e+000 3.5683167e+002
4.3535652e+000 3.4719943e+002
4.3604641e+000 3.4054718e+002
4.3673630e+000 3.2842471e+002
4.3742623e+000 3.2503146e+002
4.3811612e+000 3.3431540e+002
4.3880606e+000 3.3462808e+002
4.3949594e+000 3.3529224e+002
4.4018588e+000 3.3313510e+002
4.4087577e+000 3.4015598e+002
4.4156566e+000 3.3703552e+002
4.4225559e+000 3.3024448e+002
4.4294548e+000 3.2974786e+002
As I suggested in the comment above, coercing the pandas Series into an ndarray will fix the problem:
mod = GaussianModel()
ydata = np.array(dataDataframe[0])
xdata = np.array(dataDataframe.index.values)
pars = mod.guess(ydata, x=xdata)
out = mod.fit(ydata, pars, x=xdata)
This example works for me:
#!/usr/bin/env python
from lmfit.models import LorentzianModel
import matplotlib.pyplot as plt
import pandas as pd
dframe = pd.read_csv('peak.csv')
model = LorentzianModel()
params = model.guess(dframe['y'], x=dframe['x'])
result = model.fit(dframe['y'], params, x=dframe['x'])
print(result.fit_report())
result.plot_fit()
plt.show()
with peaks.csv of
x,y
0.000000, 0.021654
0.200000, 0.385367
0.400000, 0.193304
0.600000, 0.103481
0.800000, 0.404041
1.000000, 0.212585
1.200000, 0.253212
1.400000, -0.037306
1.600000, 0.271415
1.800000, 0.025614
2.000000, 0.066419
2.200000, -0.034347
2.400000, 0.153702
2.600000, 0.161341
2.800000, -0.097676
3.000000, -0.061880
3.200000, 0.085341
3.400000, 0.083674
3.600000, 0.190944
3.800000, 0.222168
4.000000, 0.214417
4.200000, 0.341221
4.400000, 0.634501
4.600000, 0.302566
4.800000, 0.101096
5.000000, -0.106441
5.200000, 0.567396
5.400000, 0.531899
5.600000, 0.459800
5.800000, 0.646655
6.000000, 0.662228
6.200000, 0.820844
6.400000, 0.947696
6.600000, 1.541353
6.800000, 1.763981
7.000000, 1.846081
7.200000, 2.986333
7.400000, 3.182907
7.600000, 3.786487
7.800000, 4.822287
8.000000, 5.739122
8.200000, 6.744448
8.400000, 7.295213
8.600000, 8.737766
8.800000, 9.693782
9.000000, 9.894218
9.200000, 10.193956
9.400000, 10.091519
9.600000, 9.652392
9.800000, 8.670938
10.000000, 8.004205
10.200000, 6.773599
10.400000, 6.076502
10.600000, 5.127315
10.800000, 4.303762
11.000000, 3.426006
11.200000, 2.416431
11.400000, 2.311363
11.600000, 1.748020
11.800000, 1.135594
12.000000, 0.888514
12.200000, 1.030794
12.400000, 0.543024
12.600000, 0.767751
12.800000, 0.657551
13.000000, 0.495730
13.200000, 0.447520
13.400000, 0.173839
13.600000, 0.256758
13.800000, 0.596106
14.000000, 0.065328
14.200000, 0.197267
14.400000, 0.260038
14.600000, 0.460880
14.800000, 0.335248
15.000000, 0.295977
15.200000, -0.010228
15.400000, 0.138670
15.600000, 0.192113
15.800000, 0.304371
16.000000, 0.442517
16.200000, 0.164944
16.400000, 0.001907
16.600000, 0.207504
16.800000, 0.012640
17.000000, 0.090878
17.200000, -0.222967
17.400000, 0.391717
17.600000, 0.180295
17.800000, 0.206875
18.000000, 0.240595
18.200000, -0.037437
18.400000, 0.139918
18.600000, 0.012560
18.800000, -0.053009
19.000000, 0.226069
19.200000, 0.076879
19.400000, 0.078599
19.600000, 0.016125
19.800000, -0.071217
20.000000, -0.091474

Python Matplotlib Plot Hourly Data Saved in Defaultdict Line in File

I have a file that contains a few thousand lines of data that looks like this:
defaultdict(<type 'int'>, {'2012021310': 76422, '2012021311': 94188, '2012021323': 139363, '2012021312': 111817, '2012021307': 71316, '2012021306': 82418, '2012021305': 65217, '2012021313': 127002, '2012021314': 141099, '2012021315': 147830, '2012021316': 136330, '2012021317': 122252, '2012021318': 118619, '2012021319': 115763, '2012021322': 137658, '2012021321': 130022, '2012021320': 121393, '2012021309': 69406, '2012021308': 66833})
defaultdict(<type 'int'>, {'2012021408': 139745, '2012021409': 143658, '2012021414': 288286, '2012021418': 31216, '2012021416': 268214, '2012021400': 207878, '2012021401': 269296, '2012021402': 270258, '2012021403': 275882, '2012021404': 232521, '2012021405': 195062, '2012021406': 166669, '2012021407': 142855, '2012021417': 245582, '2012021411': 194360, '2012021413': 262078, '2012021410': 158954, '2012021415': 296457, '2012021412': 237083})
defaultdict(<type 'int'>, {'2012021523': 676350, '2012021522': 670147, '2012021521': 650984, '2012021520': 617401, '2012021501': 170448, '2012021503': 246600, '2012021502': 250013, '2012021505': 363866, '2012021504': 300809, '2012021507': 333080, '2012021506': 370454, '2012021509': 343671, '2012021508': 330452, '2012021512': 549736, '2012021513': 622690, '2012021510': 387871, '2012021511': 456171, '2012021516': 647559, '2012021517': 600969, '2012021514': 692257, '2012021515': 706377, '2012021518': 579669, '2012021519': 587969})
defaultdict(<type 'int'>, {'2012021608': 333986, '2012021609': 344126, '2012021602': 651692, '2012021603': 676458, '2012021600': 664484, '2012021601': 686408, '2012021620': 932692, '2012021621': 1065501, '2012021604': 589033, '2012021605': 465191, '2012021623': 1316907, '2012021606': 389669, '2012021607': 342613, '2012021619': 828190, '2012021618': 617836, '2012021622': 1111334, '2012021611': 467532, '2012021610': 387220, '2012021613': 634585, '2012021612': 560227, '2012021615': 718498, '2012021614': 704008, '2012021617': 606396, '2012021616': 665030})
The keys are dates/hours like '2012021310' is "02-13-2012 10am" the values are simply counts of an event that occurred.
I'm have a decent understanding for plotting with matplotlib however I'm not sure how to address the data. Is there an easy way to read in each of these lines and address them in a plot similar to the way I would use csv2rec to read in two columns? Or is there a better way?
edit:
This replaces defaultdict(, with "data ="
import fileinput
filein = 'list.txt'
for line in fileinput.input([filein]):
line = line.replace("defaultdict(<type 'int'>,", "data =")
line = line.replace(")", " ")
print line
this results in:
data = {'2012021310': 76422, '2012021311': 94188, '2012021323': 139363, '2012021312': 111817, '2012021307': 71316, '2012021306': 82418, '2012021305': 65217, '2012021313': 127002, '2012021314': 141099, '2012021315': 147830, '2012021316': 136330, '2012021317': 122252, '2012021318': 118619, '2012021319': 115763, '2012021322': 137658, '2012021321': 130022, '2012021320': 121393, '2012021309': 69406, '2012021308': 66833}
data = {'2012021408': 139745, '2012021409': 143658, '2012021414': 288286, '2012021418': 31216, '2012021416': 268214, '2012021400': 207878, '2012021401': 269296, '2012021402': 270258, '2012021403': 275882, '2012021404': 232521, '2012021405': 195062, '2012021406': 166669, '2012021407': 142855, '2012021417': 245582, '2012021411': 194360, '2012021413': 262078, '2012021410': 158954, '2012021415': 296457, '2012021412': 237083}
data = {'2012021523': 676350, '2012021522': 670147, '2012021521': 650984, '2012021520': 617401, '2012021501': 170448, '2012021503': 246600, '2012021502': 250013, '2012021505': 363866, '2012021504': 300809, '2012021507': 333080, '2012021506': 370454, '2012021509': 343671, '2012021508': 330452, '2012021512': 549736, '2012021513': 622690, '2012021510': 387871, '2012021511': 456171, '2012021516': 647559, '2012021517': 600969, '2012021514': 692257, '2012021515': 706377, '2012021518': 579669, '2012021519': 587969}
data = {'2012021608': 333986, '2012021609': 344126, '2012021602': 651692, '2012021603': 676458, '2012021600': 664484, '2012021601': 686408, '2012021620': 932692, '2012021621': 1065501, '2012021604': 589033, '2012021605': 465191, '2012021623': 1316907, '2012021606': 389669, '2012021607': 342613, '2012021619': 828190, '2012021618': 617836, '2012021622': 1111334, '2012021611': 467532, '2012021610': 387220, '2012021613': 634585, '2012021612': 560227, '2012021615': 718498, '2012021614': 704008, '2012021617': 606396, '2012021616': 665030}
I'm still not sure where to go from here
edit:
I've gotten much closer with a hybrid of everyone's suggestions:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from pylab import *
from matplotlib.mlab import csv2rec
filein = 'list.txt'
output_name = "image.png"
dicts = []
line = ""
for line in fileinput.input([filein]):
line = line.replace("defaultdict(<type 'int'>,", "data = ")
line = line.replace(")", "")
line = line.strip()
exec(line)
objects = data.items()
print objects
which returns:
[('2012021307', 71316), ('2012021306', 82418), ('2012021305', 65217), ('2012021309', 69406), ('2012021310', 76422), ('2012021311', 94188), ('2012021312', 111817), ('2012021313', 127002), ('2012021314', 141099), ('2012021315', 147830), ('2012021316', 136330), ('2012021317', 122252), ('2012021318', 118619), ('2012021319', 115763), ('2012021308', 66833), ('2012021321', 130022), ('2012021320', 121393), ('2012021323', 139363), ('2012021322', 137658)]
[('2012021408', 139745), ('2012021409', 143658), ('2012021403', 275882), ('2012021418', 31216), ('2012021400', 207878), ('2012021416', 268214), ('2012021402', 270258), ('2012021414', 288286), ('2012021404', 232521), ('2012021405', 195062), ('2012021406', 166669), ('2012021407', 142855), ('2012021417', 245582), ('2012021411', 194360), ('2012021401', 269296), ('2012021413', 262078), ('2012021410', 158954), ('2012021415', 296457), ('2012021412', 237083)]
[('2012021523', 676350), ('2012021522', 670147), ('2012021521', 650984), ('2012021520', 617401), ('2012021501', 170448), ('2012021503', 246600), ('2012021502', 250013), ('2012021505', 363866), ('2012021504', 300809), ('2012021507', 333080), ('2012021506', 370454), ('2012021509', 343671), ('2012021508', 330452), ('2012021512', 549736), ('2012021513', 622690), ('2012021510', 387871), ('2012021511', 456171), ('2012021516', 647559), ('2012021517', 600969), ('2012021514', 692257), ('2012021515', 706377), ('2012021518', 579669), ('2012021519', 587969)]
[('2012021605', 465191), ('2012021608', 333986), ('2012021609', 344126), ('2012021602', 651692), ('2012021603', 676458), ('2012021600', 664484), ('2012021601', 686408), ('2012021606', 389669), ('2012021607', 342613), ('2012021622', 1111334), ('2012021623', 1316907), ('2012021620', 932692), ('2012021621', 1065501), ('2012021619', 828190), ('2012021618', 617836), ('2012021604', 589033), ('2012021611', 467532), ('2012021610', 387220), ('2012021613', 634585), ('2012021612', 560227), ('2012021615', 718498), ('2012021614', 704008), ('2012021617', 606396), ('2012021616', 665030)]
[('2012021605', 465191), ('2012021608', 333986), ('2012021609', 344126), ('2012021602', 651692), ('2012021603', 676458), ('2012021600', 664484), ('2012021601', 686408), ('2012021606', 389669), ('2012021607', 342613), ('2012021622', 1111334), ('2012021623', 1316907), ('2012021620', 932692), ('2012021621', 1065501), ('2012021619', 828190), ('2012021618', 617836), ('2012021604', 589033), ('2012021611', 467532), ('2012021610', 387220), ('2012021613', 634585), ('2012021612', 560227), ('2012021615', 718498), ('2012021614', 704008), ('2012021617', 606396), ('2012021616', 665030)]
So I'm definitely closer:
edit:
And further yet I have:
import fileinput
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from pylab import *
from matplotlib.mlab import csv2rec
filein = 'list.txt'
output_name = "image.png"
dicts = []
times = []
values = []
line = ""
for line in fileinput.input([filein]):
line = line.replace("defaultdict(<type 'int'>,", "data = ")
line = line.replace(")", "")
line = line.strip()
exec(line)
for k in sorted(data.iterkeys()):
times.append(k)
values.append(data[k])
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(times, values)
hours = mdates.HourLocator()
fmt = mdates.DateFormatter('%Y - %M:%D:%H')
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(fmt)
fig.autofmt_xdate(bottom=0.2, rotation=90, ha='left')
ax.grid()
plt.savefig(output_name)
This gets me to the point where I've generated a plot at least. The issue now is that x access doesn't generate labels properly from the data being input. I believe that this is due to the somewhat unconventional time-stamp. Any last suggestions?
I would find a way to get that output into something more userful if at all possible.If not, you could replace this
defaultdict(<type 'int'>,
with
data =
while looping over the lines, using string.replace(), and then use the exec() function to execute the literal code that is in that line.
Try:
dicts = []
with open(filename,'r') as f:
for l in f:
exec("dicts.append(" + l[l.index('{'):l.index('}')+1] + ")")
which should read in all of the dicts and store them in the list dicts. You could then construct a list of key and value pairs with:
tsvals = []
for d in dicts:
tsvals.append(d.items())
and if you wanted them sorted you could of course sort them once you were done constructing that. I would probably save the key and value pairs once I had them like that. A csv file would probably suffice for that purpose.
The exec command should only be used on trusted data, though. It is very insecure, but if you are the one generating the data, or if you trust the person generating the data to give you only good data, then I think that is the best way to do this.

Categories

Resources