How To Plot With 2 Y-axis In1 Graph | Python For Finance| 2020

preview_player
Показать описание
In this video, I'll walk you through on how to use the twinx method
to plot multiple sets of data with different scale, in 1 graph.

It's particularly useful when you want to compare different trends
in the same period of time.

Here as an example,
I'm using the Dow Jones Index data getting from Yahoo Finance.

You can use the JupyterLab with Google Colab
(watch this for setup:
)
or you can setup JupyterLab on your own machine
(watch this for setup:
)

Subscribe my channel @
Рекомендации по теме
Комментарии
Автор

Great job man!
I have a question: Can you make multiple columns to share the same y-axis by simply using this code:
y1 = df['Volume', 'High', 'Low' ] ?

jimmysantadeo
Автор

I want to plot y1 axis from a column of dataframe A and y2 axis from a column of dataframe B. Tried to follow your tutorial but still can't manage to do it. Any suggestion?

ilovekhr
Автор

Can multiple y-data be plotted on the same figure, each showing their own scale? For example, this video is almost perfect except can't get it to work for more than 2 y-series. The matplotlib "plaxes_grid example code" is also almost perfect*, in that it has multiple y-series, but the x and y scale is the same for all the data.

I have a csv file containing a date, followed by temperature, pressure, uv-index, humidity, etc. I'd like to :

1) use the same x-axis (date) for all the different y-data (preferably superimposed on top of each other instead of each in its own area). The y-axis would need to show the different scales for the different y-values (0F-100F, 25hg-35hg, 1uvi-10uvi, etc).
2) On the legend, I'd like to click on the different lines to either show or hide that data (and its scale).

*
ax.plot(x, i * x, label=r'$y={}x$'.format(i))
secondary_y=True, ax2 = ax1.twinx(), ax1.sharex(ax2), get_shared_y_axes, ax2.autoscale . ax2 = plt.subplot(212, sharex = ax1)

bennguyen
Автор

if i want to plot y2 axis as "BAR" style not "line" style, then how will be the code

hellofrombd
Автор

Hi, Thank you for your video. I am grateful if anyone can help me to solve my issue below:
I want to combine both stacked bar and line graphs in one using two y-axis. My coding are
import pandas as pd
import matplotlib.pyplot as plt
df1 = pd.DataFrame([['16-Jun-21', 0, 41.86, 2.33, 8.14, 41.86, 5.81], ['29-Jul-21', 0, 65.08, 0, 6.07, 24.51, 4.34],
['11-Aug-21', 0, 49.66, 0, 5.52, 37.93, 6.9], ['15-Sep-21', 0, 51.12, 0, 3.19, 31.63, 14.06],
['6-Oct-21', 0, 65.79, 0, 6.73, 20.56, 6.92], ['3-Nov-21', 0, 57.03, 0, 5.54, 34.06, 3.37],
['1-Dec-21', 0, 35.49, 17.74, 5.91, 32.53, 8.32], ['30-Dec-21', 0, 71.46, 0, 2.98, 21.84, 3.72],
['27-Jan-22', 0.21, 51.45, 0, 0.83, 42.74, 4.56], ['23-Feb-22', 0, 40, 0, 4, 46, 10],
['23-Mar-22', 0, 33.33, 15.52, 4.02, 38.51, 8.62], ['20-Apr-22', 0, 3.19, 0, 1.59, 94.02, 1.2],
['18-May-22', 0, 7.08, 17.7, 3.54, 64.16, 7.52], ['15-Jun-22', 0, 0, 56.46, 0, 33.88, 9.66],
['20-Jul-22', 0, 0, 15.23, 2.03, 78.17, 4.57]],
columns=['Date', 'Cylindrospermopsis raciborskii', 'Merismopedia sp.', 'Aphanocapsa sp.',
'Chroococcus sp.', 'Gloeocapsa sp.', 'Planktolyngbya sp.'])

df2=pd.DataFrame([['16-Jun-21', 330], ['29-Jul-21', 961], ['11-Aug-21', 834], ['15-Sep-21', 612], ['6-Oct-21', 564],
['3-Nov-21', 537], ['1-Dec-21', 595], ['30-Dec-21', 876], ['27-Jan-22', 518], ['23-Feb-22', 225],
['23-Mar-22', 710], ['20-Apr-22', 259], ['18-May-22', 476], ['15-Jun-22', 716], ['20-Jul-22', 219]],
columns=['Date', 'Total Cyanobacteria'])
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot = df1.plot(x='Date', kind='bar', stacked=True)
ax2.plot = df2.plot(x='Date', color='red')
plt.plot()
plt.show()
HOWEVER, I DO NOT know how to combine them in one figure with doubed y-axis?
Thank you.

nguyenthanhkien