简体   繁体   中英

combine different dataframes into one graph

I have two dataframes the first one

price = pd.read_csv('top_50_tickers.csv')
     timestamp     GME   MVIS    TSLA    AMC
0   2021-07-23  180.36  13.80  643.38  36.99
1   2021-07-22  178.85  14.18  649.26  37.24
2   2021-07-21  185.81  15.03  655.29  40.78
3   2021-07-20  191.18  14.41  660.50  43.09
4   2021-07-19  173.49  13.67  646.22  34.62

the second one

df1 = pd.read_csv('discussion_thread_data.csv')
tickers                        dt  AMC  GME  MVIS  TSLA
0       2021-03-19 21:00:00+06:00   11   13     0    11
1       2021-03-19 22:00:00+06:00    0    0     3     0
2       2021-03-19 23:00:00+06:00    0    5     0     3
3       2021-03-20 00:00:00+06:00    4    0     6     0

I want to put column AMC,GME.. from the first dataframe on top of AMC, GME from another dataframe. I want to have 4 separate graph with merged graphs on top of each other Here is what I have but it works only with one ticker So I assume I need to loop through each column

fig = plt.figure()
ax = fig.add_subplot()
ax2 = fig.add_subplot(frame_on=False)
ax.plot(price.timestamp, price.GME, color="C0")
ax.axes.xaxis.set_visible(False)
ax2.plot(df1.dt, df1.GME, color="C1")
ax2.yaxis.set_label_position("Ticker Occurence")
ax2.yaxis.tick_right()
ax.set_xlabel('Time Frame')
ax.set_ylabel('Price')

Appreciate any help

Put all lines in the same subplot, eg:

ax = fig.add_subplot()
ax.plot(price.timestamp, price.GME, color="C0")
ax.plot(df1.dt, df1.GME, color="C1")

etc.

I generally find it more easy using subplots instead of figure eg


#Get plotting-columns
plot_cols = df1.columns[1:] #assuming the first columns is not to be plotted, but the rest are


fig,axes= plt.subplots(len(plot_cols),1) #rows x columns 

#Plot them

for i,col in enumerate(plot_cols):  #i = index, col = column-name
   
   axes[i].plot(price.timestamp,price[col])
   axes[i].plot(df1.dt,df1[col])
    .
    .
   #do other stuff with axes[i]
  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM