【数据可视化】Pandas画散点图

pandas.DataFrame.plot.scatter

使用函数:DataFrame.plot.scatter(xys=Nonec=None**kwds)

功能:创建具有不同标记点大小和颜色的散点图

每个点的坐标由两个数据帧列定义,实心圆用于表示每个点。这种图可用于查看两个变量之间的复杂相关性。点可以是例如自然2D坐标,例如地图中的经度和纬度,或者通常可以是可以相对于彼此绘制的任何一对度量。

参数:

x : int or str

The column name or column position to be used as horizontal coordinates for each point.

用作每个点的水平坐标的列名称或列位置。

y : int or str

The column name or column position to be used as vertical coordinates for each point.

用作每个点的垂直坐标的列名称或列位置。

扫描二维码关注公众号,回复: 2745177 查看本文章

s : scalar or array_like, optional

The size of each point. Possible values are:

每个点的大小。可能的值是:

  • A single scalar so all points have the same size.

        单个标量,所有点都具有相同的大小。

  • A sequence of scalars, which will be used for each point’s size recursively. For instance, when passing [2,14] all points size will be either 2 or 14, alternatively.

        一系列标量,将递归地用于每个点的大小。例如,当传递[2,14]时,所有点的大小将为2或14,或者。

c : str, int or array_like, optional

The color of each point. Possible values are:

每个点的颜色。可能的值是:

  • A single color string referred to by name, RGB or RGBA code, for instance ‘red’ or ‘#a98d19’.

       由名称,RGB或RGBA代码引用的单个颜色字符串,例如“red”或“#a98d19”。

  • A sequence of color strings referred to by name, RGB or RGBA code, which will be used for each point’s color recursively. For intance [‘green’,’yellow’] all points will be filled in green or yellow, alternatively.

        由名称、RGB或RGBA代码引用的一系列颜色字符串,它们将被递归地用于每个点的颜色。对于“绿色”,“黄色”的所有点都将被绿色或黄色填充。

  • A column name or position whose values will be used to color the marker points according to a colormap.

        列名称或位置,其值将用于根据色彩图对标记点着色。

**kwds

Keyword arguments to pass on to pandas.DataFrame.plot().

Returns:

axes : matplotlib.axes.Axes or numpy.ndarray of them

例子

导入库:

import pandas as pd
import numpy as np

简单的散点图

df = pd.DataFrame(np.random.rand(50, 4), columns=['a', 'b', 'c', 'd'])
df.plot.scatter(x='a', y='b')

散点分组

ax = df.plot.scatter(x='a', y='b', color='b', label='Group 1')
df.plot.scatter(x='c', y='d', color='g', label='Group 2',ax=ax)

散点的点形状

df.plot.scatter(x='a', y='b',marker='x')

常用的Marker有:

Marker类型 散点形状
"."(点)
","(逗号)、"s" 像素(小正方形)
"o"
"V" 倒三角
"^" 正三角
"<" 左三角
">" 右三角
"*" 星星
"x" 叉号
"+" 加号

散点颜色

df.plot.scatter(x='a', y='b', c='c', colormap='viridis')

常用颜色:

参数 颜色
b/blue 蓝色
c/cyan 蓝绿色
g/green 绿色
k/black 黑色
m/magenta 品红/分红
r/red 红色
w/white 白色
y/yellow 黄色

剩下的其余参数,详情请见matplotlib scatter 文档和matplotlib.pyplot 的相关参数。

猜你喜欢

转载自blog.csdn.net/ChenVast/article/details/81631130
今日推荐