将不同的数据框组合成一个图



我有两个数据框架第一个

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

第二个

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

我想把列AMC,GME..从AMC之上的第一个数据帧,GME从另一个数据帧。我想要有4个独立的图,它们相互叠加在一起这是我所拥有的,但它只适用于一个计时器所以我假设我需要循环遍历每一列

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')

感谢您的帮助

将所有行放在同一子图中,例如:

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

等。

我通常发现使用子图代替figure更容易,例如


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

最新更新