python下的matplotlib、pandas做散点图的逐步深入分析
1.代码1:
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()
2.图1
3.且报错:
MatplotlibDeprecationWarning: Setting a circle marker using `(..., 3)` is deprecated
since Matplotlib 3.0, and support for it will be removed in 3.2. Directly pass 'o' instead.
plt.scatter(x, y, s=area, c=colors, alpha=0.5, marker=(9, 3, 30))
4.修改后:
#注意marker=('o'),里面是o字母小写的o不是0(零)
plt.scatter(x, y, s=area, c=colors, alpha=0.5, marker=('o'))
#就OK了
5.代码2:
import matplotlib.pyplot as plt import numpy as np xv=[15,25,35,45,55,88,43,67,56,77,79,32,11,54,32] n=len(xv) #n就是列表x的个数,用len就可以测 yv = [x * np.random.rand() for x in xv] colors = np.random.rand(n) # 随机产生50个0~1之间的颜色值 area = np.pi * (10 * np.random.rand(n))**2 # 点的半径范围:0~10 #注意marker=('o'),里面是o字母小写的o不是0(零) plt.scatter(xv, yv, s=area, c=colors, alpha=0.5, marker=('o')) plt.show()
6.图2
7.一个点的散点图
代码3:
import matplotlib.pyplot as plt plt.scatter(3,3, marker=('o')) plt.show()
图3
8.两组数据的散点图
代码4:
import matplotlib.pyplot as plt x=[3,5,9,7,5,8,6,1,2,4] y=[4,8,1,6,2,3,7,5,9,5] plt.scatter(x,y, marker=('o')) plt.show()
图4
9.读取excel的数据做散点图
代码5:
import matplotlib.pyplot as plt import pandas as pd import xlrd #读取excel #注意这里的excel表格只有一张表单,默认sheet1和文件地址默认根目录下 #df=pd.read_excel('xxx/xxx/xxx/22.xls',sheet_name='sheet2') #比如格式自定路径下的文件和表单2 df=pd.read_excel('22.xls') datax=df['x'] #读取x列 datay=df['y'] #读取y列 ''' 类似这样的数据 x=[3,5,9,7,5,8,6,1,2,4] y=[4,8,1,6,2,3,7,5,9,5] ''' #print("读取指定行的数据:\n{0}".format(datay)) plt.scatter(datax,datay, marker=('o')) #自定义标签和标题 plt.xlabel('x-value') plt.ylabel('y-label') plt.title('scatter') plt.show()
图5和图4一样,略