赞
踩
这两天数学建模中需要画个散点图,索性就把代码发上来吧,帖子嘛~当然是多多益善喽
数据是一千组x,y坐标数据
打印下head
E:\Anaconda3\python.exe E:/Anaconda3/Lib/site-packages/tensorflow/examples/tutorials/mnist/剑魔.py
0 1 2 3 4
0 person_ID pickup_x pickup_y dropoff_x dropoff_y
1 p0001 23 30.2029 22.6848 28.5
2 p0002 21 25.5917 21.3687 24.5
3 p0003 21.3153 25.5 22.2491 24
4 p0004 20.2052 25.5 18.6191 21.5
0 1 2
0 taxi_ID now_x now_y
1 t0001 20.8034 25.0000
2 t0002 22.7184 24.5000
3 t0003 25.5000 27.3651
4 t0004 26.0000 32.0301
上代码:
import pandas as pd import matplotlib.pyplot as plt # 加载数据 data = pd.read_csv('C:/Users/Administrator/Desktop/A题/requests.csv', header=None) taxi = pd.read_csv('C:/Users/Administrator/Desktop/A题/taxi.csv', header=None) print(data.head()) print(taxi.head()) # 上车地点坐标数据 x_up = data.loc[1:, 1:1].values x_up_list = list(x_up) y_up = data.loc[1:, 2:2].values y_up_list = list(y_up) # 下车地点坐标数据 x_down = data.loc[1:, 3:3].values x_down_list = list(x_down) y_down = data.loc[1:, 4:].values y_down_list = list(y_down) # 空车位置坐标数据 x_taxi = taxi.loc[1:, 1:1].values x_taxi_list = list(x_taxi) y_taxi = taxi.loc[1:, 2:2].values y_taxi_list = list(y_taxi) fig = plt.figure(0) plt.xlabel('x') plt.ylabel('y') plt.title('requests') plt.scatter(x_up_list, y_up_list, c='red', alpha=1, marker='+', label='pickup') # c='red'定义为红色,alpha是透明度,marker是画的样式 plt.scatter(x_down_list, y_down_list, c='blue', alpha=1, marker='+', label='dropout') # c='blue'定义为蓝色 plt.scatter(x_taxi_list, y_taxi_list, c='green', alpha=1, marker='+', label='Taxi') # c='green'定义为绿色 plt.grid(True) plt.legend(loc='best') plt.show()
跟画折线、条形图啊其实就一回事,就是就是换了个画图的函数plt.scatter()
局部放大后:
**划重点。。。**图中的散点我用的是+画出来的,当然可以有很多很多中形状可以画
上面代码中数据太多,为了更好说明我取50个随机坐标作演示。
import matplotlib.pyplot as plt
import numpy as np
n = 50
# 随机产生50个0~2之间的x,y坐标
x = np.random.rand(n)*2
y = np.random.rand(n)*2
colors = np.random.rand(n) # 随机产生50个0~1之间的颜色值
area = np.pi * (10 * np.random.rand(n))**2 # 点的半径范围:0~10
# 画散点图
plt.scatter(x, y, s=area, c=colors, alpha=0.5, marker=(9, 3, 30))
plt.show()
样式汇总:
marker=(9, 3, 30)
marker='.'
marker=','
marker='o'
marker='v'
marker='^'
marker='<'
marker='>'
marker='1'
marker='2'
marker='3'
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。