Jack Novozinsky
Jack Novozinsky

Reputation: 11

Creating a visualization with 2 Y-Axis scales

I am currently trying to plot the price of the 1080 graphics card against the price of bitcoin over time, but the scales of the Y axis are just way off. This is my code so far:

import pandas as pd
from datetime import date
import matplotlib.pyplot as plt
from matplotlib.pyplot import *
import numpy as np

GPUDATA = pd.read_csv("1080Prices.csv") 
BCDATA = pd.read_csv("BitcoinPrice.csv")

date = pd.to_datetime(GPUDATA["Date"])
price = GPUDATA["Price_USD"]

date1 = pd.to_datetime(BCDATA["Date"])
price1 = BCDATA["Close"]

plot(date, price)
plot(date1, price1)

And that produces this:

enter image description here

The GPU prices, of course, are in blue and the price of bitcoin is in orange. I am fairly new to visualizations and I'm having a rough time finding anything online that could help me fix this issue. Some of the suggestions I found on here seem to deal with plotting data from a single datasource, but my data comes from 2 datasources.

One has entries of the GPU price in a given day, the other has the open, close, high, and low price of bitcoin in a given day. I am struggling to find a solution, any advice would be more than welcome! Thank you!

Upvotes: 0

Views: 53

Answers (1)

gmds
gmds

Reputation: 19885

What you want to do is twin the X-axis, such that both plots will share the X-axis, but have separate Y-axes. That can be done in this way:

import numpy as np
import pandas as pd

import matplotlib.pyplot as plt

GPUDATA = pd.read_csv("1080Prices.csv") 
BCDATA = pd.read_csv("BitcoinPrice.csv")

gpu_dates = pd.to_datetime(GPUDATA["Date"])
gpu_prices = GPUDATA["Price_USD"]

btc_dates = pd.to_datetime(BCDATA["Date"])
btc_prices = BCDATA["Close"]

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()  # Create a new Axes object sharing ax1's x-axis

ax1.plot(gpu_dates, gpu_prices, color='blue')
ax2.plot(btc_dates, btc_prices, color='red')

As you have not provided sample data, I am unable to show a relevant demonstration, but this should work.

Upvotes: 1

Related Questions