1
$\begingroup$

I would like to plot=ListLinePlot[...] a few lines and color them with some external data set. This can be done with a small trick plot[[1, 2, 3]] = colors from this post. It explicitly replaces the color function after plotting by colors from our external data. This seems to work well if we plot all the data.

But actually I need to set a PlotRange in order to exclude some parts of these lines. Then the trick causes color distortion at the edges (e.g., the top left corner should all be cyan). I can imagine it's somehow because my external color data colors includes all the data points not yet constrained by PlotRange. But I failed to find a way out that can get both the coloring and which parts of lines to plot right.

enter image description here

data = {{{0.005653237735962933`, 
     0.010607993242911453`}, {0.034133659932433426`, 
     0.015653920720404807`}, {0.06065606795896324`, 
     0.01943721540412645`}, {0.08390995286468342`, 
     0.022014161059906858`}, {0.08916370907323788`, 
     0.036279818787930664`}, {0.09435165171429662`, 
     0.04798509431937201`}, {0.09332996220228577`, 
     0.07721768668167454`}, {0.09285391023217408`, 
     0.10980907718130412`}, {0.09281339118360192`, 
     0.1399830570984551`}, {0.09281185576000006`, 
     0.16998328305361163`}, {0.09281242494956476`, 
     0.1999999930342066`}, {0.09281172095384738`, 
     0.23001655241341518`}, {0.09281058837993444`, 
     0.260010234754466`}, {0.09281058837858451`, 
     0.2899897652379816`}, {0.09281172085783579`, 
     0.31998344729109784`}, {0.09281242051920402`, 
     0.3499999999999994`}, {0.09281172085783547`, 
     0.38001655270890233`}, {0.09281058837857209`, 
     0.4100102347619082`}, {0.09281058837857194`, 
     0.4399897652380926`}, {0.09281172085783529`, 
     0.4699834472910991`}, {0.09281242051920392`, 
     0.5000000000000007`}, {0.09281172085783557`, 
     0.5300165527089037`}, {0.09281058837857248`, 
     0.5600102347619078`}, {0.09281058837857226`, 
     0.5899897652380933`}, {0.09281172085783553`, 
     0.6199834472910989`}, {0.09281242051920398`, 
     0.6500000000000005`}, {0.09281172085783441`, 
     0.6800165527088851`}, {0.09281058837839327`, 
     0.7100102347601643`}, {0.09281058836146726`, 
     0.7399897651237732`}, {0.09281171979328497`, 
     0.7699834422043546`}, {0.09281237726033027`, 
     0.7999998370475062`}, {0.09281058931463071`, 
     0.8300125631863386`}, {0.09279263969194475`, 
     0.8599437627597543`}, {0.09267159902619321`, 
     0.8894568138393825`}, {0.09246895607964628`, 
     0.9178968122072689`}, {0.09304552428140787`, 
     0.9450723795438566`}, {0.09276917256117499`, 
     0.9595629234597737`}, {0.07145776101794576`, 
     0.9629064668943011`}, {0.046246517458753494`, 
     0.9655545850691856`}, {0.01848907659132105`, 
     0.9708120255120138`}}, {{0.025346191611582285`, 
     0.007722760961108188`}, {0.055298680572146666`, 
     0.008408624088891358`}, {0.08245431275378341`, 
     0.010786974962749584`}, {0.1055703543178903`, 
     0.015996582484846682`}, {0.10340132082771206`, 
     0.030162958189361863`}, {0.10394841146794698`, 
     0.053739515641404445`}, {0.10532941026032286`, 
     0.08125528588355856`}, {0.10569597512355791`, 
     0.11021729216516679`}, {0.10573970641320904`, 
     0.14002019712392727`}, {0.10574224216469964`, 
     0.17000277188451296`}, {0.10574224040350974`, 
     0.20000002665066166`}, {0.10574233186033416`, 
     0.229998152759875`}, {0.1057424774214458`, 
     0.25999885811160056`}, {0.10574247742129485`, 
     0.29000114189306375`}, {0.10574233186828734`, 
     0.3200018477039201`}, {0.10574224190847084`, 
     0.3500000000000005`}, {0.10574233186828713`, 
     0.37999815229608`}, {0.10574247742129102`, 
     0.4099988581069568`}, {0.10574247742129082`, 
     0.44000114189304373`}, {0.10574233186828698`, 
     0.4700018477039196`}, {0.10574224190847041`, 
     0.5000000000000007`}, {0.10574233186828698`, 
     0.5299981522960802`}, {0.10574247742129171`, 
     0.5599988581069576`}, {0.10574247742129095`, 
     0.5900011418930446`}, {0.10574233186828677`, 
     0.6200018477039205`}, {0.10574224190847102`, 
     0.6500000000000002`}, {0.10574233186828631`, 
     0.679998152296078`}, {0.10574247742123524`, 
     0.7099988581071168`}, {0.10574247741852298`, 
     0.740001141935534`}, {0.10574233187163844`, 
     0.77000185146915`}, {0.10574225029080235`, 
     0.8000001820627612`}, {0.10574280027854309`, 
     0.8300032921649151`}, {0.10575484865874195`, 
     0.860083122636692`}, {0.10591814139556845`, 
     0.890754067428075`}, {0.10694546384691221`, 
     0.9222674529357735`}, {0.10912736314483029`, 
     0.9476551159138625`}, {0.10726987441584522`, 
     0.9644695989256474`}, {0.09333013017764032`, 
     0.9706372956622847`}, {0.06777243788819755`, 
     0.9741368634459536`}, {0.03894760018961324`, 
     0.9754571977851386`}}, {{0.037927368609894985`, 
     0.0079171833021351`}, {0.07023097557275536`, 
     0.008609614204127944`}, {0.10085514198768483`, 
     0.010612604165474982`}, {0.1267950574745349`, 
     0.016788078605830224`}, {0.12410056170145212`, 
     0.0268377412817646`}, {0.1204301529754309`, 
     0.04918636133697628`}, {0.11983582790612077`, 
     0.07943024870388579`}, {0.1198632236679382`, 
     0.10994609085966908`}, {0.11987743032004944`, 
     0.13999859820878402`}, {0.11987884609285496`, 
     0.1699998701186915`}, {0.11987892707657331`, 
     0.20000000789527378`}, {0.11987891561686716`, 
     0.23000022841216025`}, {0.11987889412080897`, 
     0.26000014102034447`}, {0.11987889412105811`, 
     0.2899998589837866`}, {0.11987891564606024`, 
     0.3199997718322591`}, {0.11987892894918209`, 
     0.3500000000000009`}, {0.11987891564606054`, 
     0.3800002281677416`}, {0.11987889412105893`, 
     0.41000014101625354`}, {0.1198788941210586`, 
     0.43999985898374694`}, {0.11987891564606021`, 
     0.4699997718322591`}, {0.11987892894918198`, 
     0.5000000000000004`}, {0.1198789156460605`, 
     0.5300002281677417`}, {0.11987889412105941`, 
     0.5600001410162541`}, {0.11987889412105876`, 
     0.5899998589837471`}, {0.11987891564606032`, 
     0.6199997718322592`}, {0.11987892894918195`, 
     0.6500000000000004`}, {0.11987891564606064`, 
     0.6800002281677455`}, {0.11987889412106872`, 
     0.7100001410167963`}, {0.11987889412396165`, 
     0.7399998590325916`}, {0.11987891593977584`, 
     0.7699997744266164`}, {0.11987894503916775`, 
     0.8000000798998869`}, {0.11987942672693612`, 
     0.8300015638461942`}, {0.11988835253363081`, 
     0.8600102541703518`}, {0.11998039144920497`, 
     0.8899956420332011`}, {0.1204951496674051`, 
     0.9188860433545576`}, {0.12063427154774377`, 
     0.9395019850172361`}, {0.11557596813545072`, 
     0.9614805318414349`}, {0.11378888447077079`, 
     0.9713986067949428`}, {0.08407155494584004`, 
     0.9740104284515182`}, {0.05247134884848827`, 
     0.9752464937480022`}}, {{0.25928169023514036`, 
     0.018809353668001176`}, {0.21539834282403117`, 
     0.018925138336405474`}, {0.17144292318444293`, 
     0.019208816889124576`}, {0.13017911624923928`, 
     0.017409457741358284`}, {0.14061608140144014`, 
     0.04374621418690256`}, {0.13762867894107525`, 
     0.05298725793481839`}, {0.1355087665423328`, 
     0.07960424787563367`}, {0.1352101833822162`, 
     0.10988619359392104`}, {0.13518680522404866`, 
     0.13998999195502282`}, {0.13518582088676953`, 
     0.16999958362031303`}, {0.1351858028844983`, 
     0.19999998957989806`}, {0.13518580518020956`, 
     0.22999997157415905`}, {0.1351858085835579`, 
     0.2599999825019115`}, {0.1351858085837691`, 
     0.29000001749812715`}, {0.13518580519279952`, 
     0.32000002831256513`}, {0.1351858030970625`, 
     0.3500000000000015`}, {0.13518580519279966`, 
     0.37999997168743666`}, {0.13518580858377066`, 
     0.4099999825018878`}, {0.13518580858377044`, 
     0.440000017498112`}, {0.13518580519279946`, 
     0.4700000283125654`}, {0.13518580309706263`, 
     0.4999999999999998`}, {0.1351858051927996`, 
     0.529999971687436`}, {0.13518580858377108`, 
     0.5599999825018884`}, {0.1351858085837705`, 
     0.5900000174981125`}, {0.1351858051927996`, 
     0.6200000283125652`}, {0.1351858030970627`, 
     0.6500000000000007`}, {0.1351858051928`, 
     0.6799999716874399`}, {0.13518580858379778`, 
     0.7099999825021046`}, {0.13518580858651225`, 
     0.7400000175001943`}, {0.13518580533648172`, 
     0.7700000273758569`}, {0.13518580597861482`, 
     0.7999999254539671`}, {0.13518574832160676`, 
     0.8299973577365157`}, {0.13518188773735304`, 
     0.8599520617366768`}, {0.13511735260115637`, 
     0.8895075384911746`}, {0.13477419642721994`, 
     0.9171195052647197`}, {0.1352332653956375`, 
     0.9444158934514592`}, {0.14066646829068047`, 
     0.9669107589694801`}, {0.15195462560813658`, 
     0.9638451960931548`}, {0.19586106755082475`, 
     0.9643137758179364`}, {0.23979632851224253`, 
     0.964485276251047`}}, {{0.312256950494226`, 
     0.037347620955894105`}, {0.2695352354736121`, 
     0.041211523762204695`}, {0.22630598887343367`, 
     0.04310306645992625`}, {0.1832255765447495`, 
     0.04517007093034727`}, {0.1552524285928343`, 
     0.025732299977658697`}, {0.15256018724415604`, 
     0.061563574634907504`}, {0.1517327778287645`, 
     0.08241330302688903`}, {0.15160686129173984`, 
     0.11017065980347657`}, {0.15159706109426654`, 
     0.14000583557239937`}, {0.15159619302911304`, 
     0.17000001566755524`}, {0.1515961520701392`, 
     0.19999999459851867`}, {0.1515961507590771`, 
     0.2300000033037484`}, {0.151596150164982`, 
     0.2600000021477148`}, {0.15159615016497643`, 
     0.28999999784986563`}, {0.15159615074930938`, 
     0.31999999652103267`}, {0.15159615111044622`, 
     0.3500000000000008`}, {0.15159615074930924`, 
     0.3800000034789658`}, {0.15159615016497693`, 
     0.41000000215012006`}, {0.15159615016497677`, 
     0.4399999978498807`}, {0.15159615074930888`, 
     0.4699999965210344`}, {0.1515961511104454`, 
     0.5000000000000001`}, {0.15159615074930885`, 
     0.5300000034789671`}, {0.15159615016497774`, 
     0.5600000021501202`}, {0.15159615016497682`, 
     0.589999997849881`}, {0.15159615074930902`, 
     0.6199999965210348`}, {0.15159615111044583`, 
     0.6500000000000002`}, {0.15159615074930913`, 
     0.6800000034789666`}, {0.1515961501649894`, 
     0.7100000021499058`}, {0.1515961501648996`, 
     0.7399999978212508`}, {0.15159615064883353`, 
     0.7699999947953832`}, {0.15159614306518096`, 
     0.7999999547692912`}, {0.15159586671270955`, 
     0.8299999443740167`}, {0.15159188862704948`, 
     0.8600180845554259`}, {0.1516059502757374`, 
     0.8902052707559164`}, {0.15262672010381925`, 
     0.9200499560720813`}, {0.1601051436897147`, 
     0.9442793239652855`}, {0.16462619753658736`, 
     0.9367410894469714`}, {0.2070899113975395`, 
     0.9394210953865082`}, {0.2503459762842237`, 
     0.9412133063424318`}, {0.29338613758676585`, 
     0.9437334648899658`}}, {{0.32437749483980566`, 
     0.013579576526984978`}, {0.2931853388092946`, 
     0.014131407663997671`}, {0.26225911233436566`, 
     0.02983723290780291`}, {0.22564830385383972`, 
     0.05683069813737821`}, {0.1868346506858329`, 
     0.0676724843508884`}, {0.17438741673457211`, 
     0.052921958913778704`}, {0.16942002573161433`, 
     0.0817802653003875`}, {0.1689705927336392`, 
     0.11016564960174659`}, {0.16896964897228775`, 
     0.14000915066655786`}, {0.16897046700495527`, 
     0.1700004493867539`}, {0.1689704849790972`, 
     0.20000001143390636`}, {0.1689704848529491`, 
     0.22999999971882304`}, {0.16897048495548422`, 
     0.25999999975689353`}, {0.1689704849553213`, 
     0.2900000002426586`}, {0.16897048484098418`, 
     0.32000000039265947`}, {0.1689704847703196`, 
     0.34999999999999976`}, {0.16897048484098423`, 
     0.3799999996073422`}, {0.16897048495532016`, 
     0.40999999975732443`}, {0.1689704849553203`, 
     0.44000000024267577`}, {0.1689704848409841`, 
     0.47000000039265816`}, {0.1689704847703202`, 
     0.5000000000000004`}, {0.16897048484098404`, 
     0.5299999996073427`}, {0.16897048495532113`, 
     0.5599999997573245`}, {0.16897048495532008`, 
     0.590000000242677`}, {0.1689704848409842`, 
     0.6200000003926588`}, {0.16897048477032034`, 
     0.65`}, {0.16897048484098384`, 
     0.6799999996073415`}, {0.16897048495530653`, 
     0.7099999997570816`}, {0.16897048495322736`, 
     0.7400000002394816`}, {0.16897048471957674`, 
     0.7700000015842204`}, {0.16897048334385922`, 
     0.8000000912558846`}, {0.1689706478351958`, 
     0.830002363565479`}, {0.16898002827394318`, 
     0.8599951202365685`}, {0.16922853388681638`, 
     0.8888463927805229`}, {0.17163325880472333`, 
     0.9077335747779499`}, {0.17408403106292555`, 
     0.9230417214193035`}, {0.20811956021371525`, 
     0.9206392639355905`}, {0.24682998813583196`, 
     0.93919449855491`}, {0.27969766629382614`, 
     0.9653945567465219`}, {0.3102766358325893`, 
     0.9705576999643954`}}, {{0.34206074064028963`, 
     0.015917798986428998`}, {0.31102912833396335`, 
     0.03608426676531343`}, {0.27556931838468796`, 
     0.04533010590320492`}, {0.24671231397770246`, 
     0.03749088516806732`}, {0.22067617422866387`, 
     0.06723672671624034`}, {0.19403412621921828`, 
     0.08018547575207054`}, {0.1891285144042013`, 
     0.08863417802204268`}, {0.1872252914753819`, 
     0.11068927026354083`}, {0.1870995923023892`, 
     0.1400063848048224`}, {0.18709743669162013`, 
     0.16999969979888535`}, {0.18709745270365036`, 
     0.19999999838045715`}, {0.18709745375866083`, 
     0.2300000001897816`}, {0.18709745374560086`, 
     0.26000000001647966`}, {0.187097453745603`, 
     0.2899999999867654`}, {0.18709745377172998`, 
     0.3199999999785563`}, {0.18709745378787868`, 
     0.35000000000000026`}, {0.1870974537717306`, 
     0.38000000002144546`}, {0.18709745374560255`, 
     0.41000000001325376`}, {0.18709745374560144`, 
     0.4399999999867486`}, {0.18709745377173018`, 
     0.46999999997855585`}, {0.18709745378787856`, 
     0.5000000000000018`}, {0.1870974537717304`, 
     0.5300000000214458`}, {0.18709745374560222`, 
     0.5600000000132525`}, {0.18709745374560208`, 
     0.5899999999867476`}, {0.18709745377173043`, 
     0.619999999978556`}, {0.18709745378787862`, 
     0.6500000000000015`}, {0.1870974537717299`, 
     0.6800000000214451`}, {0.18709745374558656`, 
     0.7100000000135689`}, {0.18709745374590595`, 
     0.7400000000254086`}, {0.18709745392323357`, 
     0.7700000013763376`}, {0.18709746425833623`, 
     0.7999999495312992`}, {0.18709787759222068`, 
     0.829993367579359`}, {0.18711633662152302`, 
     0.8597257120673181`}, {0.18758939728115742`, 
     0.8860999917275778`}, {0.19094327291021881`, 
     0.9121790116865812`}, {0.20757563404084955`, 
     0.9032999427441618`}, {0.23566677169997535`, 
     0.9364407747793666`}, {0.26157099579437154`, 
     0.9450322663882625`}, {0.29529798356720255`, 
     0.9369542652995086`}, {0.3287716030097226`, 
     0.9626087810770055`}}};
xlst = {-0.9, -0.855, -0.8099999999999999, -0.7649999999999999, \
-0.7199999999999999, -0.6749999999999998, -0.6299999999999998, \
-0.5849999999999997, -0.5399999999999997, -0.49499999999999966, \
-0.4499999999999996, -0.4049999999999996, -0.35999999999999954, \
-0.3149999999999995, -0.26999999999999946, -0.22499999999999942, \
-0.17999999999999938, -0.13499999999999934, -0.0899999999999993, \
-0.04499999999999926, 7.771561172376096*^-16, 0.04500000000000082, 
   0.09000000000000086, 0.135000000000001, 0.18000000000000094, 
   0.22500000000000087, 0.270000000000001, 0.31500000000000117, 
   0.3600000000000011, 0.405000000000001, 0.4500000000000012, 
   0.49500000000000133, 0.5400000000000013, 0.5850000000000012, 
   0.6300000000000013, 0.6750000000000015, 0.7200000000000014, 
   0.7650000000000013, 0.8100000000000015, 0.8550000000000016};

imagesize = 300; imagesize0 = 
 1.1 imagesize; labelsize = 22; ticksize = 18; aspctrt = 
 1/2; colorlabel1 = Black; colorlabel2 = Gray;
subfiglabellst = 
  Style["(" <> # <> ")", colorlabel1, 1.2 labelsize, 
     FontFamily -> "Helvetica"] & /@ {"a", "b", "c", "d"};
subfiginsetlst = 
  Inset[#, ImageScaled[{0.92, 0.92}]] & /@ subfiglabellst;
labelinsetlst = 
  Table[Inset[Style["y", colorlabel2, labelsize], 
    ImageScaled[{If[EvenQ[i], 0.51, 0.51], 0.92}]], {i, 4}];
insetlst = Transpose@{subfiginsetlst, labelinsetlst};
cf = Blend[{{0, Cyan}, {1, Brown}}, #] &;

plotdata = Transpose[{xlst, #[[All, 1]]}] & /@ data;
yrange = {0.11, 0.26};
colors = VertexColors -> (Flatten@Map[cf[#[[2]]] &, data, {2}]);
plot = ListLinePlot[plotdata, PlotRange -> yrange, 
   PlotRangeClipping -> False, AxesLabel -> {"x", None}, 
   Ticks -> {Automatic, {0.14, 0.2, 0.26}}, 
   LabelStyle -> Directive[labelsize], 
   TicksStyle -> Directive[ticksize], 
   Epilog -> {Black, Dashed, Thickness[0.01], 
     Line[{{-0.9, 0.18}, {0.9, 0.18}}], insetlst[[3]]}, 
   ImageSize -> imagesize, AspectRatio -> aspctrt, 
   ColorFunction -> Function[{x, y}, Hue@y]];
plot[[1, 2, 3]] = colors;
plot
$\endgroup$
2
2
$\begingroup$

Here is one way to do the same thing

let's first restructure your data as

plotdata = Transpose[{xlst, #[[All, 1]], #[[All, 2]]}] & /@ data;   

now we need to color the curves according to the value of the third element in plotdata with the colorfunction cf redefined as

cf[arg_] := Blend[{Cyan, Brown}, Rescale[arg, {0, 1}]];  

then the desired results can be obtained as follows

Show[Table[
  ListLinePlot[plotdata[[i]][[All, 1 ;; 2]], PlotRange -> yrange, 
   PlotRange -> yrange, PlotRangeClipping -> False, 
   AxesLabel -> {"x", None}, Ticks -> {Automatic, {0.14, 0.2, 0.26}}, 
   LabelStyle -> Directive[labelsize], 
   TicksStyle -> Directive[ticksize], 
   Epilog -> {Black, Dashed, Thickness[0.01], 
     Line[{{-0.9, 0.18}, {0.9, 0.18}}], insetlst[[3]]}, 
   ImageSize -> imagesize, AspectRatio -> aspctrt, 
   ColorFunction :> 
    Function[{x, y}, 
     cf[plotdata[[i]][[Position[plotdata[[i]][[All, 1]], 
          Nearest[plotdata[[i]][[All, 1]], x][[1]]][[1, 1]], 3]]]], 
   ColorFunctionScaling -> False], {i, 1, Length@plotdata}]]    

enter image description here

$\endgroup$
2
  • $\begingroup$ Thank you for this answer! It works as far as I can see. I'm a bit curious why it is necessary to plot each line separately and then combine them. $\endgroup$
    – xiaohuamao
    Nov 15 '21 at 0:46
  • 1
    $\begingroup$ @xiaohuamao yes, that is annoying but don't know how to remove that at the moment and make it general. I will try later $\endgroup$ Nov 15 '21 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.