Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shift a line in a matplotlib plot?

Tags:

I am trying to plot two lists in python, one is test1 and other is predictions1 .

I wish to plot the first 150 entries of the test1 list and then plot the entries 101- 150 of predictions1 list, so that the two plots get superimposed on one another. Here is what I tried:

import matplotlib.pyplot as plt
plt.figure(figsize=(15,8))
plt.plot(test1[1:150])
plt.plot(predictions1[101:150], color='red')
plt.show()

But I am getting the result: enter image description here

Clearly this is not what I wanted to achieve as I want the red plot to get superimposed over the blue plot towards the end. Please help.

like image 271
The Doctor Avatar asked Mar 09 '18 14:03

The Doctor


People also ask

How do I change the width of a line in matplotlib?

Line Width You can use the keyword argument linewidth or the shorter lw to change the width of the line.


1 Answers

The idea would be to create a list of numbers to use as your x data, from 0 to 150:

x_data = range(150)

Then slice this so that for the first set of data, your x axis uses numbers 0 to 149. Then the second set of data needs to be sliced to use the numbers 100 to 149.

plt.plot(x_data[:], test1[:150])
plt.plot(x_data[100:], predictions1[100:150], color='red')

Note that Python indexing starts at 0, not 1

like image 151
DavidG Avatar answered Sep 22 '22 13:09

DavidG