0

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

2
  • Are you saying you have two dataframes with lots of matched columns, and you want to create lots of merged graphs, merging one column at a time? E.g. AMC-AMC from the two dataframes, then GME-GME in a different graph, etc? Commented Jul 28, 2021 at 15:14
  • Yes that is what I want Commented Jul 29, 2021 at 10:42

2 Answers 2

2

Put all lines in the same subplot, e.g.:

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

etc.

Sign up to request clarification or add additional context in comments.

3 Comments

can you do it in a for loop otherwise it is redundant
I have around 50 tickers and wouldn't be able to write them manually
I don't understand: Your question is about how to combine lines in a single graph, and in the example you use different variable names. If in fact you have a list of dataframes, then loop over the list and plot each dataframe. You can use label instead of color to distinguish them, and supply some label from the dataframe or from some other iterable. Or something along these lines. I do not see any of this in your question, so I'm not sure which variant would be applicable to you.
0

I generally find it more easy using subplots instead of figure e.g


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

14 Comments

yeah but how do I combine two datasets into one graph and then graph 4 graphs that overlap
I think you'll need to provide a visual expected outcome - its rather difficult to understand how you want it to look
Do you need two plots with the same columns from each dataframe in the same plots e.g GME from df1 and prices in plot 1, AMC from df1 and prices in plot2 etc or...?
yeah and in total I want 4 graphs with the same pattern
Then just do as above - but plot what you want in the same ax object
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.