How to draw single plot with two colors in Pandas

Viewed 42

I plotted below graph with the list of values. But the graphs are not merging. I would need the Red plot starts right after the blue plot.

plt.plot(x_list[:251])
plt.plot(x_list[251:],color='r')

enter image description here

1 Answers

Create example data

x_list = np.random.randint(0,100,300)

Make another list, fill the data till 251 with NANs:

y_list = [np.nan]*300
y_list[251:] = x_list[251:]

Plot

plt.plot(x_list[:251])
plt.plot(y_list,color='r')

Alternative data given by OP

x_list = [[26756.0], [0.0], [831.0], [23676.0], [0.0], [0.0], [0.0], [1325.0], [33293.0], [32323.000000000004], [30535.0], [32351.0], [30859.000000000004], [28416.0]]

Note that I put brackets around np.nan to be in line with the input data - which is a nested list. As the example list has length of 14, I used 10 as point to let start the second time-serie.

y_list = [[np.nan]]*len(x_list)
y_list[10:] = x_list[10:]
plt.plot(x_list[:10])
plt.plot(y_list, color='r')

An alternative would be to flatten your nested list using list(np.array(x_list).flat) and then continue with the first solution.

Related