Skip to main content

Data transformation for conditional multicolour line matplotlib

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

Popular posts from this blog

ValueError: X has 10 features, but LinearRegression is expecting 1 features as input

So, I am trying to predict the model but its throwing error like it has 10 features but it expacts only 1. So I am confused can anyone help me with it? more importantly its not working for me when my friend runs it. It works perfectly fine dose anyone know the reason about it? cv = KFold(n_splits = 10) all_loss = [] for i in range(9): # 1st for loop over polynomial orders poly_order = i X_train = make_polynomial(x, poly_order) loss_at_order = [] # initiate a set to collect loss for CV for train_index, test_index in cv.split(X_train): print('TRAIN:', train_index, 'TEST:', test_index) X_train_cv, X_test_cv = X_train[train_index], X_test[test_index] t_train_cv, t_test_cv = t[train_index], t[test_index] reg.fit(X_train_cv, t_train_cv) loss_at_order.append(np.mean((t_test_cv - reg.predict(X_test_cv))**2)) # collect loss at fold all_loss.append(np.mean(loss_at_order)) # collect loss at order plt.plot(np.log(al...

Sorting large arrays of big numeric stings

I was solving bigSorting() problem from hackerrank: Consider an array of numeric strings where each string is a positive number with anywhere from to digits. Sort the array's elements in non-decreasing, or ascending order of their integer values and return the sorted array. I know it works as follows: def bigSorting(unsorted): return sorted(unsorted, key=int) But I didnt guess this approach earlier. Initially I tried below: def bigSorting(unsorted): int_unsorted = [int(i) for i in unsorted] int_sorted = sorted(int_unsorted) return [str(i) for i in int_sorted] However, for some of the test cases, it was showing time limit exceeded. Why is it so? PS: I dont know exactly what those test cases were as hacker rank does not reveal all test cases. source https://stackoverflow.com/questions/73007397/sorting-large-arrays-of-big-numeric-stings

How to load Javascript with imported modules?

I am trying to import modules from tensorflowjs, and below is my code. test.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title </head> <body> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"></script> <script type="module" src="./test.js"></script> </body> </html> test.js import * as tf from "./node_modules/@tensorflow/tfjs"; import {loadGraphModel} from "./node_modules/@tensorflow/tfjs-converter"; const MODEL_URL = './model.json'; const model = await loadGraphModel(MODEL_URL); const cat = document.getElementById('cat'); model.execute(tf.browser.fromPixels(cat)); Besides, I run the server using python -m http.server in my command prompt(Windows 10), and this is the error prompt in the console log of my browser: Failed to loa...