How to manage a list in python

Viewed 74

Here are the two lists I have :

l1 = ['5', '10', '15', '20', '25', '30', '35', '40', '45']
l2 = ['0.011530', '0.039914', '0.085069', '0.145798', '0.213572', '0.287898', '0.355587', '0.413209', '0.472346']

thanks to these two lists I would like to create two lists with the same values but with some changes :

new_list1 = [5,10,10,15,15,20,20,25,25,30,30,35,35,40,40,45,45]
new_list2 = ['0.011530', '0.011530', '0.039914', '0.039914', '0.085069', '0.085069', '0.145798', '0.145798', '0.213572', '0.213572', '0.287898', '0.287898', '0.355587', '0.355587', '0.413209', '0.413209', '0.472346']

the purpose of this two new lists is to create a graph and instead of a line, I will get steps.
[What I dont want]
1

[What I want]
2

so as a beginner i wrote that :

def test():
 
  new_list1.insert(0,l1[0])
  new_list2.insert(0,l2[0])

  new_list1.insert(1,l1[1])
  new_list2.insert(1,l2[0])

  new_list1.insert(2,l1[1])
  new_list2.insert(2,l2[1])

  new_list1.insert(3,l1[2])
  new_list2.insert(3,l2[1])

  new_list1.insert(4,l1[2])
  new_list2.insert(4,l2[2])
  
  new_list1.insert(5,l1[3])
  new_list2.insert(5,l2[2])

  new_list1.insert(6,l1[3])
  new_list2.insert(6,l2[3])

  new_list1.insert(7,l1[4])
  new_list2.insert(7,l2[3])
  
  new_list1.insert(8,l1[4])
  new_list2.insert(8,l2[4])

  new_list1.insert(9,l1[5])
  new_list2.insert(9,l2[4])

  new_list1.insert(10,l1[5])
  new_list2.insert(10,l2[5])
  
  new_list1.insert(11,l1[6])
  new_list2.insert(11,l2[5])

  new_list1.insert(12,l1[6])
  new_list2.insert(12,l2[6])

  new_list1.insert(13,l1[7])
  new_list2.insert(13,l2[6])
  
  new_list1.insert(14,l1[7])
  new_list2.insert(14,l2[7])

  new_list1.insert(15,l1[8])
  new_list2.insert(15,l2[7])

  new_list1.insert(16,l1[8])
  new_list2.insert(16,l2[8])

  print(new_list1)
  print(new_list2)

  x = np.array(new_list1) 
  y = np.array(new_list2)
  
  # plotting
  plt.title("Line graph") 
  plt.xlabel("X axis") 
  plt.ylabel("Y axis") 
  plt.plot(x, y, color ="green") 
  plt.show()

which makes the trick, but I want it to be more "beautiful" and easy to read, with some "for" loop. How can I do that ?

4 Answers

You can use plt.step(x,t) this way:

import matplotlib.pyplot as plt

l1 = ['5', '10', '15', '20', '25', '30', '35', '40', '45']
l2 = ['0.011530', '0.039914', '0.085069', '0.145798', '0.213572', '0.287898', '0.355587', '0.413209', '0.472346']

x = [int(v) for v in l1]
y = [float(k) for k in l2]

plt.step(x, y)

enter image description here

This is the for loop that I believe replaces your insert functions.

def test():
  for i in range(16+1):
    new_list1.append(l1[i//2+i%2])
    new_list2.append(l2[i//2])

This is my method, which I think is pretty easy to understand:

new_lst1 = []
new_lst2 = []

for count, i in enumerate(l1):
    if count != 0:
        new_lst1.append(int(i))
        new_lst1.append(int(i))
    else:
        new_lst1.append(int(i))

for count, i in enumerate(l2):
    if count != len(l2) - 1:
        new_lst2.append(i)
        new_lst2.append(i)
    else:
        new_lst2.append(i)

Basically, the enumerate allows us to loop through the list while simultaneously getting the count of the list. This allows us to only add one 5.

You don't need to mess with your original array at all! plt.plot() takes an argument drawstyle, which you can set to 'steps' to get your desired output without any changes to the original array.

import matplotlib.pyplot as plt

l1 = ['5', '10', '15', '20', '25', '30', '35', '40', '45']
l2 = ['0.011530', '0.039914', '0.085069', '0.145798', '0.213572', '0.287898', '0.355587', '0.413209', '0.472346']

x = [int(v) for v in l1]
y = [float(k) for k in l2]

plt.plot(x, y, drawstyle='steps')

enter image description here

However, note that this is not the plot you want: In your plot, the first horizontal line is at the y=0.011530, but in this one it is at y=0.039914.

To fix this, you simply need to duplicate the last x value and the first y value

x.insert(-1, x[-1])
y.insert(0, y[0])
plt.plot(x, y, drawstyle='steps')

enter image description here


If you still want to mess with your array, use a loop!

new_list_x = []
new_list_y = []
# zip three lists: 
#    0th through penultimate elements of x -> currentX
#    0th through penultimate elements of y -> currentY
#    1st through last elements of x -> nextX
for currentX, currentY, nextX in zip(x[:-1], y[:-1], x[1:]):
    # Add the point at the current (x, y)
    new_list_x.append(currentX)
    new_list_y.append(currentY)

    # Add the point at the next x but the current y
    new_list_x.append(nextX)
    new_list_y.append(currentY)

plt.plot(new_list_x, new_list_y)

enter image description here

Related