【数据可视化】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.

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

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 的相关参数。

 

  • 9
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值