2017-11-24 7 views
1
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() 

Ist es möglich, die df3-Linie mit zwei verschiedenen Farben zu plotten? Erste Farbe für die Zeilen 0 bis 14 und zweite Farbe für die Zeilen 15 bis 24. In gewisser Weise möchte ich markieren, wo df1 beendet wurde und df2 gestartet wurde.Plotting-Linie mit verschiedenen Farben

Antwort

0

Was

#[...] 
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

1

Gerade Grundstück nur der Teil der Datenrahmen, die Sie in möchten, was Farbe Sie mögen, z.B. df3.iloc[:15,:].plot(color="green").

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) 
ax = df3.iloc[:15,:].plot(y=0, color="crimson") 
df3.iloc[15:,:].plot(y=0, color="C0", ax=ax) 
plt.show() 

enter image description here

Verwandte Themen