-
# 常用图标:散点图 scatter fig = plt.figure() ax = fig.add_subplot(3, 3, 1) # 3行3列第1个子图,创建一个子图 n = 128 X = np.random.normal(0, 1, n) Y = np.random.normal(0, 1, n) T = np.arctan2(Y, X) # 用来上色,arctan的平方(Y/X) # plt.axes([0.05, 0.05, 0.9, 0.9]) # 指定显示范围 ax.scatter(X, Y, s=25, c=T, alpha=.5) # s:size plt.xlim(-1.5*1.1, 1.5*1.1), plt.xticks() # xlim设置横轴的上下限, xticks设置横轴记号 plt.ylim(-1.5*1.1, 1.5*1.1), plt.yticks() plt.axis() plt.title("scatter") plt.xlabel("x") plt.ylabel("y") # bar ax = fig.add_subplot(332) # 3行3列第2个 n = 5 X = np.arange(n) Y1 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n) Y2 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n) ax.bar(X, +Y1, facecolor='#9999ff', edgecolor='purple') ax.bar(X, -Y2, facecolor='#ff9999', edgecolor='green') for x, y in zip(X, Y1): plt.text(x + 0.4, y + 0.05, '%.2f' % y, ha='center', va='bottom') for x, y in zip(X, Y2): plt.text(x + 0.4, -y - 0.05, '%.2f' % y, ha='center', va='top') # Pie fig.add_subplot(333) n = 20 Z = np.ones(n) Z[-1] *= 2 # 最后一个设为2 # explode 每个扇形离中心的距离,labels 将颜色值显示出来 plt.pie(Z, explode=Z * .05, colors=['%f' % (i / float(n)) for i in range(n)], labels=['%.2f' % (i / float(n)) for i in range(n)]) # plt.gca().set_aspect('equal') # 正圆 plt.xticks([]), plt.yticks([]) # 极坐标图 polar fig.add_subplot(334, polar=True) # polar!= True为折线图 n = 20 theta = np.arange(0.0, 2 * np.pi, 2 * np.pi / n) radii = 10 * np.random.rand(n) plt.polar(theta, radii) # 两种方法都可以 # plt.plot(theta, radii) # heatmap 热图 fig.add_subplot(335) from matplotlib import cm data = np.random.rand(3, 3) cmap = cm.Blues # 蓝色系 # interpolation 插值:用离它最近的值做插值,coloarmap, aspect 缩放,vmin 白色,vmax 这里为蓝色 map = plt.imshow(data, interpolation='nearest', cmap=cmap, aspect='auto', vmin=0, vmax=1) # 3D from mpl_toolkits.mplot3d import Axes3D ax = fig.add_subplot(336, projection="3d") ax.scatter(1, 1, 3, s=100) # hot map 热力图 fig.add_subplot(313) def f(x, y): return (1 - x / 2 + x **5 + y ** 3) * np.exp(-x ** 2 - y **2) n = 256 x = np.linspace(-3, 3, n) y = np.linspace(-3, 3, n) X, Y = np.meshgrid(x, y) plt.contourf(X, Y, f(X, Y), 8, alpha=.75, cmap=plt.cm.hot) # cmap 指定热力图颜色 plt.savefig("./data/fig.png", dpi=72) # 以分辨率 72 来保存图片 plt.show()
查看全部 -
#encoding=utf-8 import numpy as np def main(): # line import matplotlib.pyplot as plt x = np.linspace(-np.pi, np.pi, 256, endpoint=True) # -pi和pi之间取256个点,并包含最后一个点 c, s = np.cos(x), np.sin(x) plt.figure(1) # 绘制第一个图 plt.plot(x, c, color="blue", linewidth=1.0, linestyle='-', label="COS", alpha=0.5) plt.plot(x, s, "r*", label="SIN") # r:red, *: 线型 plt.title("Cos & Sin") # 现在显示四周都有边框,如何只显示两条坐标轴呢,需要除去两边边框 ax = plt.gca() # 轴的编辑器 #spines表示四周的线 ax.spines["right"].set_color("none") # 去掉右外框 ax.spines["top"].set_color("none") # 去掉上外框 ax.spines["left"].set_position(("data", 0)) # 设置左边线位置 ax.spines["bottom"].set_position(("data", 0)) ax.xaxis.set_ticks_position("bottom") # 编辑轴上数字,坐标轴的数字显示在横轴下面和纵轴左面 ax.yaxis.set_ticks_position("left") # 编辑轴上数字 # 设置横轴,两个数组,第一个指定横轴标识的位置,第二个为内容 plt.xticks([-np.pi, -np.pi / 2.0, np.pi / 2.0, np.pi], [r'$-\pi$', r'$-\pi/2$', r'$+\pi/2$', r'$+\pi$']) plt.yticks(np.linspace(-1, 1, 5, endpoint=True)) # 直接给y轴标数字即可 for label in ax.get_xticklabels() + ax.get_yticklabels(): # 设置坐标轴数字显示大小和颜色 label.set_fontsize(16) # 设置label的小方块及其背景 face: 背景色,edge: 边缘,alpha:透明度 label.set_bbox(dict(facecolor="purple", edgecolor="None", alpha=0.2)) plt.legend(loc="upper left") # 图例说明,loc指定位置 plt.grid() # 网格线 # plt.axis([-1, 1, -0.5, 1]) # [-1, 1] 横轴显示范围,[-0.5,1] 纵轴显示范围 plt.fill_between(x, np.abs(x)<0.5, c, c>0.5, color="green", alpha=0.25) # 填充两条线间的颜色 t = 1 plt.plot([t, t], [0, np.cos(t)], 'y', linewidth=3, linestyle='--') # 加注释 plt.annotate("cos(1)", xy=(t, np.cos(1)), xycoords="data", xytext=(+10, +30), textcoords="offset points", arrowprops=dict(arrow, connection)) # xy为加的点的位置, xytext加偏移量,textcoords指定是一个相对位置,connectionstyle指定弧度为0.2 plt.show() ''' plt.fill_between(x, y1, y2=0, where=None, interpolate=False, step=None, hold=None, data=None, **kwargs) x - array( length N) 定义曲线的 x 坐标 y1 - array( length N ) or scalar 定义第一条曲线的 y 坐标 y2 - array( length N ) or scalar 定义第二条曲线的 y 坐标 where - array of bool (length N), optional, default: None 排除一些(垂直)区域被填充。 注:我理解的垂直区域,但帮助文档上写的是 horizontal regions ''' # 我的理解:从y1到y2的水平区域 if __name__ == "__main__": main()
查看全部 -
#Basic print(df.head(3)) #head()打印出前几行 print(df.tail(3)) #tail()打印出后几行 print(df.index) #index()打印出索引 print(df.values) #values()打印出values print(df.T) #T 转置 print(df.sort(columns="C")) #第C列的values按从小到大的顺序排序 print(df.sort_index(axis=1,ascending=False)) #将index进行降序排序 print(df.describe()) #打印出所有属性值 的数量 平均值 标准差 最小值 最大值等
查看全部 -
print(df.sort_values(by="C"))
查看全部 -
#20180423
# -*- coding: cp936 -*-
import numpy as np
import matplotlib.pyplot as plt
def run():
#-pi和pi之间取256个点,并包含最后一个点
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
#e为cosx, f为sin(x)
c, d = np.cos(x), np.sin(x)
plt.figure(1) #指定图1
plt.plot(x, c, color='blue', linewidth=1.0, linestyle='-', label="Cos", alpha=0.1)
#*表示线型为由*点构成
plt.plot(x, d, "r*", label='Sin')
plt.title("Cos&Sin")
ax=plt.gca()
#spines表示四周的线
ax.spines["right"].set_color("none")
ax.spines["top"].set_color("none")
#坐标轴起始设置
ax.spines["left"].set_position(("data", 0))
ax.spines["bottom"].set_position(("data", 0))#此处为两个括弧
#20180425
ax.xaxis.set_ticks_position('bottom') #设置x轴的标注位于bottom轴
ax.yaxis.set_ticks_position('left')
plt.yticks(np.linspace(-np.pi,np.pi,5,endpoint=True))
plt.yticks(np.linspace(-1,1,5,endpoint=True))
for label in ax.get_xticklabels()+ax.get_yticklabels():
#改变label的大小
label.set_fontsize(16)
#设置label的小方块及其背景
label.set_bbox(dict(facecolor='white',edgecolor))
#标注sin和cos的图例
plt.legend(loc='upper left')
#显示网格线
plt.grid()
#当x绝对值小于0.5,为真记为1,此范围内从1开始到曲线cosx填充,其余为0开始到曲线cosx填充,c>0.5确定横轴范围x,保证c=cosx>0.5
plt.fill_between(x,np.abs(x)<0.5,c,c>0.5,color='green',alpha=0.1)
plt.show()
run()
查看全部 -
shape看结构,ndim看维度,dtype看数据类型,itemsize看元素字节大小,size看数组元素个数
查看全部 -
#Basic print(df.head(3)) #head()打印出前几行 print(df.tail(3)) #tail()打印出后几行 print(df.index) #index()打印出索引 print(df.values) #values()打印出values print(df.T) #T 转置 print(df.sort(columns="C")) #第C列的values按从小到大的顺序排序 print(df.sort_index(axis=1,ascending=False)) #将index进行降序排序 print(df.describe()) #打印出所有属性值 的数量 平均值 标准差 最小值 最大值等查看全部
-
import numpy as np def main(): #line import matplotlib.pyplot as plt x=np.linspace(-np.pi,np.pi,256,endpoint=True) c,s=np.cos(x),np.sin(x) plt.figure(2) plt.plot(x,c,color="blue",linewidth=1,line,label="COS",alpha=0.5) plt.plot(x,s,"r*",label="SIN") plt.title("sin&cos") ax=plt.gca() ax.spines["right"].set_color("none") ax.spines["top"].set_color("none") ax.spines["left"].set_position(("data",0)) ax.spines["bottom"].set_position(("data",0)) ax.xaxis.set_ticks_position("bottom") ax.yaxis.set_ticks_position("left")查看全部
-
Python 数据分析大家族: numpy: 数据结构基础 scipy: 强大的科学计算方法(矩阵分析等) matplotlib:丰富的可视化套件 pandas: 基础数据分析套件 scikit-learn :强大的数据分析建模库 keras:人工神经网络查看全部
-
官网的下载速度特别慢 可以用清华镜像查看全部
-
print(np.zeros([2,4]))#输出元素都为0的2行4列数组 print(np.ones([3,5]))#输出元素都为1 的2行4列数组 print("Rand:")#生成随机数 print(np.random.rand(2, 4))#输出2行4列的随机数组 print(np.random.rand())#生成一个随机数 print(np.random.randint(1, 14, 5))#在1到14之间生成5个随机数 print("RandInt:")#生成随机整数 print(np.random.randint(1,10))#生成1个1到10之间的随机整数 print(np.random.randint(1,10,3))#生成3个1到10之间的随机整数[a b c] print("Randn:")#生成正态分布的随机数 print(np.random.randn())#生成一个标准正态分布的随机数 print(np.random.randn(2, 4))#生成2行4列标准正态分布的随机数 print("Choice:")#从给定数组中随机选取一个数 print(np.random.choice([10, 20, 30]))#从给定数组中随机选取一个打印出来 print("Distribute:")#生成数学上某种指定分布 print(np.random.beta(1, 10, 100))#生成从1到10一共100个beta分布的数
查看全部 -
import pandas as pd
Series是pandas中基础的数据结构
date_range('20200202',periods=8)依次生成8天,天数作为主键,行标签
DataFrame(数据,index主键也就是行标签,colums也就是列标签)
类似excel
查看全部 -
import numpy as np def main(): ##积分 from scipy.integrate import quad,dblquad,nquad#引入积分模块 print (quad(lambda x:np.exp(-x),0,np.inf))#0到无穷大的积分,返回的是一个值和误差 print (dblquad(lambda t,x:np.exp(-x*t)/t**3,0,np.inf,lambda x:1,lambda x:np.inf))#二元积分,两个参数t,x,先定义t的取值范围(常数),再定义x的取值范围(相当于t的函数) def f(x,y):##n维积分 return x*y def bound_y():#定义y的边界 return [0,0.5] def boundx(y):#定义x的边界 return [0,1-2*y] print (nquad(f,[boundx,bound_y])) if __name__=='__main__': main()
查看全部 -
#导包 import numpy as np import matplotlib.pyplot as plt
#横轴设置 x = np.linspace(-np.pi,np.pi,512,endpoint=True)
#构建sin,cos函数 c, s = np.cos(x), np.sin(x)
#绘图 plt.figure() plt.plot(x,s) plt.plot(x,c)
plt.show()
查看全部 -
# 2. Some 常用 Arrays print np.zeros([2, 4]) print np.ones([3, 5]) print "Rand:" print np.random.rand(2, 4) # 2行4列随机数数组 print np.random.rand() # 产生一个随机数 print "RandInt:" print np.random.randint(1, 10, 3) # 参数为随机整数的范围[1,10], 3个 print "Randn:" # 标准动态分布的随机数 print np.random.randn(2, 4) # 2行4列 print np.random.choice([10, 20, 30]) # 列表中随机一个产生 print "Distribute:" # 分布 print np.random.beta(1, 10, 100) # beta分布,还可二项分布等
查看全部
举报