简体   繁体   English

如何用多行从2个不同的数据集中创建可视化?

[英]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. 我正在尝试绘制GPU的价格与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. 我已经能够创建2个单独的可视化文件,按年份显示GPU的平均价格和加密货币的平均价格,但似乎无法将它们结合起来。

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

That produces this image for "GPU Prices": 生成此图像的“ GPU价格”:
图片

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. 编辑:数据框中的条目列出了产品的ID,然后是日期,然后是该日期产品的价格。 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. 因此,GPU数据框和加密数据框上都有很多重复的年份和ID,这就是为什么我按功能进行分组。

Here is a solution that creates separate y-axis for each series. 这是为每个系列创建单独的y轴的解决方案。 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. 您可以修改以下每个y轴的参数,例如上下限。

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 https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建由两条不同数据线的数据组成的 integer? - How to create an integer composed of data from two different data lines? 如何从python中的大数据文件中读取某些行? - How to read certain sets of lines from a big data file in python? 如何从python中的.txt文件中的时间序列数据创建可视化 - How to create visualization from time series data in a .txt file in python 如何将具有不同列中多组数据的行拆分为多行 - How to split a row with multiple sets of data in different columns into mutiple rows 如何读取多个数据集,并创建带有年份列的单个 dataframe - How to read multiple data sets, and create a single dataframe with a year column 如何绘制来自两个不同数据集的数据 - How to plot data from two different data sets 如何使用Python在不同文件的单个图形中创建多行图表? - How create Multiple Lines Chart with Python in a single graphic from different files? 如何从一组元组创建多个集 - How can i create multiple sets from a set of tuples 将文本文件中的行附加到列表中时,会将整行设置为字符串。 如何识别文本文件中的不同数据类型? - When appending lines from a text file into a list, it sets the whole line to a string. How do I recognise different data types inside a text file? 如何从 python 中的 splunk 统计数据创建可视化? - How to create visualization from splunk stats in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM