简体   繁体   中英

How to create visualization from 2 different data sets with multiple lines?

I am trying to plot the price of GPU's vs. the price of Cryptocurrency.

I have been able to create 2 separate visualizations that show the average price of GPUs and the average price of cryptocurrency by year, but cannot seem to combine them.

...
plt.plot(GPUDATA.groupby(GPUDATA['Date'].dt.strftime('%Y')['Price_USD'].mean())
...

That produces this image for "GPU Prices":
图片

plt.plot(BITCOINDATA.groupby(BITCOINDATA['Date'].dt.strftime('%Y'))['Open'].mean())

That produces this image for "Crypto Prices":
加密货币

I need these two visualizations to be combined into one graph. I'm sort of new to creating visualizations, so I'm not sure how much more info needs to be provided. I would be happy to provide any more needed information! Thanks!

EDIT: The entries in the dataframes list the id of the product, then a date, then the price of the product on that date. Because of this, there are a lot of duplicate years and ids on both the GPU dataframe and the Crypto dataframe, thats why I did the group by function.

Here is a solution that creates separate y-axis for each series. I generated random data, that's why the charts look different. You can modify parameters such as lower and upper bounds for each y-axis below.

import matplotlib.pyplot as plt

fig, ax1 = plt.subplots(figsize=(10,10))
ax2 = ax1.twinx()

ax1.plot(GPUDATA.groupby(GPUDATA['Date'].dt.strftime('%Y'))['Price_USD'].mean(),'r', label = 'Avg GPU Prices')
ax1.set_ylabel("Avg GPU Price", color='r', fontsize=20)
ax1.set_ylim(350,600)
ax1.tick_params(axis='y', colors= 'r', labelsize=14)
ax1.tick_params(axis='x', colors= 'k', labelsize=14)

ax2.plot(BITCOINDATA.groupby(BITCOINDATA['Date'].dt.strftime('%Y'))['Open'].mean(), 'b', label = 'Avg Crypto Prices')
ax2.set_ylabel("Avg Crypto Price", color='b', fontsize=20)
ax2.set_ylim(0, 900)
ax2.tick_params(axis='y', colors= 'b', labelsize=14)

lines, labels = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax1.legend(lines + lines2, labels + labels2, loc=2, fontsize=20)

ax1.grid(b=False)
ax2.grid(b=False)

plt.title("Yearly Average GPU and Crypto Prices", fontsize=25)
plt.show()

在此处输入图片说明

plt.close('all')
ax = GPUDATA.groupby(GPUDATA['Date'])['Price_USD'].mean().plot()
BITCOINDATA.groupby(BITCOINDATA['Date'])['Open'].mean().plot(ax=ax)
plt.show()

在此处输入图片说明

You can add labels and legends using the plot parameters

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM