Home > Mobile >  How to plot one line with two different colors in pandas
How to plot one line with two different colors in pandas

Time:10-22

import pandas as pd
import matplotlib.pyplot as plt
alist = [110,120,130]
blist = [150,170,190]

df1 = pd.Series(alist)
df2 = pd.Series(blist)

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


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

Is there any way I can plot this line but have "df1" in the color blue and "df2" in the color green and still keep them as the same line? My current plot looks like this: enter image description here

If you use [3:] then you get gap between lines:

enter image description here


Full working code:

import pandas as pd
import matplotlib.pyplot as plt

alist = [110,120,130]
blist = [150,170,190]

df1 = pd.Series(alist)
df2 = pd.Series(blist)

df3 = pd.concat([df1, df2]).reset_index(drop=True)

df3.iloc[:3].plot(color='blue')
df3.iloc[2:].plot(color='green')
#df3.iloc[3:].plot(color='green')  # with gap

plt.show()
  • Related