我有以下问题。我有一个数组列表。在每个阵列中都有不同数量的速度值。现在我想在一个图中绘制所有速度值,其中x轴为速度值(时间步长)的最大数量,y轴为速度值。因此,这意味着某些单元格会在特定时间移动或消失,并且数组的长度不同。有些有60个时间步,有的只有20个或2个。我怎么能用matplotlib做到这一点。
stepwiseSpeed = `[array([ 1.55858028, 1.72319652, 1.3138632 , ..., 1.21889017,
0.89490572, 0.57537662]), array([ 1.91378539, 1.94151339, 2.32322109, ..., 1.67023367,
1.82005941, 2.11918622]), array([ 1.33111955, 1.32013105, 1.58057118, ..., 1.39378854,
1.57944246, 0.9993698 ]), array([ 0.49445374, 0.55514075, 0.67257435, ..., 1.14848269,
1.04420137, 1.07907484]), array([ 2.54790115, 2.35476761, 2.2023148 , ..., 0.9137895 ,
0.66586954, 0.8247339 ]), array([ 1.54280143, 1.32324648, 1.50265473, ..., 0.76789729,
1.35688697, 2.33849316]), array([ 0.16252154, 0.04299128, 0.29318296, ..., 0.38305124,
0.39047567, 0.46256891]), array([ 0.25298221, 0.41818806, 0.33350037, ..., 0.65907682,
0.83928452, 0.39371468]), array([ 1.09880219, 1.01478976, 0.91687649, ..., 1.02647455,
1.24970487, 2.01485763]), array([ 0.09464143, 1.61802874, 1.49291569, ..., 1.76469325,
1.14202627, 0.71533366]), array([ 1.60550031, 1.73888988, 2.21143692, 2.3515708 , 2.18094274,
2.09998345, 2.21021413, 1.76824263]), array([ 1.34700937, 1.42637381, 1.59221112, ..., 1.30645599,
1.54239716, 1.35439913]), array([ 1.89335819, 2.08723573, 1.57333285, ..., 2.11308856,
1.47246027, 1.15664612]), array([ 1.12155082, 0.92655882, 1.09706882, ..., 0.77595183,
1.63657821, 0.75386007]), array([ 2.49641528, 2.49097636, 1.96445323, ..., 2.6539554 ,
2.81124034, 2.14660301]), array([ 0.77921708, 0.9108684 , 1.518581 , ..., 1.38129088,
1.29851386, 1.73863948]), array([ 2.00074417, 1.9842234 , 2.00441045, ..., 2.17050622,
1.88565884, 1.03864166]), array([ 3.53476407, 2.73784518, 2.25230393, ..., 0.7960468 ,
1.77199217, 2.19730864]), array([ 1.53449153, 2.57789691, 1.45867414, ..., 2.22159633,
1.59388394, 1.22748238]), array([ 0.36409477, 2.35263363, 2.12942533, 2.34612894, 2.47864605]), array([ 2.17469504, 1.64095125, 2.16791075, ..., 2.07901635,
1.84478427, 2.53679483]), array([ 1.56738652, 1.56266927, 0.67997004, ..., 1.78691361,
2.09958669, 1.59975561]), array([ 1.20872247, 1.6410732 , 1.89742411]), array([ 0.20453362, 0.11991351, 0.12212289, ..., 0.08566359,
0.05703069, 0.29739704]), array([ 1.75904022, 2.09773408, 1.79617517, ..., 0.82287378,
1.36801069, 1.51320562]), array([ 0.05261179, 0.46579663]), array([ 0.23269777, 1.90598072, 1.56422417, ..., 0.3680197 ,
0.357694 , 0.06367496]), array([ 1.01395513, 0.98519541, 1.07660601, ..., 0.9802617 ,
0.76729655, 0.96600945]), array([ 1.33202637, 0.7318615 , 1.1559628 , ..., 1.03510893,
1.26631947, 1.48354786]), array([ 1.0866606 , 0.77068882, 0.88053223, ..., 1.90948219,
2.21011476, 1.97420224]), array([ 2.12812975, 2.01956097, 2.31859661, ..., 1.94602891,
1.85868509, 1.74235258]), array([ 1.76357854, 1.43027873, 1.48372378, ..., 1.8284423 ,
2.08478086, 1.2062364 ]), array([ 0.30522 , 0.07227206, 0.10364 , ..., 0.20251049,
0.17750282, 0.20282135]), array([ 2.69737326, 1.79450223, 1.81635191, ..., 2.31043205,
1.76532405, 2.16768921]), array([ 0.26849069, 0.18535506, 0.04669047, ..., 0.57618508,
0.75687466, 0.10831667]), array([ 2.18284476, 3.10184336, 2.73087605, ..., 2.10712339,
2.0009146 , 1.67278458]), array([ 2.6001824 , 2.96293136, 3.03568485, ..., 2.12006226,
2.64044409, 2.46736889]), array([ 0.9525294 , 0.81262322, 1.24229546, ..., 0.9767913 ,
0.51150611, 0.79479589]), array([ 0.25192161, 0.40685378, 0.22003295, ..., 1.37511136,
0.93462987, 0.45534438]), array([ 1.9968668 , 1.8816708 , 1.52276344, ..., 0.45460422,
0.8757444 , 0.60405981]), array([ 0.86648024, 0.56151425, 0.29774234, ..., 1.12381682,
1.2909269 , 2.86136895]), array([ 0.62895548, 0.83833913, 0.86012688, ..., 0.95821462,
0.78016617, 0.77060042]), array([ 0.43987726, 0.05630275, 0.18917783, ..., 0.075316 ,
0.52368311, 0.12119509]), array([ 0.32087575, 0.06232576]), array([ 1.13415222, 1.21691341, 1.70172596, ..., 0.98508794,
1.18055855, 2.08319478]), array([ 0.30171054, 0.10151108, 0.13138588, ..., 0.28987109,
0.41692325, 0.70072124]), array([ 1.23198265, 0.98577102, 0.93972403, ..., 0.8060411 ,
0.57367805, 0.55542101]), array([ 1.9453846 , 1.93433774, 1.95535348, ..., 1.71128607,
1.69986066, 1.99904802]), array([ 0.41382907, 0.13898291, 0.0505099 , ..., 0.20115914,
0.1050488 , 0.18561385]), array([ 0.38006907, 0.30888914, 0.25646247, ..., 0.05544367,
0.08395237, 0.65774862]), array([ 0.2860354 , 0.11174301, 0.1454132 , ..., 0.10700117,
0.2100976 , 0.20984756]), array([ 1.53951681, 1.2273341 , 1.24489809, ..., 1.47022532,
1.50214189, 1.63154788]), array([ 0.99671862, 1.36467505, 1.17222694, ..., 1.60129674,
1.05191171, 1.29765192]), array([ 0.15404951, 0.10419813, 0.0985 , ..., 0.14107179,
0.14647867, 0.04180012]), array([ 0.28887065, 0.1084493 , 0.11624543, ..., 0.45650192,
0.56707142, 0.28541417]), array([ 2.26928414, 1.25146075, 1.47439386, ..., 2.60592695,
1.27440437, 1.87131672]), array([ 2.92374199, 1.46373563, 1.80281981, ..., 2.19150006,
2.25192146, 1.90295139]), array([ 2.18285919, 1.68016011, 1.40383136, ..., 0.42678625,
1.25455271, 0.38832074]), array([ 3.02971764, 2.74982045, 1.8573224 , ..., 2.40567215,
2.23071659, 1.80791683]), array([ 1.48257757, 1.29211184, 1.48204698, ..., 2.66644656,
2.10154289, 1.17909648]), array([ 2.03090356, 2.20390024, 1.81261034, ..., 1.81366018,
2.36686396, 3.11423377]), array([ 1.76671793, 1.82343261, 1.41152152, ..., 2.21197344,
1.61988155, 1.75330146]), array([ 1.12100602, 1.39713609, 1.05732966, ..., 0.81416122,
1.79136994, 1.20562867]), array([ 0.12583819, 0.18826179, 0.02466779, ..., 0.22483883,
0.22450891, 0.03037269]), array([ 1.87311038, 1.90238436, 1.58469973, ..., 1.9883275 ,
1.73602074, 1.56803141]), array([ 1.86455745, 1.81486398, 1.48632407, ..., 1.36969212,
1.00423565, 1.0501563 ]), array([ 2.73055786, 1.83777563, 1.85286137, ..., 2.24384815,
2.29956893, 2.31718471]), array([ 1.89764341, 1.70214872, 1.84366598, 1.69067331, 1.63166556,
1.57210718, 2.0997838 , 0.5 ]), array([ 2.88394868, 1.76794683, 1.84730412, ..., 1.67066791,
2.20931828, 3.14142364]), array([ 2.21248983, 2.04950945, 1.99745063, ..., 1.74589211,
1.37430391, 0.65894044]), array([ 2.44579256, 3. , 2.90203726, ..., 2.22868913,
2.30628576, 2.55041418]), array([ 1.68212306, 1.4199279 , 1.20669155, ..., 2.49021455,
2.35037364, 2.39554091]), array([ 1.06665189, 1.11918508, 0.7972208 , ..., 1.60282813,
1.58208533, 2.19066457]), array([ 0.78931109, 1.13041054, 1.16357477, ..., 2.66146163,
2.43427053, 2.23752419]), array([ 2.19094643, 1.76558574, 1.37872767, 0.82824211, 2.18727027,
2.62784099]), array([ 0.10083774, 0.43023395, 0.19314502, ..., 0.55084526,
0.18818209, 0.33118575]), array([ 1.22523885, 1.1780226 , 1.16143898, ..., 1.37054305,
1.27952735, 1.66479338]), array([ 0.1667708 , 0.19700063, 0.06909595, ..., 0.11294357,
0.14055693, 0.19833368]), array([ 0.27619649, 0.09141116, 0.07320007, ..., 0.49362992,
0.1314962 , 0.23830076]), array([ 1.67102162, 1.62518745, 1.98942636, ..., 1.45618723,
0.337855 , 1.66846674]), array([ 0.60795991, 0.44838739, 0.38203796, ..., 0.46585942,
0.30358401, 0.73156357]), array([ 1.07226862, 0.78236069, 0.73008647, ..., 1.18012425,
2.05000006, 1.35999559]), array([ 0.49515351, 0.34681443, 0.57254039, ..., 1.12471385,
0.88768252, 0.37862415]), array([ 0.43722134, 1.31378784, 1.28410767, ..., 0.22951961,
0.1011002 , 0.13063786]), array([ 1.58778919, 0.91317975, 1.3874045 , ..., 0.67955371,
1.56955766, 1.84941207]), array([ 1.38600081, 0.71548463, 1.1139072 , ..., 1.7361188 ,
1.09014047, 1.67402337]), array([ 2.33964084, 2.2881121 , 2.64705969, ..., 2.54892414,
2.3285146 , 3.00123183]), array([ 2.93343459, 2.78420698, 3.37859161, ..., 2.13336835,
1.99106655, 2.74309697]), array([ 2.03878156, 1.97883665, 1.779593 , ..., 1.69101892,
1.77543185, 1.60652552]), array([ 1.84313381, 2.33591765, 2.26208267, ..., 1.83826991,
1.81539837, 1.66015007]), array([ 0.32584812, 0.12905909, 0.10983624, ..., 0.08502353,
0.02263846, 0.16272216]), array([ 2.08540224, 2.52326103, 1.78199502, ..., 1.43025566,
2.10360892, 2.50847209]), array([ 1.35194989, 1.00768299, 1.06636731, ..., 1.07219984,
2.88258604, 2.13120324]), array([ 0.97252044, 1.74217981, 1.53105299, ..., 2.04182596,
2.23229221, 2.309091 ]), array([ 1.41432245, 1.86468771, 1.77704396, ..., 0.95134549,
1.31210156, 0.69083464]), array([ 2.24700801, 2.08037316, 2.02301044, ..., 1.45537504,
2.23908045, 2.63455883]), array([ 0.82991641, 1.86141431, 2.34076531, ..., 1.10209584,
0.92174847, 0.97488012]), array([ 0.38514316, 0.21352752, 0.25347436, ..., 0.07790058,
0.16285346, 0.1315038 ]), array([ 2.02986761, 0.82133809, 1.17532176, ..., 1.28708828,
1.62466558, 2.66293832]), array([ 0.8934702 , 0.85802346, 0.89695541, ..., 0.08343411,
0.16163926, 0.21664545]), array([ 0.33850923, 0.24557687, 0.16454331, ..., 0.22479435,
0.57760475, 0.41688518]), array([ 1.39075708, 2.9092652 , 0.99997012, ..., 2.42601984,
2.08349058, 0.59230482]), array([ 1.97287994, 2.01780977, 2. , ..., 0. ,
0.5 , 0. ]), array([ 0.32086641, 1.29471425, 0.04457858, ..., 0. ,
0. , 0.5 ]), array([ 2.43895316, 2.83801762, 2.62735456, ..., 1.96420684,
2.3198847 , 2.05312749]), array([ 3.02936536, 3.22485054, 2.52072336, ..., 0.76459957,
0.41013565, 0.45389261]), array([ 0.29225545, 0.17980615, 1.03240556, 1.21714112, 1.18821421,
0.52382917, 0.82153591, 1.41904061, 2.27450665]), array([ 1.2816858 , 1.25700358, 1.20278115, ..., 1.61915263,
1.77364441, 1.30971085]), array([ 1.97751422, 1.95173058, 1.54928661, ..., 1.98322496,
2.04750488, 2.2632575 ]), array([ 0.63925621, 0.66576892, 0.97573818, ..., 0.12602579,
0.2588035 , 0.31642535]), array([ 0.40626223, 0.1778595 , 0.03687818, ..., 0.13887134,
0.31905642, 0.4443152 ]), array([ 0.34145644, 0.1360046 , 0.04825971, ..., 0.03860052,
0.23750474, 0.17262677]), array([ 0.18390283, 1.48037166, 1.23468427, 1.91344885, 2.13213813,
1.17380769, 0.28618613, 1.14347333]), array([ 1.22660283, 1.54239465, 1.60848912, ..., 1.05759692,
1.14263828, 0.64083091]), array([ 2.44351223, 2.59941864, 2.38900785, ..., 1.92910893,
2.1107687 , 2.58347581]), array([ 2.05453803, 2.15442811, 1.75392645, ..., 1.27033165,
2.41534604, 2.00029848]), array([ 0.78288521, 1.48917536, 1.16919673, ..., 1.30538433,
1.33542587, 1.96086575]), array([ 1.68339575, 1.85524459, 2.07925479, ..., 1.26744033,
1.1795496 , 2.08592246]), array([ 0.21437001, 0.20903827, 0.08782084, ..., 0.18694719,
0.15453964, 0.28100712]), array([ 2.02139958, 1.63307333, 1.38518167, ..., 2.25808353,
1.90529453, 1.34233844]), array([ 1.92463516, 1.36573286, 1.54907004, ..., 1.65766228,
1.73290421, 1.35040781]), array([ 0.13159502, 0.14026582, 0.10248902, ..., 0.1916155 ,
0.60245539, 0.11750106]), array([ 0.63557376, 0.24203306, 0.34270286]), array([ 0.18857691, 0.21780553, 0.15810914, ..., 0.05126646,
0.13080998, 0.1913276 ]), array([ 1.22705674]), array([ 0.13507128]), array([ 0.2354708]), array([ 1.57633531, 1.55151837, 1.31369422, ..., 1.91784319,
2.21293391, 1.97695631]), array([ 0. , 1. , 2.09862604, ..., 0. ,
0. , 0.5 ]), array([ 0.95880042, 0.66884116, 1.06916042, ..., 2.20919182,
1.6887334 , 2.08884777]), array([ 0.21483017, 0.14633694, 0.07961156, ..., 0.50707421,
0.26750187, 0.28801389]), array([ 1.53528532, 1.30901499]), array([ 0.04164733]), array([ 0.31047423]), array([ 0.19588581, 0.67608062]), array([ 2.43942872, 1.38864862, 1.69930611, ..., 2.72903078,
2.74475104, 3.03296245]), array([ 0.13336041]), array([ 1.4435943 , 1.33855332, 1.26706748, ..., 2.52732828,
1.83266643, 1.19031361]), array([ 0.71127087, 0.54525063, 0.11205467, 0.37567439, 0.54685739,
0.15082855]), array([ 0.34405232, 0.10654225, 0.22720145, ..., 1.48355317,
1.2728799 , 1.53241125]), array([ 2.]), array([ 0.59890442]), array([ 1.26222106, 1.8219377 ]), array([ 0.46585942, 0.76667888, 0.56226373, ..., 0.07724312,
0.28786499, 0.13753272]), array([ 0.1091444 , 0.16341665, 0.3989693 , 0.30698901, 0.24913551,
0.48188432, 0.51878922, 0.08511903]), array([ 1.87770978]), array([ 2.]), array([ 0.5, 0.5, 0. , ..., 0. , 0.5, 0. ]), array([ 1.12430067, 1.20262733, 1.41128461, ..., 1.41489505,
1.09233843, 0.56896507]), array([ 0.4415654 , 0.21175753, 0.36739624]), array([ 0.17096345, 0.33056391, 0.53811244]), array([ 0.08941616, 0.13057756, 0.08549415, 1.23515394, 0.27620735]), array([ 1.69062185, 0.55601371, 1.40494991, ..., 2.48426247,
2.41843612, 2.37813845]), array([ 0.46693602, 0.02700463, 0.69161839]), array([ 0.98284803, 0.20435875, 0.56879258, ..., 0.45329019,
0.38247778, 0.58895883]), array([ 1.17094086, 1.86998135, 0.40516324, ..., 2.04802661,
1.68366861, 2.22164556]), array([ 0.35565468]), array([ 0.11051357, 1.94093206, 0.42017437, 1.93846873, 0.11490866]), array([ 0.23832121, 0.16985656, 0.14502155, ..., 0.09553141,
0.74882909, 0.25167936]), array([ 0.24344661, 0.71604469, 0.57635167, ..., 2.1796533 ,
3.2387026 , 2.82588928]), array([ 1.19543841, 1.98482902, 0.80832806, ..., 2.25231686,
1.6028353 , 1.72162627]), array([ 1.5, 1. , 2. , 1. , 1. , 0.5, 1. ]), array([ 0.19445951, 0.25506568, 0.12495199, ..., 0.2677989 ,
0.19560803, 0.39318475]), array([ 0.26885916, 0.51650024, 0.10744766, ..., 0.52832992,
0.67342854, 0.64594292]), array([ 0.43311113, 0.36389731, 0.49640835, ..., 0.27952325,
0.22430615, 0.35013033]), array([ 0.88554757, 0.18689302, 0.14018559, ..., 0.16794121,
0.37962514, 0.57132128]), array([ 1.58438356, 1.51923805, 1.36669108, ..., 2.04041969,
2.24300986, 1.63742633]), array([ 0. , 0. , 0. , ..., 0.5, 0.5, 1. ]), array([ 1.54588041, 1.13103061, 0.35044258, ..., 2.1321865 ,
2.13657729, 2.38603541]), array([ 0.22650883, 0.50450223, 0.31563428, ..., 0.27592073,
0.23753947, 0.22973028]), array([ 0.22088741, 0.40644342, 0.39419824, 0.33696773, 0.16667633,
0.34639031, 0.15257949, 0.19111842]), array([ 0.0981644 , 0.2241456 , 0.23967582, ..., 0.10651878,
0.2224455 , 0.34074367]), array([ 1.55110235, 1.0887975 , 0.86550924, ..., 1.76440479,
1.48901083, 1.73694595]), array([ 0.38426195, 0.42262158, 0.31236557, ..., 1.87444025,
2.11489107, 2.39972566]), array([ 1.37938619, 0.66078627, 0.62203798, ..., 2.16635806,
1.03723973, 0.8222416 ]), array([ 2.06387603, 0.13650092, 1.96042833, ..., 0.05960914,
0.06356493, 1.379999 ]), array([ 0.27693411, 0.09910222, 0.34492354, ..., 0.16399543,
0.09848858, 0.17686789]), array([ 0. , 0. , 0. , ..., 0.5, 0.5, 3. ]), array([ 1.87005842]), array([ 0.5 , 0. , 0.5 , ..., 0.83683272,
1.49475358, 0. ]), array([ 0.18063015, 0.44328997, 1.01612598, ..., 1.57924484,
0.6307337 , 0.56330498]), array([ 1.47563757, 1.07001168, 0.55430136, 0.41983925, 0.23619113,
0.40562174, 1.41572075, 1.14993152, 1.18746842, 1.68457532]), array([ 0.28687323, 0.29052108, 0.21541588, 0.10160832, 0.06171912,
0.09023996, 0.1082601 ]), array([ 1. , 1. , 1. , 1.5]), array([ 0.40021619, 0.69313942]), array([ 1.51482309, 2.01856118, 1.96068413, 0.44666123]), array([ 0.1333276 , 0.0885 , 0.15088158]), array([ 0.23098322, 0.3935775 , 0.50150025]), array([ 1.37265746, 0.73863421]), array([ 0.95313929, 1.74625385])]`
第一种方法,这给了我不幸的每个阵列一个情节:
for i in stepwiseSpeed:
timesteps = np.arange(0,len(i),1)
plt.figure(figsize=(20,5))
plt.plot(timesteps,i, '-o')
plt.xlabel('time')
plt.ylabel('speed')
plt.title('speed at timepoint t')
plt.grid(True)
plt.show()
答案 0 :(得分:3)
您应该将图形和轴初始化与循环分开。使用matplotlib的面向对象语法通常也更清楚。
fig = plt.figure(figsize=(20,5))
ax = fig.add_subplot(111)
for i in stepwiseSpeed:
timesteps = np.arange(0,len(i),1)
ax.plot(timesteps,i, '-o')
ax.set_xlabel('time')
ax.set_ylabel('speed')
ax.set_title('speed at timepoint t')
ax.grid(True)
fig.show()
请注意,不再需要使用ax.hold(True)
,因为它现在是默认行为。
答案 1 :(得分:2)
问题在于代码中的第3行。您忘记提供图形名称,因此var client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:51411/api");
var inventories = new []{ new Inventory(1, "foo"), new Inventory(2, "bar") };
var response =await client.PostAsJsonAsync("mobileAPI/inventory?tenantId=42", inventories);
每次都会创建一个新图形。
实际上,您可以从代码中删除它。时间步也似乎是不必要的,但它取决于你。
plt.figure()
参考文献:Python: Plot multiple graphs on the same figure
P.S。如果你想要一些复杂的数据图,Kaggle Datascience书籍提供了许多创造性的方法来绘制多维数据:
https://www.kaggle.com/battuzz94/a-guide-to-titanic-challenge