Python 绘制多个散点图熊猫

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43061768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:28:00  来源:igfitidea点击:

Plotting multiple scatter plots pandas

pythonpandasmatplotlibscatter-plot

提问by Amoroso

I think there are many questions on plotting multiple graphs but not specifically for this case as shown below.

我认为绘制多个图形有很多问题,但不是专门针对这种情况,如下所示。

The pandas documentation says to 'repeat plot method' to plot multiple column groups in a single axes. However, how would this work for 3 or more column groups? For example if we define a third column:

熊猫文档说“重复绘图方法”在单个轴上绘制多个列组。但是,这如何适用于 3 个或更多列组?例如,如果我们定义第三列:

bx = df.plot(kind='scatter', x='a',y='f',color = 'Green',label ='f')

Where would this bx be passed into?

这个 bx 会被传递到哪里?

Also, if the plot is the same graph, shouldn't the x-axis be consistently either 'a' or 'c'? but the documentation has 2 different x axis: 'a' and'c'

另外,如果绘图是同一张图,x 轴不应该始终是 'a' 或 'c' 吗?但文档有 2 个不同的 x 轴:'a''c'

enter image description here

在此处输入图片说明

采纳答案by kazemakase

Where would this bx be passed into?

这个 bx 会被传递到哪里?

You ought to repeat the second call to plot, not the first, so there is no need for bx.

您应该重复第二次调用plot,而不是第一个,因此不需要bx

In detail: plottaxes an optional axargument. This is the axes it draws into. If the argument is not provided the function creates a new plot and axes. In addition, the axes is returned by the function so it can be reused for further drawing operations. The idea is notto pass an axargument to the first call to plotand use the returned axes in all subsequent calls.

详细说明:plot对可选ax参数征税。这是它绘制的轴。如果未提供参数,该函数会创建一个新的图和轴。此外,轴由函数返回,因此可以重用于进一步的绘图操作。这个想法不是ax参数传递给第一次调用plot并在所有后续调用中使用返回的轴。

You can verify that each call to plot returns the same axes that it got passed:

您可以验证对 plot 的每次调用是否返回与它传递的轴相同的轴:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(100, 6), columns=['a', 'b', 'c', 'd', 'e', 'f'])


ax1 = df.plot(kind='scatter', x='a', y='b', color='r')    
ax2 = df.plot(kind='scatter', x='c', y='d', color='g', ax=ax1)    
ax3 = df.plot(kind='scatter', x='e', y='f', color='b', ax=ax1)

print(ax1 == ax2 == ax3)  # True

enter image description here

在此处输入图片说明

Also, if the plot is the same graph, shouldn't the x-axis be consistently either 'a' or 'c'?

另外,如果绘图是同一张图,x 轴不应该始终是“a”或“c”吗?

Not necessarily. If it makes sense to put different columns on the same axes depends on what data they represent. For example, if awas income and cwas expenditures it would make sense to put both on the same 'money' axis. In contrast, if awas number of peas and cwas voltage they should probably not be on the same axis.

不必要。将不同的列放在相同的轴上是否有意义取决于它们代表什么数据。例如,如果a是收入和c是支出,那么将两者放在同一个“金钱”轴上是有意义的。相比之下,如果a是豌豆数和c电压,它们可能不应该在同一轴上。

回答by ImportanceOfBeingErnest

You can plot any column against any column you like. Whether that makes sense you have to decide for yourself. E.g. plotting a column denoting time on the same axis as a column denoting distance may not make sense, but plotting two columns which both contain distance on the same axis, is fine.

您可以针对您喜欢的任何列绘制任何列。这是否有意义,您必须自己决定。例如,在同一轴上绘制表示时间的列与表示距离的列可能没有意义,但在同一轴上绘制包含距离的两列就可以了。

In order to specify that a certin plot should be on an already existing axes (ax), you'd specify the axkeyword as seen in the documentation. Of couse you can create several plots on the same axes.

为了指定 certin 图应该在已经存在的轴 ( ax) 上,您可以指定ax文档中看到的关键字。当然,您可以在同一轴上创建多个图。

ax = df.plot(kind="scatter", x="x",y="a", color="b", label="a vs. x")
df.plot(x="x",y="b", color="r", label="b vs. x", ax=ax)
df.plot( x="x",y="c", color="g", label="c vs. x", ax=ax)

A complete example:

一个完整的例子:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,6.3, 50)
a = (np.sin(x)+1)*3
b = (np.cos(x)+1)*3
c = np.ones_like(x)*3
d = np.exp(x)/100.
df = pd.DataFrame({"x":x, "a":a, "b":b, "c":c, "d":d})

ax = df.plot(kind="scatter", x="x",y="a", color="b", label="a vs. x")
df.plot(x="x",y="b", color="r", label="b vs. x", ax=ax)
df.plot( x="x",y="c", color="g", label="c vs. x", ax=ax)
df.plot( x="d",y="x", color="orange", label="b vs. d", ax=ax)
df.plot( x="a",y="x", color="purple", label="x vs. a", ax=ax)

ax.set_xlabel("horizontal label")
ax.set_ylabel("vertical label")
plt.show()

enter image description here

在此处输入图片说明

回答by user2739472

Inside the pyvizverse there is a library called hvplotwhich provides very nice high-level plotting functionality (on top of holoviews) that works out of the box with Pandas:

pyvizverse 中有一个名为的库hvplot,它提供了非常好的高级绘图功能(在 之上holoviews),它与 Pandas 一起开箱即用:

import numpy as np
import hvplot.pandas

df = pd.DataFrame(np.random.randn(100, 6), columns=['a', 'b', 'c', 'd', 'e', 'f'])

df.hvplot(x='a', y=['b', 'c', 'd', 'e'], kind='scatter')

enter image description here

在此处输入图片说明