将图例添加到子图 matplotlib
adding legend to a subplot matplotlib
我有以下数据框(出于安全原因进行了简化):
sc =[[-2.3626099835364505, 2.34577702722125, 'A'], [-2.0075911304000322, 2.3676265784278554, 'A'], [-2.3309505510650244, 2.1812783083023524, 'A'], [-1.3748126608960896, 2.3605935362307924, 'A'], [-0.9918749681446175, 2.6765972587442204, 'A'], [-1.6960233844360806, 2.6256112751574507, 'A'], [-1.8926044442515384, 2.411841433479522, 'A'], [-2.509005088348112, 2.2914597716406937, 'A'], [-2.783376467296819, 2.2617877355046185, 'A'], [-2.3701794705898243, 2.31330534299281, 'A'], [-3.012547659111412, 2.2641638897323975, 'A'], [-1.6504249339379615, 2.3510487897219785, 'A'], [-1.6916463998349789, 2.1375490151536938, 'A'], [-1.3921736690141497, 2.3796366201969534, 'A'], [-1.0454151584166034, 2.4333124976786764, 'A'], [-2.2636703663095994, 1.9815839702975608, 'A'], [-2.307772192142976, 2.2627171528903007, 'A'], [-2.8410883083948275, 2.0652038990612898, 'A'], [-0.8487687115058202, 2.3340248207393577, 'A'], [-1.7027184356080671, 2.4904590741004564, 'A'], [-0.620252351439105, 2.8791509397053066, 'A'], [-4.768367973794037, 0.3140632669071711, 'A'], [-3.9811196194627336, 1.0617567150372336, 'A'], [-4.612631199107817, 0.9271772602763483, 'A'], [-3.604095826144104, 1.4996614123589516, 'A'], [-4.134541094094984, 1.146587562693302, 'A'], [-3.770069188034946, 0.9564678612963834, 'A'], [-4.76057382315683, 1.173989580775522, 'A'], [-4.490301442943034, 5.217214316331541, 'A'], [-4.74475825871379, 4.087505269840572, 'A'], [-4.462228289636768, 4.524645688991954, 'A'], [-3.818689567770223, 5.094498457908875, 'A'], [-4.612820931189715, 4.492748648280875, 'A'], [-5.346543617692401, 3.376496558926699, 'A'], [-5.38640878798411, 3.608189391965011, 'A'], [-4.521895718169117, 3.0017548909807146, 'A'], [-4.9591446736323554, 2.5644386133570847, 'A'], [-4.707996645744058, 3.7093033543312837, 'A'], [-4.212647650130087, 2.519714565249968, 'A'], [-6.015685902047046, 3.6326027578785935, 'A'], [-5.596960825485442, 2.42980262296789, 'A'], [-6.201489937139624, 3.092070633568435, 'A'], [-4.4735030575623576, 2.551101033090788, 'A'], [-4.658310868941498, 3.59440964215695, 'A'], [-4.494828187857855, 4.05701500246433, 'A'], [-4.6420229008119565, 3.623467004350195, 'A'], [-5.562926805563235, 3.7119825931298465, 'A'], [-5.987141920211359, 3.7710548667058013, 'A'], [-5.425433396191337, 3.5029144756897654, 'A'], [-3.6411553756602086, 3.1795219723105146, 'A'], [0.3273741943867551, 2.4898018907534882, 'A'], [-0.6929075677283425, 1.7916292351541905, 'A'], [0.33218476067494507, 2.431549550089504, 'A'], [-0.7030829308313081, 2.3118463182935014, 'A'], [0.2497831787915912, 2.5191983836285385, 'A'], [-0.681866305604014, 2.409510655047636, 'A'], [-1.0728495420083026, 1.9809446248917062, 'A'], [1.1643660786495489, 2.653989285246831, 'A'], [0.37615279039385524, 1.804726366809622, 'A'], [0.3354221361125493, 2.2179541198527057, 'A'], [-4.457123936208896, -0.18888919277928537, 'A'], [-1.7130604238839626, 2.342422940212129, 'A'], [-0.9180206838147861, 1.63986629431432, 'A'], [-3.674801491925776, 1.1899019356553466, 'A'], [-1.2849964201714463, 2.02465392731463, 'A'], [-0.9304741284172378, 2.450127371718659, 'A'], [-2.1647717713876884, 2.1302740481657776, 'A'], [1.0443244200242778, 2.332950643278259, 'A'], [0.40378820764970097, 1.862877265860551, 'A'], [-0.4331860240362087, 2.5214165050780784, 'A'], [0.6132280005091323, 3.7004224835070167, 'A'], [-2.377714854931382, 2.3464960167710807, 'A'], [2.062530177161494, 1.9866212479544667, 'A'], [0.7474123985273501, 2.0731907391268924, 'A'], [0.9731230037506488, 2.020579950344668, 'A'], [2.79771407258263, 3.0616630033643486, 'A'], [2.750299239115155, 3.328013929968941, 'A'], [0.4275880778208092, 3.1562503106543676, 'A'], [-2.5960518584965113, -0.1416583441223469, 'A'], [0.7591003004122073, 2.6523365108005055, 'A'], [0.10467977064290586, 2.915301092911593, 'A'], [0.6062247997556168, 2.8574431938499902, 'A'], [1.1691568853222447, -0.7139516397518147, 'A'], [-1.545533735921738, -2.2849359142867867, 'A'], [-2.6269598223798716, -3.209441422083886, 'A'], [0.8325455696281434, -1.0540560217173005, 'A'], [-2.6995517244632206, -2.270708433830261, 'A'], [0.49937989873784516, -0.6397415772413222, 'A'], [0.9440295048481291, -0.2261064583616643, 'A'], [1.3929711948474577, -0.15277386010693864, 'A'], [0.04399970219599474, -0.37627178457366034, 'A'], [0.07488917031372055, -1.114672681506685, 'A'], [2.1820611328736037, 0.14785721565620763, 'A'], [1.3001512034690565, -0.28852209350574365, 'A'], [1.5261115021081828, -0.10881677494091889, 'A'], [1.4810533140160056, -0.06812580546735332, 'A'], [1.8072200098906914, -0.0632392116683808, 'A'], [3.362047323876361, -3.296836167200378, 'A'], [5.6207422482846505, -1.9319120111843096, 'A'], [5.480783518557285, -1.623523495041347, 'A'], [5.5348224292554855, -1.0727055315396326, 'A'], [2.8288842811365567, -3.7160151335374048, 'A'], [2.982683750390456, -3.61380103927461, 'A'], [7.633552684220728, -0.33690289938538065, 'A'], [5.574104878165989, -3.7226970375908897, 'A'], [3.783910366654537, -3.7500631560837348, 'A'], [1.5888484115673116, -5.034604943265851, 'A'], [5.531922473920801, -3.6350084821166573, 'A'], [3.1363077363529057, -2.6133630152413843, 'A'], [4.197417415452695, -4.42891262481606, 'A'], [5.592843534224109, -3.278634481076688, 'A'], [2.4665506471899366, -4.696164193489069, 'A'], [3.7047101960502022, -4.093351588552793, 'A'], [0.8183947663478567, -5.161341552210629, 'A'], [1.3574602881245819, -5.094481302191971, 'A'], [2.507885923255698, -4.180846441393332, 'A'], [2.121122079400425, -5.18777542265966, 'A'], [3.621124971643953, -2.646458565417131, 'A'], [1.9177190278048863, -4.67597017430872, 'A'], [1.2757196360353733, -5.40491802698459, 'A'], [1.2669195007682503, -4.674169843274119, 'A'], [1.8236324790149379, -4.941408607517866, 'A'], [2.712425987808226, -4.33462572223536, 'A'], [2.3043894347632703, -4.7865456465225895, 'A'], [2.1135402683811115, -4.885023615368305, 'A'], [1.7005272782654184, -4.992913343278192, 'A'], [1.402622876942818, -4.65332998723223, 'A'], [0.9287969655889979, -4.953156549142575, 'A'], [5.108201814736482, -3.001878514756507, 'A'], [6.034702854319849, -1.7918485096810877, 'A'], [2.8869632276748733, -4.178178641357886, 'A'], [3.3976746386609933, -4.552394817004267, 'A'], [3.881358543814943, -3.934225265754376, 'A'], [2.597623581917941, -4.064085703956816, 'A'], [3.3715458764154524, -4.052691936175539, 'A'], [4.474209674735886, -2.8276313973507317, 'A'], [4.287364442048595, -3.7051581135000187, 'A'], [8.04389538587432, -1.0032810518213304, 'A'], [3.6039907579820474, -3.863367219021197, 'A'], [6.473682374021665, -1.4494066326372066, 'A'], [5.682143517776669, -3.045524684758874, 'A'], [8.185159919305889, 0.039871586503246174, 'A'], [7.915415340954955, -1.535598927111547, 'A'], [3.955086848352283, -2.4471975226915275, 'A'], [0.861023389393213, -3.7184300384474573, 'A'], [3.024567418280866, -3.2115442099224096, 'A'], [2.7388273494191324, -2.5589622958305314, 'A'], [3.4860726549454735, -3.534136630126466, 'A'], [2.4989520859643677, -3.4006815087754507, 'A'], [2.601772423319878, -3.745337589966074, 'A'], [6.508563625369925, -2.0215737979074717, 'A'], [5.895716083559873, -2.7628206915594844, 'A'], [1.4482993694768818, -4.200012807784804, 'A'], [2.1616807402730998, -3.141512064893326, 'A'], [-0.7397764369259493, -2.5562296043245163, 'A'], [0.63321265036917, -1.6712365233808966, 'A'], [-1.5648476689300983, -2.822859156227075, 'A'], [-0.5145340735137061, -1.8029926035161625, 'A'], [0.42747273077081277, -1.4366980478987865, 'A'], [-0.07523963714552295, -1.9673982029982466, 'A'], [1.3631736770640486, -1.1004848018528206, 'A'], [1.404242013471034, -0.954464995548393, 'A'], [0.6054479883941668, -1.5260339899991695, 'A'], [0.06714028387238516, -0.6345778374005442, 'A'], [0.810405698433202, -1.88068177675896, 'A'], [3.342226005619375, 0.45780263742607513, 'A'], [1.7290485700032596, -0.43002768234224126, 'A'], [4.3933267270984375, 1.4016075753697619, 'A'], [6.40471501769487, 2.704289469913052, 'A'], [-0.1655378590332831, -1.4542516022586822, 'A'], [0.5348107546556085, -1.4661724011651345, 'A'], [0.2894088439349217, -1.639613646447954, 'A'], [0.23822329153615668, -1.6035544628126008, 'A'], [-0.12222413008414099, -1.454626832648946, 'A'], [-1.553902826442119, -3.0172433258285554, 'A'], [2.223132290570635, -1.4792800823795835, 'A'], [-1.1240899367246486, -1.7734011218722856, 'A'], [0.3561879913042483, -0.5117559695145236, 'A'], [1.2594905620038495, -1.1379849493799084, 'A'], [1.1424566827089289, -1.464404363277175, 'A'], [0.2007647412988282, -0.607156931598518, 'A'], [5.382312864955485, 1.970044889718104, 'A'], [7.038341797588195, 2.917180933511296, 'A'], [-3.3676716272300564, -2.992872157047521, 'A'], [7.709317692714649, 2.6959180155668414, 'A'], [0.8033362592663974, 1.4677122720672067, 'A'], [-2.464019003797383, 0.10648372040620427, 'A'], [-1.597195713302199, 0.46832540580466525, 'A'], [0.36763829068828396, 1.1241080875902596, 'A'], [-0.8645274135936158, 0.8978902314301787, 'A'], [0.739233262038936, 1.6481214368006585, 'A'], [-1.894986968547285, 0.43104633669762216, 'A'], [-3.2372168733631534, -0.4025421939674454, 'A'], [0.22693555862934567, 0.9635470288780031, 'A'], [-3.1023134302615456, -0.3996518643135968, 'A'], [0.7513774887336002, 4.709903992393884, 'A'], [-0.7102573948312342, 3.8387015118799668, 'A'], [3.8168927193814612, 6.424746377455561, 'A'], [0.3330234554007937, 4.218470783386504, 'A'], [1.6757996806904645, 5.019289677746951, 'A'], [2.354428385050995, 5.091705948582642, 'A'], [4.333225636771599, 6.435838699348781, 'A'], [0.3267280510216682, 2.7407361110740425, 'A'], [-0.3829301761705263, 2.6587395804103817, 'A'], [-0.7003870824533822, 2.5066266709364307, 'A'], [1.6960646552222445, 4.5303719028106455, 'A'], [3.7134926652180615, 6.5669100932566815, 'A'], [2.8291985064812435, 6.023152487406758, 'A'], [-0.32410999008155184, 2.050573931091246, 'A'], [2.430893648992376, 6.54738374182827, 'A'], [-0.47691122195751523, 3.5889855814774667, 'A'], [0.33132115302171117, 3.161775411120895, 'A'], [1.8460295493457366, 4.44407395088134, 'A'], [2.2439896803478354, 6.316212555098901, 'A'], [0.43680912037739206, 3.035895698919401, 'A'], [1.0255274990784435, 3.635885326805688, 'A'], [2.8308500925429616, 6.405727504174064, 'A'], [2.073311152028087, 5.054319056417755, 'A'], [-4.217514163412541, 0.029574552792442695, 'A'], [2.1333948863253864, 1.1754269692740436, 'A'], [-0.11124605330690973, 0.958762261893075, 'A'], [-2.2126542850749664, 0.8035456175446051, 'A'], [-1.7372052758015184, 1.1162926749428836, 'A'], [-4.112392190384804, 0.21233706471156877, 'A'], [-3.3272244199510017, -0.4965018068613245, 'A'], [-1.111512564052498, 2.47086411178649, 'A'], [-2.747799595410357, -0.23445222160899207, 'A'], [-4.951649448896327, -0.5725904111185567, 'A'], [5.617194999608431, 2.0396177832606956, 'A'], [-0.8280156885294722, 1.6044481442649634, 'A'], [-4.467513748989247, 0.5272476039141418, 'A'], [-3.209501677874977, 0.1117093733276799, 'A'], [-3.742030668151948, -0.6031884009008012, 'A'], [-1.0508620239462523, 0.30705702917462174, 'A'], [-2.7543344337665534, -0.04064828144228885, 'A'], [-2.1270273568732296, 0.08232035233221198, 'A'], [-2.5292663975889225, -0.49979915507182365, 'A'], [-3.8599714090211736, -0.8362040426356832, 'A'], [-4.038774335599983, 0.49042502040724495, 'A'], [-4.068398706434263, 0.5184877089578044, 'A'], [-4.43470858889789, -0.6687887512732299, 'A'], [-4.413753004199418, -0.10935468519079712, 'A'], [-5.28465302528379, 0.3785138830727877, 'A'], [-0.6233545998163098, 0.0401006360726568, 'A'], [-2.1809732903572057, -0.3506481082953535, 'A'], [-2.087517768965848, -0.3251062638148907, 'A'], [-2.168320449137227, -0.6413841413008763, 'A'], [-2.931962454836154, 0.1903106526888545, 'A'], [-3.2571646323104737, 0.06094034961570407, 'A'], [-7.007905646920691, 0.6169498966780026, 'A'], [-3.4011636114044284, -0.25594503409386615, 'A'], [-6.09052011502505, 1.2468208202325137, 'A'], [-4.354422796623975, 0.9122977637464591, 'A'], [-2.215773712137751, 0.560354461438785, 'A'], [-4.289978579145782, 1.0154666998295072, 'A'], [1.0757854272511396, 3.2652578836756168, 'A'], [-2.370493026260289, 0.5448972500584975, 'A'], [-3.3935287828062672, 1.8774901865947686, 'A'], [-3.272560751534143, 0.03568518621266006, 'A'], [0.26912712252323256, -0.23408979857252465, 'A'], [0.7208271534610242, 0.3137690393250428, 'A'], [1.4302640268654832, 0.6196107832242831, 'A'], [-1.0307861726207008, -0.14564858948790732, 'A'], [-1.4304378916279872, 0.04522102038714572, 'A'], [-0.4803722824829699, 0.013530204836671469, 'A'], [-1.0459320079117131, -0.5833719514040971, 'A'], [0.10369200685802318, 0.3687827217305342, 'A'], [-3.002979406220161, -1.4642952691930187, 'A'], [-2.2194867176023148, -1.8968123061768272, 'A'], [-1.5151481738403245, -1.0765672116576797, 'A'], [-1.5067233751938909, -1.5310545276237124, 'A'], [-1.9028345658710761, -0.4852946041464928, 'A'], [-0.8189273132414655, -0.12739152115967434, 'A'], [-2.2426616244404274, -0.3507124729148306, 'A'], [-1.6245017806958262, -0.6639006437186832, 'A'], [-0.44994314611849845, -0.9954633806173914, 'A'], [-4.500819416338982, -4.358186759045402, 'A'], [-0.9777793950542033, -0.7022563584022304, 'A'], [-0.4993001239908364, 0.3410536273012378, 'A'], [-0.3745392703393849, 0.3835496148323278, 'A'], [-1.2054293017018338, -1.3491568074882974, 'A'], [-4.078565722914769, -0.13184399294888582, 'A'], [-3.5149425082772856, -3.3627831204211027, 'A'], [-5.843872877181417, -1.2168720429385023, 'A'], [-5.228244858746559, -3.989500031190506, 'A'], [-4.760705470337702, -1.6933614335126472, 'A'], [-3.475687194825685, -1.7388911605557615, 'A'], [-4.85298262823147, -1.7949241686589545, 'A'], [-4.313577960950585, -4.404512308230435, 'A'], [-6.120407217378619, -0.6332857092441777, 'A'], [-3.4907530512021996, -2.2615558077588855, 'A'], [-3.414497966454189, -2.05959120858767, 'A'], [-3.457698491003345, -2.3482136859611122, 'A'], [-3.587687555519662, -3.0341684440917005, 'A'], [-4.653561449747282, -2.8268880453806466, 'A'], [-4.385456405994636, -1.6534957959274856, 'A'], [-4.9384968200080825, -2.130359711316302, 'A'], [-4.25249737245042, -0.8223979555458059, 'A'], [-1.5132643942330672, -1.5240782481425468, 'A'], [-5.046476028533958, -0.8832215846030057, 'A'], [-5.6251501455190835, -1.0778948913589546, 'A'], [-4.492491735957441, -3.4333130137530343, 'A'], [-3.290882681568372, -3.5768812958621443, 'A'], [-5.628869325112712, -1.3566874836226144, 'A'], [-5.592319879604197, -1.708607070517318, 'A'], [-4.1658071412546045, -1.778873159299542, 'A'], [-4.414072425861396, -1.5947209647948313, 'A'], [2.3109023136356295, 0.8211499218942658, 'A'], [2.199451487250583, 1.8066445074980533, 'A'], [2.497877767687725, 0.5693911444201016, 'A'], [-5.343091928141313, -1.426069064026246, 'A'], [-1.1463104295680866, 0.38331741388547963, 'A'], [5.678626724940067, 3.5505510246945278, 'A'], [-1.4442378516099255, -0.3357781986280093, 'A'], [-5.331052994537495, -2.2221631207593004, 'A'], [0.13023950647675, 0.6256371449856193, 'A'], [-2.1223922847992798, -1.075232627250957, 'A'], [-2.3693748519627524, -0.34747384051738084, 'A'], [5.177097737839847, 3.238299885742315, 'A'], [4.213657649963197, 3.861490840930303, 'A'], [-3.2847140220658804, -1.0937085664063997, 'A'], [-2.0821915040772385, -2.684409554939887, 'A'], [6.68785670194751, 0.2854001898705518, 'A'], [6.502253718037854, 1.168786187832156, 'A'], [7.269968670569903, 0.9460241598010397, 'A'], [7.549527153307221, 1.3876368872909408, 'A'], [14.107533264848001, 5.714209564908541, 'A'], [10.615334117027976, 3.265821195301891, 'A'], [6.493698784293358, 1.2871521268423693, 'A'], [9.331510887236258, 2.176685772666222, 'A'], [6.421272901198505, 2.0182540213177167, 'A'], [6.272223418246192, 2.0098510018496594, 'A'], [6.487062535736635, -0.980358434822404, 'A'], [6.314266956807722, -1.1730807808523072, 'A'], [5.6913554422038395, -1.7097622739455103, 'A'], [5.116318868567786, -1.9849708365311889, 'A'], [6.3149700533143065, -0.8871599246640899, 'A'], [8.191042754677907, 0.7246938783904523, 'A'], [5.341042036799151, -2.2002462716679534, 'A'], [5.125152410210292, -1.753466577931911, 'A'], [7.448023296739685, 1.6440990478539996, 'A'], [15.058787932711937, 3.6139600303262394, 'A'], [2.104457425485387, -2.0342870432199534, 'A'], [0.7689748471815588, -3.358084799188546, 'A'], [2.3826291119064877, -1.4482501637884104, 'A'], [-1.6993688540312635, -3.4707689821696017, 'A'], [-0.5793217141496829, -3.4565326003805996, 'A'], [0.6217245227612146, -3.510370601826024, 'A'], [-0.3853674703117758, -3.743980500761143, 'A'], [8.170808394405572, 2.963806154835631, 'A'], [0.9756283146415439, -3.56688266340445, 'A'], [-3.0666238580109093, -2.4219781898936987, 'A'], [0.11061479796156488, -4.3606680381682486, 'A'], [-4.464117286209441, -0.33320894526424766, 'A'], [-3.0687359814855695, 0.20461880428289933, 'A'], [-4.875953641707615, -1.3704282334193627, 'A'], [-5.773434373365418, 1.5895403531674377, 'A'], [-2.456190988154502, 0.11266106436956183, 'A'], [-4.8645264934179995, -1.4986756831637795, 'A'], [-2.8048818667943802, 0.4374711572428851, 'A'], [-5.195602709773577, -1.1963673709313916, 'A'], [-5.634035578858843, -2.2856689565263397, 'A'], [-3.8671470568419064, -1.186517036048942, 'A'], [-5.2049085326633024, -1.4794175690783782, 'A'], [-6.183786712264101, -2.0527254731695463, 'A'], [-3.729514576745839, 0.11488460522224504, 'A'], [-4.887014896848255, -0.1066470406470426, 'A'], [-5.774442899295047, 0.6475167762275699, 'A'], [-4.78854045032299, -0.30245239067093016, 'A'], [-6.238834376462753, 0.7648597634968872, 'A'], [-4.127281943507791, 0.4476893444175745, 'A'], [-5.838183331444605, -1.4104101632899917, 'A'], [-4.199817994495076, 0.5699772649725662, 'A'], [-3.6329130499762945, 0.9714249393743809, 'A'], [-2.009363346652952, 1.2176642282920127, 'A'], [-3.0298324183510417, 1.008375386685997, 'A'], [2.039180579155086, 0.9432381278764693, 'A'], [-1.2553806116616193, 0.5919661194389959, 'A'], [0.4807721035887499, 1.532011860348611, 'A'], [-7.524525261251259, -0.2676016323877108, 'A'], [-7.445640931374195, 0.3584606018758793, 'A'], [-3.9379519967071897, 0.28509061299561095, 'A'], [-3.831729443727788, 1.2188310377255511, 'A'], [-5.702975652273074, -1.1892327625842443, 'A'], [-4.439948690317305, -0.6088240907977401, 'A'], [-5.134355589937063, -0.4387085034684176, 'A'], [-0.02417400344377166, 1.7371379442466228, 'C'], [-0.6169717100956201, -0.05470546053768472, 'C'], [1.3430601284846464, -1.4464247606324383, 'C'], [0.722639390413349, -1.3801432562066855, 'C'], [0.39787849549810056, -2.4219694757483885, 'C'], [4.909528215889961, 2.0304933297843837, 'C'], [3.4118989013962016, 0.39767905531674236, 'C'], [0.6504014510267492, -1.6412643081044251, 'C'], [3.575936483209145, 0.8886797800183428, 'C'], [4.050441633621285, -1.4111297986303057, 'C'], [3.524835391957593, -0.7962598344052034, 'C'], [-0.25695358649833727, -4.792777584314668, 'C'], [2.2712661247595887, -0.8960707283107315, 'C'], [1.2402850213944012, -3.6348074322476056, 'C'], [-0.3704320551931987, -1.3966574621948589, 'C'], [10.59369072217267, 5.063370086272745, 'C'], [7.388162919594694, 3.2530347772626085, 'C'], [5.074167549425396, -2.616255955565353, 'D'], [6.015804612050362, -2.428070159933407, 'D'], [8.886710081651788, 0.06251626043708561, 'D'], [8.04345129509809, -0.42855094118916953, 'D'], [7.5867398899867995, -0.35816408207813816, 'D'], [4.745999193692063, -3.01272153238224, 'D'], [17.26178347959192, 3.6904816061952608, 'D'], [11.012544268630574, 1.2796512711456722, 'D'], [11.952051954648535, 2.274670680774901, 'D'], [4.57247777825687, -2.740539006829948, 'D'], [4.64712122875631, -2.9541760750503148, 'D'], [9.822119025017109, 1.114596244101624, 'D'], [5.10813665885816, -2.5117620420661098, 'D'], [5.241282203885775, -2.785619655460366, 'D'], [4.348640406623504, -3.028668176390842, 'D'], [10.291138396478695, 1.4519674624623602, 'D'], [8.425360839100868, 0.12244076193242436, 'D'], [5.9291348966828705, -2.8002566618852027, 'D'], [12.285336218974248, 2.2614951522964537, 'D'], [10.123016060319232, 0.8422847481086354, 'D'], [-6.362058536370231, -1.776888721966689, 'B'], [-3.607790414898749, -0.11405382585885869, 'B'], [-3.5863297337978453, -1.003289609185522, 'B'], [-4.054339453868017, -0.6375200043500148, 'B'], [-4.240053513108218, -1.1755276292568613, 'B'], [-5.134777911527315, -1.4695033193550058, 'B'], [-1.4889239040130964, 1.4976310317516441, 'B'], [-6.754828146543384, -1.1786478007750532, 'B'], [-5.393355342242125, -0.7712159748923132, 'B'], [-2.6193983264691334, -1.7530225717366794, 'B'], [-4.337600871033667, -1.29871274675849, 'B'], [-3.8129587283903636, 0.3320505375336237, 'B'], [0.996577117919612, 4.011035199145417, 'B'], [1.2454893051447347, 2.4064910426011714, 'B'], [-2.28920849335583, 0.8671759601357621, 'B'], [-2.654467193555008, 0.15172163371743422, 'B'], [-3.2066143269301755, -1.0718186423966996, 'B'], [-2.1391157194198955, -0.35966062691412143, 'B'], [-2.0561793218125906, -0.4878697696741051, 'B'], [-0.8070328764507083, 0.7139337454107234, 'B'], [-0.6968579945821873, 1.1434692996521818, 'B'], [-2.3526738583605917, -0.20957185223025143, 'B'], [-0.7585178997937219, -0.904787509339181, 'B'], [3.2457790366169634, 1.650743098214606, 'B'], [0.3284646238578842, 0.24071040358540596, 'B'], [-1.4957934160157627, -1.2669962800304935, 'B'], [-1.5382403984088373, 0.0239536561604769, 'B'], [-1.2594390015524441, -0.47009338598493816, 'B'], [2.5791370303408563, 1.9935734173147306, 'B']]
列名是:
columns = ['PC1', 'PC2', 'Type']
我在 matplotlib 中绘制坐标轴,所以第一行有两张图,第二行有一张。我想在第一行的其中一张图上画一个图例。
该图的简化代码为:
x1 = sc['PC1']
y1 = sc['PC2']
groups = ('A', 'B', 'C', 'D')
colors1 = {'A':'tab:red', 'B':'tab:blue', 'C':'tab:green', 'D':'tab:purple'}
f = plt.figure(figsize=(10,10))
ax3 = f.add_subplot(111)
ax3.scatter(x1, y1, color=sc['Type'].map(colors1) ,label=[d for d in groups])
ax3.set(xlabel = "$t_1$",
ylabel = "$t_2$")
ax3.axhline(y=0, xmin=-10, xmax=20, color='k', linestyle='--', linewidth=0.75)
ax3.axvline(x=0, ymin=-10, ymax=20, color='k', linestyle='--', linewidth=0.75)
ax3.legend()
plt.show()
我想创建一个图例,其中彩色点与 A、B、C 和 D 匹配。
我看到了一些类似问题的例子,但我无法将解决方案应用到我的案例中。
有什么建议吗?
谢谢!
一种方法是一次绘制一种类型:
groups = ('A', 'B', 'C', 'D')
colors1 = {'A':'tab:red', 'B':'tab:blue', 'C':'tab:green', 'D':'tab:purple'}
f = plt.figure(figsize=(10,10))
ax3 = f.add_subplot(111)
for label in groups:
x1 = sc.loc[sc['Type'].eq(label), 'PC1']
y1 = sc.loc[sc['Type'].eq(label), 'PC2']
ax3.scatter(x1, y1, color=colors1[label], label=label)
ax3.set(xlabel = "$t_1$",
ylabel = "$t_2$")
ax3.axhline(y=0, xmin=-10, xmax=20, color='k', linestyle='--', linewidth=0.75)
ax3.axvline(x=0, ymin=-10, ymax=20, color='k', linestyle='--', linewidth=0.75)
ax3.legend()
plt.show()
我有以下数据框(出于安全原因进行了简化):
sc =[[-2.3626099835364505, 2.34577702722125, 'A'], [-2.0075911304000322, 2.3676265784278554, 'A'], [-2.3309505510650244, 2.1812783083023524, 'A'], [-1.3748126608960896, 2.3605935362307924, 'A'], [-0.9918749681446175, 2.6765972587442204, 'A'], [-1.6960233844360806, 2.6256112751574507, 'A'], [-1.8926044442515384, 2.411841433479522, 'A'], [-2.509005088348112, 2.2914597716406937, 'A'], [-2.783376467296819, 2.2617877355046185, 'A'], [-2.3701794705898243, 2.31330534299281, 'A'], [-3.012547659111412, 2.2641638897323975, 'A'], [-1.6504249339379615, 2.3510487897219785, 'A'], [-1.6916463998349789, 2.1375490151536938, 'A'], [-1.3921736690141497, 2.3796366201969534, 'A'], [-1.0454151584166034, 2.4333124976786764, 'A'], [-2.2636703663095994, 1.9815839702975608, 'A'], [-2.307772192142976, 2.2627171528903007, 'A'], [-2.8410883083948275, 2.0652038990612898, 'A'], [-0.8487687115058202, 2.3340248207393577, 'A'], [-1.7027184356080671, 2.4904590741004564, 'A'], [-0.620252351439105, 2.8791509397053066, 'A'], [-4.768367973794037, 0.3140632669071711, 'A'], [-3.9811196194627336, 1.0617567150372336, 'A'], [-4.612631199107817, 0.9271772602763483, 'A'], [-3.604095826144104, 1.4996614123589516, 'A'], [-4.134541094094984, 1.146587562693302, 'A'], [-3.770069188034946, 0.9564678612963834, 'A'], [-4.76057382315683, 1.173989580775522, 'A'], [-4.490301442943034, 5.217214316331541, 'A'], [-4.74475825871379, 4.087505269840572, 'A'], [-4.462228289636768, 4.524645688991954, 'A'], [-3.818689567770223, 5.094498457908875, 'A'], [-4.612820931189715, 4.492748648280875, 'A'], [-5.346543617692401, 3.376496558926699, 'A'], [-5.38640878798411, 3.608189391965011, 'A'], [-4.521895718169117, 3.0017548909807146, 'A'], [-4.9591446736323554, 2.5644386133570847, 'A'], [-4.707996645744058, 3.7093033543312837, 'A'], [-4.212647650130087, 2.519714565249968, 'A'], [-6.015685902047046, 3.6326027578785935, 'A'], [-5.596960825485442, 2.42980262296789, 'A'], [-6.201489937139624, 3.092070633568435, 'A'], [-4.4735030575623576, 2.551101033090788, 'A'], [-4.658310868941498, 3.59440964215695, 'A'], [-4.494828187857855, 4.05701500246433, 'A'], [-4.6420229008119565, 3.623467004350195, 'A'], [-5.562926805563235, 3.7119825931298465, 'A'], [-5.987141920211359, 3.7710548667058013, 'A'], [-5.425433396191337, 3.5029144756897654, 'A'], [-3.6411553756602086, 3.1795219723105146, 'A'], [0.3273741943867551, 2.4898018907534882, 'A'], [-0.6929075677283425, 1.7916292351541905, 'A'], [0.33218476067494507, 2.431549550089504, 'A'], [-0.7030829308313081, 2.3118463182935014, 'A'], [0.2497831787915912, 2.5191983836285385, 'A'], [-0.681866305604014, 2.409510655047636, 'A'], [-1.0728495420083026, 1.9809446248917062, 'A'], [1.1643660786495489, 2.653989285246831, 'A'], [0.37615279039385524, 1.804726366809622, 'A'], [0.3354221361125493, 2.2179541198527057, 'A'], [-4.457123936208896, -0.18888919277928537, 'A'], [-1.7130604238839626, 2.342422940212129, 'A'], [-0.9180206838147861, 1.63986629431432, 'A'], [-3.674801491925776, 1.1899019356553466, 'A'], [-1.2849964201714463, 2.02465392731463, 'A'], [-0.9304741284172378, 2.450127371718659, 'A'], [-2.1647717713876884, 2.1302740481657776, 'A'], [1.0443244200242778, 2.332950643278259, 'A'], [0.40378820764970097, 1.862877265860551, 'A'], [-0.4331860240362087, 2.5214165050780784, 'A'], [0.6132280005091323, 3.7004224835070167, 'A'], [-2.377714854931382, 2.3464960167710807, 'A'], [2.062530177161494, 1.9866212479544667, 'A'], [0.7474123985273501, 2.0731907391268924, 'A'], [0.9731230037506488, 2.020579950344668, 'A'], [2.79771407258263, 3.0616630033643486, 'A'], [2.750299239115155, 3.328013929968941, 'A'], [0.4275880778208092, 3.1562503106543676, 'A'], [-2.5960518584965113, -0.1416583441223469, 'A'], [0.7591003004122073, 2.6523365108005055, 'A'], [0.10467977064290586, 2.915301092911593, 'A'], [0.6062247997556168, 2.8574431938499902, 'A'], [1.1691568853222447, -0.7139516397518147, 'A'], [-1.545533735921738, -2.2849359142867867, 'A'], [-2.6269598223798716, -3.209441422083886, 'A'], [0.8325455696281434, -1.0540560217173005, 'A'], [-2.6995517244632206, -2.270708433830261, 'A'], [0.49937989873784516, -0.6397415772413222, 'A'], [0.9440295048481291, -0.2261064583616643, 'A'], [1.3929711948474577, -0.15277386010693864, 'A'], [0.04399970219599474, -0.37627178457366034, 'A'], [0.07488917031372055, -1.114672681506685, 'A'], [2.1820611328736037, 0.14785721565620763, 'A'], [1.3001512034690565, -0.28852209350574365, 'A'], [1.5261115021081828, -0.10881677494091889, 'A'], [1.4810533140160056, -0.06812580546735332, 'A'], [1.8072200098906914, -0.0632392116683808, 'A'], [3.362047323876361, -3.296836167200378, 'A'], [5.6207422482846505, -1.9319120111843096, 'A'], [5.480783518557285, -1.623523495041347, 'A'], [5.5348224292554855, -1.0727055315396326, 'A'], [2.8288842811365567, -3.7160151335374048, 'A'], [2.982683750390456, -3.61380103927461, 'A'], [7.633552684220728, -0.33690289938538065, 'A'], [5.574104878165989, -3.7226970375908897, 'A'], [3.783910366654537, -3.7500631560837348, 'A'], [1.5888484115673116, -5.034604943265851, 'A'], [5.531922473920801, -3.6350084821166573, 'A'], [3.1363077363529057, -2.6133630152413843, 'A'], [4.197417415452695, -4.42891262481606, 'A'], [5.592843534224109, -3.278634481076688, 'A'], [2.4665506471899366, -4.696164193489069, 'A'], [3.7047101960502022, -4.093351588552793, 'A'], [0.8183947663478567, -5.161341552210629, 'A'], [1.3574602881245819, -5.094481302191971, 'A'], [2.507885923255698, -4.180846441393332, 'A'], [2.121122079400425, -5.18777542265966, 'A'], [3.621124971643953, -2.646458565417131, 'A'], [1.9177190278048863, -4.67597017430872, 'A'], [1.2757196360353733, -5.40491802698459, 'A'], [1.2669195007682503, -4.674169843274119, 'A'], [1.8236324790149379, -4.941408607517866, 'A'], [2.712425987808226, -4.33462572223536, 'A'], [2.3043894347632703, -4.7865456465225895, 'A'], [2.1135402683811115, -4.885023615368305, 'A'], [1.7005272782654184, -4.992913343278192, 'A'], [1.402622876942818, -4.65332998723223, 'A'], [0.9287969655889979, -4.953156549142575, 'A'], [5.108201814736482, -3.001878514756507, 'A'], [6.034702854319849, -1.7918485096810877, 'A'], [2.8869632276748733, -4.178178641357886, 'A'], [3.3976746386609933, -4.552394817004267, 'A'], [3.881358543814943, -3.934225265754376, 'A'], [2.597623581917941, -4.064085703956816, 'A'], [3.3715458764154524, -4.052691936175539, 'A'], [4.474209674735886, -2.8276313973507317, 'A'], [4.287364442048595, -3.7051581135000187, 'A'], [8.04389538587432, -1.0032810518213304, 'A'], [3.6039907579820474, -3.863367219021197, 'A'], [6.473682374021665, -1.4494066326372066, 'A'], [5.682143517776669, -3.045524684758874, 'A'], [8.185159919305889, 0.039871586503246174, 'A'], [7.915415340954955, -1.535598927111547, 'A'], [3.955086848352283, -2.4471975226915275, 'A'], [0.861023389393213, -3.7184300384474573, 'A'], [3.024567418280866, -3.2115442099224096, 'A'], [2.7388273494191324, -2.5589622958305314, 'A'], [3.4860726549454735, -3.534136630126466, 'A'], [2.4989520859643677, -3.4006815087754507, 'A'], [2.601772423319878, -3.745337589966074, 'A'], [6.508563625369925, -2.0215737979074717, 'A'], [5.895716083559873, -2.7628206915594844, 'A'], [1.4482993694768818, -4.200012807784804, 'A'], [2.1616807402730998, -3.141512064893326, 'A'], [-0.7397764369259493, -2.5562296043245163, 'A'], [0.63321265036917, -1.6712365233808966, 'A'], [-1.5648476689300983, -2.822859156227075, 'A'], [-0.5145340735137061, -1.8029926035161625, 'A'], [0.42747273077081277, -1.4366980478987865, 'A'], [-0.07523963714552295, -1.9673982029982466, 'A'], [1.3631736770640486, -1.1004848018528206, 'A'], [1.404242013471034, -0.954464995548393, 'A'], [0.6054479883941668, -1.5260339899991695, 'A'], [0.06714028387238516, -0.6345778374005442, 'A'], [0.810405698433202, -1.88068177675896, 'A'], [3.342226005619375, 0.45780263742607513, 'A'], [1.7290485700032596, -0.43002768234224126, 'A'], [4.3933267270984375, 1.4016075753697619, 'A'], [6.40471501769487, 2.704289469913052, 'A'], [-0.1655378590332831, -1.4542516022586822, 'A'], [0.5348107546556085, -1.4661724011651345, 'A'], [0.2894088439349217, -1.639613646447954, 'A'], [0.23822329153615668, -1.6035544628126008, 'A'], [-0.12222413008414099, -1.454626832648946, 'A'], [-1.553902826442119, -3.0172433258285554, 'A'], [2.223132290570635, -1.4792800823795835, 'A'], [-1.1240899367246486, -1.7734011218722856, 'A'], [0.3561879913042483, -0.5117559695145236, 'A'], [1.2594905620038495, -1.1379849493799084, 'A'], [1.1424566827089289, -1.464404363277175, 'A'], [0.2007647412988282, -0.607156931598518, 'A'], [5.382312864955485, 1.970044889718104, 'A'], [7.038341797588195, 2.917180933511296, 'A'], [-3.3676716272300564, -2.992872157047521, 'A'], [7.709317692714649, 2.6959180155668414, 'A'], [0.8033362592663974, 1.4677122720672067, 'A'], [-2.464019003797383, 0.10648372040620427, 'A'], [-1.597195713302199, 0.46832540580466525, 'A'], [0.36763829068828396, 1.1241080875902596, 'A'], [-0.8645274135936158, 0.8978902314301787, 'A'], [0.739233262038936, 1.6481214368006585, 'A'], [-1.894986968547285, 0.43104633669762216, 'A'], [-3.2372168733631534, -0.4025421939674454, 'A'], [0.22693555862934567, 0.9635470288780031, 'A'], [-3.1023134302615456, -0.3996518643135968, 'A'], [0.7513774887336002, 4.709903992393884, 'A'], [-0.7102573948312342, 3.8387015118799668, 'A'], [3.8168927193814612, 6.424746377455561, 'A'], [0.3330234554007937, 4.218470783386504, 'A'], [1.6757996806904645, 5.019289677746951, 'A'], [2.354428385050995, 5.091705948582642, 'A'], [4.333225636771599, 6.435838699348781, 'A'], [0.3267280510216682, 2.7407361110740425, 'A'], [-0.3829301761705263, 2.6587395804103817, 'A'], [-0.7003870824533822, 2.5066266709364307, 'A'], [1.6960646552222445, 4.5303719028106455, 'A'], [3.7134926652180615, 6.5669100932566815, 'A'], [2.8291985064812435, 6.023152487406758, 'A'], [-0.32410999008155184, 2.050573931091246, 'A'], [2.430893648992376, 6.54738374182827, 'A'], [-0.47691122195751523, 3.5889855814774667, 'A'], [0.33132115302171117, 3.161775411120895, 'A'], [1.8460295493457366, 4.44407395088134, 'A'], [2.2439896803478354, 6.316212555098901, 'A'], [0.43680912037739206, 3.035895698919401, 'A'], [1.0255274990784435, 3.635885326805688, 'A'], [2.8308500925429616, 6.405727504174064, 'A'], [2.073311152028087, 5.054319056417755, 'A'], [-4.217514163412541, 0.029574552792442695, 'A'], [2.1333948863253864, 1.1754269692740436, 'A'], [-0.11124605330690973, 0.958762261893075, 'A'], [-2.2126542850749664, 0.8035456175446051, 'A'], [-1.7372052758015184, 1.1162926749428836, 'A'], [-4.112392190384804, 0.21233706471156877, 'A'], [-3.3272244199510017, -0.4965018068613245, 'A'], [-1.111512564052498, 2.47086411178649, 'A'], [-2.747799595410357, -0.23445222160899207, 'A'], [-4.951649448896327, -0.5725904111185567, 'A'], [5.617194999608431, 2.0396177832606956, 'A'], [-0.8280156885294722, 1.6044481442649634, 'A'], [-4.467513748989247, 0.5272476039141418, 'A'], [-3.209501677874977, 0.1117093733276799, 'A'], [-3.742030668151948, -0.6031884009008012, 'A'], [-1.0508620239462523, 0.30705702917462174, 'A'], [-2.7543344337665534, -0.04064828144228885, 'A'], [-2.1270273568732296, 0.08232035233221198, 'A'], [-2.5292663975889225, -0.49979915507182365, 'A'], [-3.8599714090211736, -0.8362040426356832, 'A'], [-4.038774335599983, 0.49042502040724495, 'A'], [-4.068398706434263, 0.5184877089578044, 'A'], [-4.43470858889789, -0.6687887512732299, 'A'], [-4.413753004199418, -0.10935468519079712, 'A'], [-5.28465302528379, 0.3785138830727877, 'A'], [-0.6233545998163098, 0.0401006360726568, 'A'], [-2.1809732903572057, -0.3506481082953535, 'A'], [-2.087517768965848, -0.3251062638148907, 'A'], [-2.168320449137227, -0.6413841413008763, 'A'], [-2.931962454836154, 0.1903106526888545, 'A'], [-3.2571646323104737, 0.06094034961570407, 'A'], [-7.007905646920691, 0.6169498966780026, 'A'], [-3.4011636114044284, -0.25594503409386615, 'A'], [-6.09052011502505, 1.2468208202325137, 'A'], [-4.354422796623975, 0.9122977637464591, 'A'], [-2.215773712137751, 0.560354461438785, 'A'], [-4.289978579145782, 1.0154666998295072, 'A'], [1.0757854272511396, 3.2652578836756168, 'A'], [-2.370493026260289, 0.5448972500584975, 'A'], [-3.3935287828062672, 1.8774901865947686, 'A'], [-3.272560751534143, 0.03568518621266006, 'A'], [0.26912712252323256, -0.23408979857252465, 'A'], [0.7208271534610242, 0.3137690393250428, 'A'], [1.4302640268654832, 0.6196107832242831, 'A'], [-1.0307861726207008, -0.14564858948790732, 'A'], [-1.4304378916279872, 0.04522102038714572, 'A'], [-0.4803722824829699, 0.013530204836671469, 'A'], [-1.0459320079117131, -0.5833719514040971, 'A'], [0.10369200685802318, 0.3687827217305342, 'A'], [-3.002979406220161, -1.4642952691930187, 'A'], [-2.2194867176023148, -1.8968123061768272, 'A'], [-1.5151481738403245, -1.0765672116576797, 'A'], [-1.5067233751938909, -1.5310545276237124, 'A'], [-1.9028345658710761, -0.4852946041464928, 'A'], [-0.8189273132414655, -0.12739152115967434, 'A'], [-2.2426616244404274, -0.3507124729148306, 'A'], [-1.6245017806958262, -0.6639006437186832, 'A'], [-0.44994314611849845, -0.9954633806173914, 'A'], [-4.500819416338982, -4.358186759045402, 'A'], [-0.9777793950542033, -0.7022563584022304, 'A'], [-0.4993001239908364, 0.3410536273012378, 'A'], [-0.3745392703393849, 0.3835496148323278, 'A'], [-1.2054293017018338, -1.3491568074882974, 'A'], [-4.078565722914769, -0.13184399294888582, 'A'], [-3.5149425082772856, -3.3627831204211027, 'A'], [-5.843872877181417, -1.2168720429385023, 'A'], [-5.228244858746559, -3.989500031190506, 'A'], [-4.760705470337702, -1.6933614335126472, 'A'], [-3.475687194825685, -1.7388911605557615, 'A'], [-4.85298262823147, -1.7949241686589545, 'A'], [-4.313577960950585, -4.404512308230435, 'A'], [-6.120407217378619, -0.6332857092441777, 'A'], [-3.4907530512021996, -2.2615558077588855, 'A'], [-3.414497966454189, -2.05959120858767, 'A'], [-3.457698491003345, -2.3482136859611122, 'A'], [-3.587687555519662, -3.0341684440917005, 'A'], [-4.653561449747282, -2.8268880453806466, 'A'], [-4.385456405994636, -1.6534957959274856, 'A'], [-4.9384968200080825, -2.130359711316302, 'A'], [-4.25249737245042, -0.8223979555458059, 'A'], [-1.5132643942330672, -1.5240782481425468, 'A'], [-5.046476028533958, -0.8832215846030057, 'A'], [-5.6251501455190835, -1.0778948913589546, 'A'], [-4.492491735957441, -3.4333130137530343, 'A'], [-3.290882681568372, -3.5768812958621443, 'A'], [-5.628869325112712, -1.3566874836226144, 'A'], [-5.592319879604197, -1.708607070517318, 'A'], [-4.1658071412546045, -1.778873159299542, 'A'], [-4.414072425861396, -1.5947209647948313, 'A'], [2.3109023136356295, 0.8211499218942658, 'A'], [2.199451487250583, 1.8066445074980533, 'A'], [2.497877767687725, 0.5693911444201016, 'A'], [-5.343091928141313, -1.426069064026246, 'A'], [-1.1463104295680866, 0.38331741388547963, 'A'], [5.678626724940067, 3.5505510246945278, 'A'], [-1.4442378516099255, -0.3357781986280093, 'A'], [-5.331052994537495, -2.2221631207593004, 'A'], [0.13023950647675, 0.6256371449856193, 'A'], [-2.1223922847992798, -1.075232627250957, 'A'], [-2.3693748519627524, -0.34747384051738084, 'A'], [5.177097737839847, 3.238299885742315, 'A'], [4.213657649963197, 3.861490840930303, 'A'], [-3.2847140220658804, -1.0937085664063997, 'A'], [-2.0821915040772385, -2.684409554939887, 'A'], [6.68785670194751, 0.2854001898705518, 'A'], [6.502253718037854, 1.168786187832156, 'A'], [7.269968670569903, 0.9460241598010397, 'A'], [7.549527153307221, 1.3876368872909408, 'A'], [14.107533264848001, 5.714209564908541, 'A'], [10.615334117027976, 3.265821195301891, 'A'], [6.493698784293358, 1.2871521268423693, 'A'], [9.331510887236258, 2.176685772666222, 'A'], [6.421272901198505, 2.0182540213177167, 'A'], [6.272223418246192, 2.0098510018496594, 'A'], [6.487062535736635, -0.980358434822404, 'A'], [6.314266956807722, -1.1730807808523072, 'A'], [5.6913554422038395, -1.7097622739455103, 'A'], [5.116318868567786, -1.9849708365311889, 'A'], [6.3149700533143065, -0.8871599246640899, 'A'], [8.191042754677907, 0.7246938783904523, 'A'], [5.341042036799151, -2.2002462716679534, 'A'], [5.125152410210292, -1.753466577931911, 'A'], [7.448023296739685, 1.6440990478539996, 'A'], [15.058787932711937, 3.6139600303262394, 'A'], [2.104457425485387, -2.0342870432199534, 'A'], [0.7689748471815588, -3.358084799188546, 'A'], [2.3826291119064877, -1.4482501637884104, 'A'], [-1.6993688540312635, -3.4707689821696017, 'A'], [-0.5793217141496829, -3.4565326003805996, 'A'], [0.6217245227612146, -3.510370601826024, 'A'], [-0.3853674703117758, -3.743980500761143, 'A'], [8.170808394405572, 2.963806154835631, 'A'], [0.9756283146415439, -3.56688266340445, 'A'], [-3.0666238580109093, -2.4219781898936987, 'A'], [0.11061479796156488, -4.3606680381682486, 'A'], [-4.464117286209441, -0.33320894526424766, 'A'], [-3.0687359814855695, 0.20461880428289933, 'A'], [-4.875953641707615, -1.3704282334193627, 'A'], [-5.773434373365418, 1.5895403531674377, 'A'], [-2.456190988154502, 0.11266106436956183, 'A'], [-4.8645264934179995, -1.4986756831637795, 'A'], [-2.8048818667943802, 0.4374711572428851, 'A'], [-5.195602709773577, -1.1963673709313916, 'A'], [-5.634035578858843, -2.2856689565263397, 'A'], [-3.8671470568419064, -1.186517036048942, 'A'], [-5.2049085326633024, -1.4794175690783782, 'A'], [-6.183786712264101, -2.0527254731695463, 'A'], [-3.729514576745839, 0.11488460522224504, 'A'], [-4.887014896848255, -0.1066470406470426, 'A'], [-5.774442899295047, 0.6475167762275699, 'A'], [-4.78854045032299, -0.30245239067093016, 'A'], [-6.238834376462753, 0.7648597634968872, 'A'], [-4.127281943507791, 0.4476893444175745, 'A'], [-5.838183331444605, -1.4104101632899917, 'A'], [-4.199817994495076, 0.5699772649725662, 'A'], [-3.6329130499762945, 0.9714249393743809, 'A'], [-2.009363346652952, 1.2176642282920127, 'A'], [-3.0298324183510417, 1.008375386685997, 'A'], [2.039180579155086, 0.9432381278764693, 'A'], [-1.2553806116616193, 0.5919661194389959, 'A'], [0.4807721035887499, 1.532011860348611, 'A'], [-7.524525261251259, -0.2676016323877108, 'A'], [-7.445640931374195, 0.3584606018758793, 'A'], [-3.9379519967071897, 0.28509061299561095, 'A'], [-3.831729443727788, 1.2188310377255511, 'A'], [-5.702975652273074, -1.1892327625842443, 'A'], [-4.439948690317305, -0.6088240907977401, 'A'], [-5.134355589937063, -0.4387085034684176, 'A'], [-0.02417400344377166, 1.7371379442466228, 'C'], [-0.6169717100956201, -0.05470546053768472, 'C'], [1.3430601284846464, -1.4464247606324383, 'C'], [0.722639390413349, -1.3801432562066855, 'C'], [0.39787849549810056, -2.4219694757483885, 'C'], [4.909528215889961, 2.0304933297843837, 'C'], [3.4118989013962016, 0.39767905531674236, 'C'], [0.6504014510267492, -1.6412643081044251, 'C'], [3.575936483209145, 0.8886797800183428, 'C'], [4.050441633621285, -1.4111297986303057, 'C'], [3.524835391957593, -0.7962598344052034, 'C'], [-0.25695358649833727, -4.792777584314668, 'C'], [2.2712661247595887, -0.8960707283107315, 'C'], [1.2402850213944012, -3.6348074322476056, 'C'], [-0.3704320551931987, -1.3966574621948589, 'C'], [10.59369072217267, 5.063370086272745, 'C'], [7.388162919594694, 3.2530347772626085, 'C'], [5.074167549425396, -2.616255955565353, 'D'], [6.015804612050362, -2.428070159933407, 'D'], [8.886710081651788, 0.06251626043708561, 'D'], [8.04345129509809, -0.42855094118916953, 'D'], [7.5867398899867995, -0.35816408207813816, 'D'], [4.745999193692063, -3.01272153238224, 'D'], [17.26178347959192, 3.6904816061952608, 'D'], [11.012544268630574, 1.2796512711456722, 'D'], [11.952051954648535, 2.274670680774901, 'D'], [4.57247777825687, -2.740539006829948, 'D'], [4.64712122875631, -2.9541760750503148, 'D'], [9.822119025017109, 1.114596244101624, 'D'], [5.10813665885816, -2.5117620420661098, 'D'], [5.241282203885775, -2.785619655460366, 'D'], [4.348640406623504, -3.028668176390842, 'D'], [10.291138396478695, 1.4519674624623602, 'D'], [8.425360839100868, 0.12244076193242436, 'D'], [5.9291348966828705, -2.8002566618852027, 'D'], [12.285336218974248, 2.2614951522964537, 'D'], [10.123016060319232, 0.8422847481086354, 'D'], [-6.362058536370231, -1.776888721966689, 'B'], [-3.607790414898749, -0.11405382585885869, 'B'], [-3.5863297337978453, -1.003289609185522, 'B'], [-4.054339453868017, -0.6375200043500148, 'B'], [-4.240053513108218, -1.1755276292568613, 'B'], [-5.134777911527315, -1.4695033193550058, 'B'], [-1.4889239040130964, 1.4976310317516441, 'B'], [-6.754828146543384, -1.1786478007750532, 'B'], [-5.393355342242125, -0.7712159748923132, 'B'], [-2.6193983264691334, -1.7530225717366794, 'B'], [-4.337600871033667, -1.29871274675849, 'B'], [-3.8129587283903636, 0.3320505375336237, 'B'], [0.996577117919612, 4.011035199145417, 'B'], [1.2454893051447347, 2.4064910426011714, 'B'], [-2.28920849335583, 0.8671759601357621, 'B'], [-2.654467193555008, 0.15172163371743422, 'B'], [-3.2066143269301755, -1.0718186423966996, 'B'], [-2.1391157194198955, -0.35966062691412143, 'B'], [-2.0561793218125906, -0.4878697696741051, 'B'], [-0.8070328764507083, 0.7139337454107234, 'B'], [-0.6968579945821873, 1.1434692996521818, 'B'], [-2.3526738583605917, -0.20957185223025143, 'B'], [-0.7585178997937219, -0.904787509339181, 'B'], [3.2457790366169634, 1.650743098214606, 'B'], [0.3284646238578842, 0.24071040358540596, 'B'], [-1.4957934160157627, -1.2669962800304935, 'B'], [-1.5382403984088373, 0.0239536561604769, 'B'], [-1.2594390015524441, -0.47009338598493816, 'B'], [2.5791370303408563, 1.9935734173147306, 'B']]
列名是:
columns = ['PC1', 'PC2', 'Type']
我在 matplotlib 中绘制坐标轴,所以第一行有两张图,第二行有一张。我想在第一行的其中一张图上画一个图例。
该图的简化代码为:
x1 = sc['PC1']
y1 = sc['PC2']
groups = ('A', 'B', 'C', 'D')
colors1 = {'A':'tab:red', 'B':'tab:blue', 'C':'tab:green', 'D':'tab:purple'}
f = plt.figure(figsize=(10,10))
ax3 = f.add_subplot(111)
ax3.scatter(x1, y1, color=sc['Type'].map(colors1) ,label=[d for d in groups])
ax3.set(xlabel = "$t_1$",
ylabel = "$t_2$")
ax3.axhline(y=0, xmin=-10, xmax=20, color='k', linestyle='--', linewidth=0.75)
ax3.axvline(x=0, ymin=-10, ymax=20, color='k', linestyle='--', linewidth=0.75)
ax3.legend()
plt.show()
我想创建一个图例,其中彩色点与 A、B、C 和 D 匹配。
我看到了一些类似问题的例子,但我无法将解决方案应用到我的案例中。
有什么建议吗?
谢谢!
一种方法是一次绘制一种类型:
groups = ('A', 'B', 'C', 'D')
colors1 = {'A':'tab:red', 'B':'tab:blue', 'C':'tab:green', 'D':'tab:purple'}
f = plt.figure(figsize=(10,10))
ax3 = f.add_subplot(111)
for label in groups:
x1 = sc.loc[sc['Type'].eq(label), 'PC1']
y1 = sc.loc[sc['Type'].eq(label), 'PC2']
ax3.scatter(x1, y1, color=colors1[label], label=label)
ax3.set(xlabel = "$t_1$",
ylabel = "$t_2$")
ax3.axhline(y=0, xmin=-10, xmax=20, color='k', linestyle='--', linewidth=0.75)
ax3.axvline(x=0, ymin=-10, ymax=20, color='k', linestyle='--', linewidth=0.75)
ax3.legend()
plt.show()