绘制多个散点图 pandas [英] Plotting multiple scatter plots pandas

查看:117
本文介绍了绘制多个散点图 pandas 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为绘制多个图形存在很多问题,但并非专门针对这种情况,如下所示.

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

pandas文档说要重复绘图方法"以在单个轴上绘制多个列组.但是,这对于3个或更多列组将如何工作?例如,如果我们定义第三列:

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:

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

此bx会传递到哪里?

此外,如果绘图是同一图形,则x轴是否应始终为'a'或'c'?但是文档有2个不同的x轴:'a''c'

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'

推荐答案

此bx会传递到哪里?

Where would this bx be passed into?

您应该重复第二个呼叫plot,而不是第一个,因此不需要bx.

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

详细信息:plot对可选的ax自变量赋税.这是它绘制的轴.如果未提供参数,则该函数将创建一个新的图和轴.另外,该轴由该函数返回,因此可以将其重新用于进一步的绘制操作.的想法是 not 不能将ax参数传递给对plot的第一次调用,并在所有后续调用中使用返回的轴.

In detail: plot taxes an optional ax argument. 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 not to pass an ax argument to the first call to plot and use the returned axes in all subsequent calls.

您可以验证每个绘图调用返回的轴均与传递的轴相同:

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

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

如果图是同一张图,那么x轴不应该始终是'a'或'c'吗?

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

不一定.将不同的列放在相同的轴上是否有意义取决于它们所代表的数据.例如,如果a是收入,而c是支出,则将两者置于相同的货币"轴是有意义的.相反,如果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 a was income and c was expenditures it would make sense to put both on the same 'money' axis. In contrast, if a was number of peas and c was voltage they should probably not be on the same axis.

这篇关于绘制多个散点图 pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆