%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
# 在-10和10之间生成一个数列,共100个数
x = np.linspace(-10,10,100)
# 用正弦函数创建第二个数组
y = np.sin(x)
# 用plot函数绘制一个数组关于另一个数组的折线图
plt.plot(x,y,marker="x")
# plt.plot(x,y,marker="o")
def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
start:起始点;stop:终止点;num:默认50,生成start和stop之间50个等差间隔的元素,必须是非负的
endpoint: If True, stop is the last sample. Otherwise, it is not included. Default is True.
endpoint:bool类型。可选参数,默认值为True,这时stop就是最后的样本。为False时,不包含stop的值。
retstep:bool类型。可选参数,默认值为True,这时返回值是(samples,step),前面的是数组,后面是步长。
dtype:表示输出的数组的数据类型,如果没有给出就从其他输入中推断输出的类型
返回值:
samples:ndarray类型。在[start,stop]闭区间,或者[start,stop)半闭合区间中,数量为num,步长相等的样本。至于包不包含stop取决于endpoint参数的取值。
step:float类型。可选。只有restep参数取值为True时才会返回这个返回值,表示样本中步长。
import numpy as np
x1 = np.linspace(1,10)
x2 = np.linspace(1,10,num = 10)
x3 = np.linspace(1,10,num = 10,retstep = True)
x4 = np.linspace(2,10,num = 10,endpoint = False)print(x1)
print(x2)
print(x3)
print(x4)
print('---------------------------')
print("length of x1 is %d" % len(x1))
print("length of x2 is %d" % len(x2))
print("length of x2 is %d" % len(x3))
print("length of x2 is %d" % len(x4))
# x为x轴数据, y为y轴数据
import matplotlib.pyplot as pltx=[3,4,5] # [列表]
y=[2,3,2] # x,y元素个数N应相同
plt.plot(x,y)
plt.show()
# x, y可传入(元组), [列表], np.array, pd.Series
import numpy as np
import pandas as pd
import matplotlib.pyplot as pltx=(3,4,5) # (元组)
y1=np.array([3,4,3]) # np.array
y2=pd.Series([4,5,4]) # pd.Seriesplt.plot(x,y1)
plt.plot(y2) # x可省略,默认[0,1..,N-1]递增
plt.plot(y1,y2)
plt.show() # plt.show()前可加多个plt.plot(),画在同一张图上
# 可传入多组x, y
import numpy as np
import pandas as pd
import matplotlib.pyplot as pltx=(3,4,5)
y1=np.array([3,4,3])
y2=pd.Series([4,5,4])plt.plot(x,y1,x,y2) # 此时x不可省略
plt.show()
# x或y传入pd.DataFrame
# x, y可以不等长, x短
import numpy as np
import pandas as pd
import matplotlib.pyplot as pltdic1={'x列0':[0,1,2],'x列1':[3,4,5]}
x=pd.DataFrame(dic1)
# 最前面的竖列0,1,2是序号
dic2={'y列0':[2,3,2],'y列1':[3,4,3],'y列2':[4,5,4],'y列3':[5,6,5]}
y=pd.DataFrame(dic2)
print(x)
print(y)
plt.plot(x,y)
plt.show()
# x, y可以不等长, x长
import numpy as np
import pandas as pd
import matplotlib.pyplot as pltdic1={'x列0':[0,1,2],'x列1':[3,4,5],'x列2':[6,7,8],'x列3':[9,10,11]}
x=pd.DataFrame(dic1)
dic2={'y列0':[2,3,2],'y列1':[3,4,3]}
y=pd.DataFrame(dic2)
print(x)
print(y)
plt.plot(x,y)
plt.show()
# x或y传入二维数组
import numpy as np
import pandas as pd
import matplotlib.pyplot as pltlst1=[[0,1,2],[3,4,5],[6,7,8]]
x=np.array(lst1)
lst2=[[2,3,2],[3,4,3],[4,5,4]]
y=np.array(lst2)
print(x)
print(y)
plt.plot(x,y)
plt.show()
# 一竖列一条线:(0,2),(3,3),(6,4)
# (1,3),(4,4),(7,5)
# (2,2),(5,3),(8,4)
# plt.plot(x, y, "格式控制字符串")
import numpy as np
import pandas as pd
import matplotlib.pyplot as pltlst1=[[0,1,2],[3,4,5],[6,7,8]]
x=np.array(lst1)
lst2=[[2,3,2],[3,4,3],[4,5,4]]
y=np.array(lst2)plt.plot(x,y,"ob:") #"b"为蓝色, "o"为圆点, ":"为点线
plt.show()
# "格式控制字符串"最多可以包括三部分, "颜色", "点型", "线型"
# "颜色"与"线型"
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt# 'b':蓝色, 'm':洋红色 magenta, 'g':绿色, 'y':黄色, 'r':红色, 'k':黑色, 'w':白色, 'c':青绿色cyan
# '#008000' RGB某颜色 '0.8' 灰度值字符串,多条曲线不指定颜色时,会自动选择不同颜色
# '‐' 实线 '‐‐' 破折线 '‐.' 点划线 ':' 虚线 '' ' ' 无线条color=['b','g','r','c','m','y','k','k']
# color=['b','g','r','c','m','y','k','w']
# 最后一个白色看不到
line_style=['-','--','-.',':']
dic1=[[0,1,2],[3,4,5]]
x=pd.DataFrame(dic1)
dic2=[[2,3,2],[3,4,3],[4,5,4],[5,6,5]]
y=pd.DataFrame(dic2)
# print(x)
# print(y)
# 循环输出所有"颜色"与"线型"
# DataFrame对象的.loc[,]和.iloc[,]方法用于抽取数据,.loc[,]用行列的标签名作为参数,.iloc[,]用二维矩阵元素的网格下标作为参数。
for i in range(2):
for j in range(4):
plt.plot(x.loc[i],y.loc[j],color[i*4+j]+line_style[j])
plt.show()
# "点型"
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt# '.' 点标记 ',' 像素标记(极小点) 'o' 实心圈标记 'v' 倒三角标记 '^' 上三角标记 '>' 右三角标记 '<' 左三角标记
marker=['.',',','o','v','^','<','>','1','2','3','4','s','p','*','h','H','+','x','D','d','|','_','.',',']
dic1=[[0,1,2],[3,4,5],[6,7,8],[9,10,11],[12,13,14],[15,16,17]]
x=pd.DataFrame(dic1)
dic2=[[2,3,2.5],[3,4,3.5],[4,5,4.5],[5,6,5.5]]
y=pd.DataFrame(dic2)
# 循环输出所有"点型"
for i in range(6):
for j in range(4):
plt.plot(x.loc[i],y.loc[j],"b"+marker[i*4+j]+":") # "b"蓝色,":"点线
plt.show()
# v 1 s H d
# o > 4 h D
# , < 3 * x _
# . ^ 2 p + |
# plt.plot(x, y, "格式控制字符串", 关键字=参数)
import matplotlib.pyplot as plty=[2,3,2]
# 蓝色,线宽20,圆点,点尺寸50,点填充红色,点边缘宽度6,点边缘灰色
plt.plot(y,color="blue",linewidth=20,marker="o",markersize=50,
markerfacecolor="red",markeredgewidth=6,markeredgecolor="grey")
plt.show()
b = np.arange(5)
plt.plot(b,b*1.0,'g.-',b,b*1.5,'rx',b,b*2.0, 'b')
plt.show()