Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting line with different colors

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df1 = pd.DataFrame(np.random.randint(0,15,size=(15, 1)))
df2 = pd.DataFrame(np.random.randint(20,35,size=(10, 1)))


frames = [df1, df2]
result = pd.DataFrame(pd.concat(frames))

df3 = result.cumsum()
df3 = df3.reset_index(drop=False)
print(df3)
df3.plot(y=0)
plt.show()

Is it possible to plot the df3 line with two different colors? First color to be for rows 0 to 14 and second color for rows 15 to 24. In a way I want to mark where df1 has ended and df2 has started.

like image 904
user8491020 Avatar asked Dec 10 '25 01:12

user8491020


1 Answers

What about

#[...]
df3 = result.cumsum()
df3 = df3.reset_index(drop=False)
plt.plot(df3.mask(df3.apply(lambda x: x.index < 15))[0], color='blue')
plt.plot(df3.mask(df3.apply(lambda x: x.index > 15))[0], color='green')
plt.show()
plt.close()# do not forget this to save you from Runtime Error.

enter image description here

like image 190
keepAlive Avatar answered Dec 11 '25 14:12

keepAlive



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!