简体   繁体   English

如何在 Python 中 plot 相关图表?

[英]How to plot a correlation chart in Python?

I have a dataframe like this:我有一个像这样的 dataframe:

在此处输入图像描述

I want to plot a correlation chart between variables "BTC" and "B3" similar to this one here:我想 plot 变量“BTC”和“B3”之间的相关图表类似于这里的这个:

https://charts.coinmetrics.io/correlations/ https://charts.coinmetrics.io/correlations/

Can anyone point me some material where I can study how to do it?任何人都可以指出一些我可以研究如何做的材料吗?

Numpy and matplotlib are libraries that address your needs, see their docs related to your use case: Numpy 和 matplotlib 是满足您需求的库,请参阅与您的用例相关的文档:

https://numpy.org/doc/1.20/reference/generated/numpy.correlate.html#numpy.correlate https://numpy.org/doc/1.20/reference/generated/numpy.correlate.html#numpy.correlate

https://matplotlib.org/stable/tutorials/index.html https://matplotlib.org/stable/tutorials/index.html

These are well known and widely used but they are not part of Python standard library.这些是众所周知的并且被广泛使用,但它们不是 Python 标准库的一部分。

Some would use also pandas, but IMHO it is not needed here at all.有些人还会使用 pandas,但恕我直言,这里根本不需要。

Also you would like to load your data into your app.您还想将数据加载到您的应用程序中。 Use something like csv in standard library.在标准库中使用 csv 之类的东西。 Or use requests and json if you can call some API to get your data.或者使用 requests 和 json 如果您可以调用一些 API 来获取您的数据。

To plot a correlation chart in Python:到 plot Python 中的相关图:

import matplotlib.pyplot as plt

l = ['id1','id2','id3','id4']
y1 = [3,5,6,2]
y2 = [4,2,5,1]

fig,ax = plt.subplots()
ax.scatter(y1, y2)

for i, txt in enumerate(l):
    ax.annotate(txt, (y1[i], y2[i]))

plt.show()

Output: Output:

在此处输入图像描述

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

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