2
$\begingroup$
Date        Day_Perc_Change No. of Trades
2017-05-15  0.000000        40593
2017-05-16  -0.019184       22027
2017-05-17  -0.008550       15709
2017-05-18  -0.017699       47159
2017-05-19  -0.021748       85921

I visualized "Day_Perc_Change" in stem plot and "No. of Trades" in line plot. But now i want to compare these two plots by overlaying them on each other.. How do i visualize that?

x = df["Date"]
y = df["Day_Perc_Change"]

plt.stem(x, y, bottom=0)
plt.show()

the stem plot

df.plot(x="Date", y="No. of Trades", figsize=(10,8))

This is the line plot for Trades

i Want to overlay these two on each other to compare them..is there a way to do that?

Part 2: post trying the answer mentioned below: After trying the below code by @ElBurro i get the following outputenter image description here

The expected output should look like thisenter image description here

$\endgroup$

2 Answers 2

1
$\begingroup$

Since you just want to get a sense of distribution, Just scale the df["No. of Trades"] to the range [0, 1] and scale it up by max of df["Day_Perc_Change"].

import pandas as pd
import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt
sns.set(style="darkgrid")


scaledvolume =  df["No. of Trades"] - df["No. of Trades"].min()
scaledvolume = scaledvolume/scaledvolume.max() * df.Day_Perc_Change.max()

fig, ax = plt.subplots(figsize=(12, 6))

ax.stem(df.index, df.Day_Perc_Change , 'b', markerfmt='bo', label='Percente Change')
ax.plot(df.index, scaledvolume, 'k', label='Volume')

ax.set_xlabel('Date')
plt.legend(loc=2)

plt.tight_layout()
plt.xticks(plt.xticks()[0], df.index.date, rotation=45)
plt.show()

should get, Output: output

$\endgroup$
2
  • $\begingroup$ 'RangeIndex' object has no attribute 'date'. it gave me this message as error, what does that mean? df['Date] is a datetime64 type.i converted it, still i got this error. $\endgroup$ Jul 4, 2019 at 16:10
  • $\begingroup$ I think you should change the index of df to Date. Use df = df.set_index('Date') to set the index of df to be Date column. $\endgroup$
    – Abdul
    Jul 5, 2019 at 18:07
1
$\begingroup$

I think what you are looking for is called twinx. I just used one of the examples given and slightly modified the input parameters:

import numpy as np
import matplotlib.pyplot as plt

date_name = "Date"
stem_name = "Day_Perc_Change"
plot_name = "No. of Trades"
t = df[date_name]
data1 = df[stem_name]
data2 = df[plot_name]

fig, ax1 = plt.subplots()

color = 'tab:blue'
ax1.set_xlabel(date_name)
ax1.set_ylabel(stem_name, color=color)
ax1.stem(t, data1)
ax1.tick_params(axis='y', labelcolor=color)

ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis

color = 'tab:red'
ax2.set_ylabel(plot_name, color=color)  # we already handled the x-label with ax1
ax2.plot(t, data2, color=color)
ax2.tick_params(axis='y', labelcolor=color)

fig.tight_layout()  # otherwise the right y-label is slightly clipped
plt.show()
$\endgroup$
7
  • $\begingroup$ the package doesnt have any examples for overlaying stem plot and line lot? how shall i proceed? $\endgroup$ Jul 1, 2019 at 13:48
  • $\begingroup$ can you add your example code how you plot so far? $\endgroup$
    – El Burro
    Jul 1, 2019 at 13:56
  • $\begingroup$ df.plot(x="Date", y="No. of Trades", figsize=(10,8)) this is for line plot $\endgroup$ Jul 1, 2019 at 14:01
  • $\begingroup$ plt.stem(x, y, bottom=0) plt.show() where x was Date and y was Day_Perc_Change $\endgroup$ Jul 1, 2019 at 14:01
  • $\begingroup$ @VinitSutar would you add your code to your question? That would make it easier to read and more concise. $\endgroup$
    – m13op22
    Jul 1, 2019 at 14:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.