python数据分析——手动绘制散点图的背景颜色实例

import numpy as np
import pandas as pd
from pandas import Series,DataFrame
import matplotlib.pyplot as plt
 

手动绘制散点图的背景颜色

In [11]:
x = [1,2,3]
y = [6,7,8]
plt.scatter(x,y)
Out[11]:
<matplotlib.collections.PathCollection at 0x1f8d3a63c50>
 
 
  • 设定一组x,y
In [12]:
x = np.linspace(1,3,num=100)
y = np.linspace(6,8,num=100)
In [14]:
# 返回一个网格矩阵
xx,yy = np.meshgrid(x,y)
In [15]:
xx.shape
Out[15]:
(100, 100)
In [18]:
# 生成两列坐标点
xy = np.c_[xx.reshape(10000,),yy.reshape(10000,)]
xy
Out[18]:
array([[1.        , 6.        ],
       [1.02020202, 6.        ],
       [1.04040404, 6.        ],
       ...,
       [2.95959596, 8.        ],
       [2.97979798, 8.        ],
       [3.        , 8.        ]])
In [19]:
# 取第一列
xy[:,0]
Out[19]:
array([1.        , 1.02020202, 1.04040404, ..., 2.95959596, 2.97979798,
       3.        ])
In [21]:
# xy中第一列为scatter中第一个参数,第二列为scatter中第二个参数
plt.scatter(xy[:,0],xy[:,1])
plt.scatter([1,2,3],[6,7,8])
Out[21]:
<matplotlib.collections.PathCollection at 0x1f8d3601fd0>
 
 

meshgrid举例

a = np.array([1,2,3])
b = np.array([4,5,6])
In [8]:
aa,bb = np.meshgrid(a,b)
In [9]:
display(aa,bb)
array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])
array([[4, 4, 4],
       [5, 5, 5],
       [6, 6, 6]])
In [12]:
display(aa.reshape(9,),bb.reshape(9,))
array([1, 2, 3, 1, 2, 3, 1, 2, 3])
array([4, 4, 4, 5, 5, 5, 6, 6, 6])
In [13] 
A,B = aa.reshape(9,),bb.reshape(9,)
In [14]:
np.c_[A,B]
Out[14]:
array([[1, 4],
       [2, 4],
       [3, 4],
       [1, 5],
       [2, 5],
       [3, 5],
       [1, 6],
       [2, 6],
       [3, 6]])
posted @ 2019-10-10 15:24  陪伴is最长情的告白  阅读(1631)  评论(0编辑  收藏  举报