Python数据可视化——散点图
2017-02-10 10:48
561 查看
转载出处:http://blog.csdn.net/abcjennifer/article/details/19848269
PS: 翻了翻草稿箱, 发现居然存了一篇去年2月的文章。。。虽然naive,还是发出来吧。。。
本文记录了Python中的数据可视化——散点图scatter,
令x作为数据(50个点,每个30维),我们仅可视化前两维。labels为其类别(假设有三类)。
这里的x就用random来了,具体数据具体分析。
label设定为[1:20]->1, [21:35]->2, [36:50]->3,(python中数组连接方法:先强制转为list,用+,再转回array)
用matplotlib的scatter绘制散点图,legend和matlab中稍有不同,详见代码。
[python] view
plain copy
x = rand(50,30)
from numpy import *
import matplotlib
import matplotlib.pyplot as plt
#basic
f1 = plt.figure(1)
plt.subplot(211)
plt.scatter(x[:,1],x[:,0])
# with label
plt.subplot(212)
label = list(ones(20))+list(2*ones(15))+list(3*ones(15))
label = array(label)
plt.scatter(x[:,1],x[:,0],15.0*label,15.0*label)
# with legend
f2 = plt.figure(2)
idx_1 = find(label==1)
p1 = plt.scatter(x[idx_1,1], x[idx_1,0], marker = 'x', color = 'm', label='1', s = 30)
idx_2 = find(label==2)
p2 = plt.scatter(x[idx_2,1], x[idx_2,0], marker = '+', color = 'c', label='2', s = 50)
idx_3 = find(label==3)
p3 = plt.scatter(x[idx_3,1], x[idx_3,0], marker = 'o', color = 'r', label='3', s = 15)
plt.legend(loc = 'upper right')
result:
figure(1):
figure(2):
PS: 翻了翻草稿箱, 发现居然存了一篇去年2月的文章。。。虽然naive,还是发出来吧。。。
本文记录了Python中的数据可视化——散点图scatter,
令x作为数据(50个点,每个30维),我们仅可视化前两维。labels为其类别(假设有三类)。
这里的x就用random来了,具体数据具体分析。
label设定为[1:20]->1, [21:35]->2, [36:50]->3,(python中数组连接方法:先强制转为list,用+,再转回array)
用matplotlib的scatter绘制散点图,legend和matlab中稍有不同,详见代码。
[python] view
plain copy
x = rand(50,30)
from numpy import *
import matplotlib
import matplotlib.pyplot as plt
#basic
f1 = plt.figure(1)
plt.subplot(211)
plt.scatter(x[:,1],x[:,0])
# with label
plt.subplot(212)
label = list(ones(20))+list(2*ones(15))+list(3*ones(15))
label = array(label)
plt.scatter(x[:,1],x[:,0],15.0*label,15.0*label)
# with legend
f2 = plt.figure(2)
idx_1 = find(label==1)
p1 = plt.scatter(x[idx_1,1], x[idx_1,0], marker = 'x', color = 'm', label='1', s = 30)
idx_2 = find(label==2)
p2 = plt.scatter(x[idx_2,1], x[idx_2,0], marker = '+', color = 'c', label='2', s = 50)
idx_3 = find(label==3)
p3 = plt.scatter(x[idx_3,1], x[idx_3,0], marker = 'o', color = 'r', label='3', s = 15)
plt.legend(loc = 'upper right')
result:
figure(1):
figure(2):
相关文章推荐
- [置顶] 【python 数据可视化 】饼图,箱线图,条形图,直方图,折线图,散点图
- Python数据可视化编程通过Matplotlib创建散点图代码示例
- Python数据可视化-Matplotlib学习笔记(3)--画散点图
- python数据可视化——散点图实例之随机漫步
- Python数据可视化——散点图
- Python数据可视化之散点图和折线图
- python数据可视化——利用pyplot绘制折线图和散点图
- python数据可视化--散点图以及分类
- Python数据可视化:Matplotlib 直方图、箱线图、条形图、热图、折线图、散点图。。。
- 数据可视化:python画散点图scatter
- Python下的数据处理和机器学习,对数据在线及本地获取、解析、预处理和训练、预测、交叉验证、可视化
- 读书笔记--python数据可视化--001_读取CSV文件数据
- 读书笔记--python数据可视化--005_导出数据到JSON、CSV和EXcel
- 读书笔记--python数据可视化--004_读取JSON数据
- Caffe学习系列(13):数据可视化环境(python接口)配置
- 可视化数据分析(三) 一个简单的散点图的实现
- 【数字的可视化:python画图之散点图sactter函数详解】
- 读书笔记--python数据可视化--006_从数据库导入数据
- 举例讲解Python的Tornado框架实现数据可视化的教程