This question has been asked around but I could not adapt any of the previous answers and I am looking for help. I have the following df:
myf = {'A': {0: 0.118905, 1: 0.116909, 2: 0.114422, 3: 0.111605, 4: 0.108002, 5: 0.103467, 6: 0.0982083, 7: 0.0938204, 8: 0.090206, 9: 0.0871905, 10: 0.084656, 11: 0.08252, 12: 0.0807242, 13: 0.0792274, 14: 0.0780016, 15: 0.0770298, 16: 0.0763054, 17: 0.0758326, 18: 0.0756302, 19: 0.075625, 20: 0.075625}, 'B': {0: 0.13157894736842105, 1: 0.12597030386740332, 2: 0.12036337209302327, 3: 0.1147584355828221, 4: 0.10915584415584415, 5: 0.10355603448275864, 6: 0.09795955882352941, 7: 0.09236712598425198, 8: 0.08677966101694914, 9: 0.08119839449541284, 10: 0.07562500000000003, 11: 0.07006181318681318, 12: 0.06451219512195122, 13: 0.05898116438356164, 14: 0.053476562500000005, 15: 0.04801136363636364, 16: 0.042608695652173914, 17: 0.0373141891891892, 18: 0.03223214285714286, 19: 0.02766447368421054, 20: 0.025000000000000022}, 'x': {0: 0.0, 1: 0.05, 2: 0.1, 3: 0.15, 4: 0.2, 5: 0.25, 6: 0.3, 7: 0.35, 8: 0.4, 9: 0.45, 10: 0.5, 11: 0.55, 12: 0.6, 13: 0.65, 14: 0.7, 15: 0.75, 16: 0.8, 17: 0.85, 18: 0.9, 19: 0.95, 20: 1.0}, 'y1': {0: 0.17835797265337505, 1: 0.172319328520723, 2: 0.16536874055499218, 3: 0.15767532547570168, 4: 0.1486735047377635, 5: 0.1381930294843873, 6: 0.12929509857138935, 7: 0.12325030886953686, 8: 0.11846218212072777, 9: 0.11466787144642454, 10: 0.11169732155818163, 11: 0.10944101957114595, 12: 0.10783327222854425, 13: 0.10684294528516422, 14: 0.10647160435074597, 15: 0.10675591390692067, 16: 0.10777718512747676, 17: 0.10978722893656234, 18: 0.11271967393192793, 19: 0.11343739518387072, 20: 0.11343715663665711}, 'y2': {0: 0.16620498614958448, 1: 0.16014563810628496, 2: 0.1541069497025419, 3: 0.1480924667845986, 4: 0.1421065947039973, 5: 0.1361548751486326, 6: 0.13024437716262977, 7: 0.12438426126852255, 8: 0.11858661304222927, 9: 0.11286770894705835, 10: 0.10725000000000004, 11: 0.1017653363120395, 12: 0.09646044021415827, 13: 0.09140668981047104, 14: 0.08671875000000016, 15: 0.08259297520661152, 16: 0.07939508506616252, 17: 0.07788988312636966, 18: 0.07997448979591827, 19: 0.09188019390581716, 20: 0.15000000000000116}}
I want to plot y1 line of one color if A>=B and plot y2 of a different color if A<B
I am using this code:
plt.figure()
line1,=plt.plot(myf[myf["A"]>= myf["B"]].x,
myf[myf["A"]>= myf["B"]].y1,label=r'$y_1$')
line2,=plt.plot(myf[myf["A"]< myf["B"]].x,
myf[myf["A"]< myf["B"]].y2,label=r'$y_2$', color='r')
however the line comes back as broken segment since the df is sparse. Someone suggests to shift and manipulate data, but I am not able to understand how I shall shift it. Do you have any idea to have the line not broken?
My approach has been to create a unique column y with the one to plot and then overlap this with y2 using a different color. But this works only when there are not discontinuities among y1 and y2.
source https://stackoverflow.com/questions/74944154/data-transformation-for-conditional-multicolour-line-matplotlib
Comments
Post a Comment