python画图之散点图sactter函数详解,基本散列点绘制
2017-02-13 10:47
621 查看
原文地址:http://blog.csdn.net/u013634684/article/details/49646311 http://blog.csdn.net/abcjennifer/article/details/19848269 http://blog.csdn.net/dataningwei/article/details/53619534
最近开始学习Python编程,遇到scatter函数,感觉里面的参数不知道什么意思于是查资料,最后总结如下:
1、scatter函数原型
2、其中散点的形状参数marker如下:
3、其中颜色参数c如下:
4、基本的使用方法如下:
[python] view
plain copy
#导入必要的模块
import numpy as np
import matplotlib.pyplot as plt
#产生测试数据
x = np.arange(1,10)
y = x
fig = plt.figure()
ax1 = fig.add_subplot(111)
#设置标题
ax1.set_title('Scatter Plot')
#设置X轴标签
plt.xlabel('X')
#设置Y轴标签
plt.ylabel('Y')
#画散点图
ax1.scatter(x,y,c = 'r',marker = 'o')
#设置图标
plt.legend('x1')
#显示所画的图
plt.show()
结果如下:
5、当scatter后面参数中数组的使用方法,如s,当s是同x大小的数组,表示x中的每个点对应s中一个大小,其他如c,等用法一样,如下:
(1)、不同大小
[python] view
plain copy
#导入必要的模块
import numpy as np
import matplotlib.pyplot as plt
#产生测试数据
x = np.arange(1,10)
y = x
fig = plt.figure()
ax1 = fig.add_subplot(111)
#设置标题
ax1.set_title('Scatter Plot')
#设置X轴标签
plt.xlabel('X')
#设置Y轴标签
plt.ylabel('Y')
#画散点图
sValue = x*10
ax1.scatter(x,y,s=sValue,c='r',marker='x')
#设置图标
plt.legend('x1')
#显示所画的图
plt.show()
(2)、不同颜色
[python] view
plain copy
#导入必要的模块
import numpy as np
import matplotlib.pyplot as plt
#产生测试数据
x = np.arange(1,10)
y = x
fig = plt.figure()
ax1 = fig.add_subplot(111)
#设置标题
ax1.set_title('Scatter Plot')
#设置X轴标签
plt.xlabel('X')
#设置Y轴标签
plt.ylabel('Y')
#画散点图
cValue = ['r','y','g','b','r','y','g','b','r']
ax1.scatter(x,y,c=cValue,marker='s')
#设置图标
plt.legend('x1')
#显示所画的图
plt.show()
结果:
(3)、线宽linewidths
[python] view
plain copy
#导入必要的模块
import numpy as np
import matplotlib.pyplot as plt
#产生测试数据
x = np.arange(1,10)
y = x
fig = plt.figure()
ax1 = fig.add_subplot(111)
#设置标题
ax1.set_title('Scatter Plot')
#设置X轴标签
plt.xlabel('X')
#设置Y轴标签
plt.ylabel('Y')
#画散点图
lValue = x
ax1.scatter(x,y,c='r',s= 100,linewidths=lValue,marker='o')
#设置图标
plt.legend('x1')
#显示所画的图
plt.show()
注: 这就是scatter基本的用法。
Python 中常用画图工具matplotlib.pyplot工具使用实验。
代码:
[python] view
plain copy
print?
from sklearn.datasets.samples_generator import make_blobs
import matplotlib.pyplot as plt
X, y = make_blobs(n_samples=100, centers=3, n_features=2,random_state=0)
y=y+1;
# label
plt.figure(1)
ax=plt.subplot(121)
plt.scatter(X[:,0],X[:,1])
ax.set_title('No lable')
ax=plt.subplot(122)
plt.scatter(X[:,0],X[:,1],y*30,y*30)
ax.set_title('Have lable')
#
plt.figure(2)
ax=plt.subplot(111)
id=(y==1)
plt.scatter(X[id,0],X[id,1],s=20,color='b')
id=(y==2)
plt.scatter(X[id,0],X[id,1],s=50,color='r')
id=(y==3)
plt.scatter(X[id,0],X[id,1],s=70,color='g')
plt.show()
显示结果:
fiugre1
figure2
用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编程,遇到scatter函数,感觉里面的参数不知道什么意思于是查资料,最后总结如下:
1、scatter函数原型
2、其中散点的形状参数marker如下:
3、其中颜色参数c如下:
4、基本的使用方法如下:
[python] view
plain copy
#导入必要的模块
import numpy as np
import matplotlib.pyplot as plt
#产生测试数据
x = np.arange(1,10)
y = x
fig = plt.figure()
ax1 = fig.add_subplot(111)
#设置标题
ax1.set_title('Scatter Plot')
#设置X轴标签
plt.xlabel('X')
#设置Y轴标签
plt.ylabel('Y')
#画散点图
ax1.scatter(x,y,c = 'r',marker = 'o')
#设置图标
plt.legend('x1')
#显示所画的图
plt.show()
结果如下:
5、当scatter后面参数中数组的使用方法,如s,当s是同x大小的数组,表示x中的每个点对应s中一个大小,其他如c,等用法一样,如下:
(1)、不同大小
[python] view
plain copy
#导入必要的模块
import numpy as np
import matplotlib.pyplot as plt
#产生测试数据
x = np.arange(1,10)
y = x
fig = plt.figure()
ax1 = fig.add_subplot(111)
#设置标题
ax1.set_title('Scatter Plot')
#设置X轴标签
plt.xlabel('X')
#设置Y轴标签
plt.ylabel('Y')
#画散点图
sValue = x*10
ax1.scatter(x,y,s=sValue,c='r',marker='x')
#设置图标
plt.legend('x1')
#显示所画的图
plt.show()
(2)、不同颜色
[python] view
plain copy
#导入必要的模块
import numpy as np
import matplotlib.pyplot as plt
#产生测试数据
x = np.arange(1,10)
y = x
fig = plt.figure()
ax1 = fig.add_subplot(111)
#设置标题
ax1.set_title('Scatter Plot')
#设置X轴标签
plt.xlabel('X')
#设置Y轴标签
plt.ylabel('Y')
#画散点图
cValue = ['r','y','g','b','r','y','g','b','r']
ax1.scatter(x,y,c=cValue,marker='s')
#设置图标
plt.legend('x1')
#显示所画的图
plt.show()
结果:
(3)、线宽linewidths
[python] view
plain copy
#导入必要的模块
import numpy as np
import matplotlib.pyplot as plt
#产生测试数据
x = np.arange(1,10)
y = x
fig = plt.figure()
ax1 = fig.add_subplot(111)
#设置标题
ax1.set_title('Scatter Plot')
#设置X轴标签
plt.xlabel('X')
#设置Y轴标签
plt.ylabel('Y')
#画散点图
lValue = x
ax1.scatter(x,y,c='r',s= 100,linewidths=lValue,marker='o')
#设置图标
plt.legend('x1')
#显示所画的图
plt.show()
注: 这就是scatter基本的用法。
Python 中常用画图工具matplotlib.pyplot工具使用实验。
代码:
[python] view
plain copy
print?
from sklearn.datasets.samples_generator import make_blobs
import matplotlib.pyplot as plt
X, y = make_blobs(n_samples=100, centers=3, n_features=2,random_state=0)
y=y+1;
# label
plt.figure(1)
ax=plt.subplot(121)
plt.scatter(X[:,0],X[:,1])
ax.set_title('No lable')
ax=plt.subplot(122)
plt.scatter(X[:,0],X[:,1],y*30,y*30)
ax.set_title('Have lable')
#
plt.figure(2)
ax=plt.subplot(111)
id=(y==1)
plt.scatter(X[id,0],X[id,1],s=20,color='b')
id=(y==2)
plt.scatter(X[id,0],X[id,1],s=50,color='r')
id=(y==3)
plt.scatter(X[id,0],X[id,1],s=70,color='g')
plt.show()
显示结果:
fiugre1
figure2
用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):
基本散列点绘制
from matplotlib import pyplot as plt import numpy as np # Generating a Gaussion dataset: # creating random vectors from the multivariate normal distribution # given mean and covariance mu_vec1 = np.array([0,0]) cov_mat1 = np.array([[2,0],[0,2]]) x1_samples = np.random.multivariate_normal(mu_vec1, cov_mat1, 100) x2_samples = np.random.multivariate_normal(mu_vec1+0.2, cov_mat1+0.2, 100) x3_samples = np.random.multivariate_normal(mu_vec1+0.4, cov_mat1+0.4, 100) # x1_samples.shape -> (100, 2), 100 rows, 2 columns plt.figure(figsize=(8,6)) plt.scatter(x1_samples[:,0], x1_samples[:,1], marker='x', color='blue', alpha=0.7, label='x1 samples') plt.scatter(x2_samples[:,0], x1_samples[:,1], marker='o', color='green', alpha=0.7, label='x2 samples') plt.scatter(x3_samples[:,0], x1_samples[:,1], marker='^', color='red', alpha=0.7, label='x3 samples') plt.title('Basic scatter plot') plt.ylabel('variable X') plt.xlabel('Variable Y') plt.legend(loc='upper right') plt.show()
带标签
import matplotlib.pyplot as plt x_coords = [0.13, 0.22, 0.39, 0.59, 0.68, 0.74, 0.93] y_coords = [0.75, 0.34, 0.44, 0.52, 0.80, 0.25, 0.55] fig = plt.figure(figsize=(8,5)) plt.scatter(x_coords, y_coords, marker='s', s=50) for x, y in zip(x_coords, y_coords): plt.annotate( '(%s, %s)' %(x, y), xy=(x, y), xytext=(0, -10), textcoords='offset points', ha='center', va='top') plt.xlim([0,1]) plt.ylim([0,1]) plt.show()
相关文章推荐
- 【数字的可视化:python画图之散点图sactter函数详解】
- 【数字的可视化:python画图之散点图sactter函数详解】
- 【数字的可视化:python画图之散点图sactter函数详解】
- 【数字的可视化:python画图之散点图sactter函数详解】
- 【数字的可视化:python画图之散点图sactter函数详解】
- 【数字的可视化:python画图之散点图sactter函数详解】
- 详解Python网络爬虫功能的基本写法
- Python:使用pycha快速绘制办公常用图(饼图、垂直直方图、水平直方图、散点图等七种图形)
- Python实现的数据结构与算法之基本搜索详解
- 绘制基本图形和线型(StrokeStyle)的设置详解
- python数字图像处理(12):基本图形的绘制
- 详解Python网络爬虫功能的基本写法
- Direct2D教程II——绘制基本图形和线型(StrokeStyle)的设置详解
- [原创]python之简单计算器(超详解,只有基本功能+-*/,还有括号处理)
- python数字图像处理(12):基本图形的绘制
- Python画图基本方法总结
- Python详解(一)Python的基本语法,简单数据类型,和while,for循环控制,ifesle控制
- Python:使用pycha快速绘制办公常用图(饼图、垂直直方图、水平直方图、散点图等七种图形)
- 详解Python编程中基本的数学计算使用
- matlab画图入门篇--各种基本图形绘制的函数与实例【转载】