import matplotlib.pyplot as plt
import numpy as np
import matplotlib
if 0:
xpoints = np.array([0, 6])
ypoints = np.array([0, 250])
plt.plot(xpoints, ypoints, 'o') # 默认point连接到point,连成线; '0' 表示点
plt.show()
if 0:
xpoints = np.array([1,2,3,6])
ypoints = np.array([2,3,1,4])
plt.plot(xpoints, ypoints)
plt.show()
if 0:
# xpoints = np.array([1,2,3,6])
ypoints = np.array([2,3,1,4])
plt.plot(ypoints)# 默认xpoints为0,1,2,3,4,5,6 ..., 长度对应ypoints的长度
plt.show()
if 0:
xpoints = np.array([1,2,3,6])
ypoints = np.array([2,3,1,4])
plt.plot(xpoints, ypoints, marker="+")# marker将坐标标记为点,突出显示坐标,还有:"*","o","x", "D", "s", "P"等等
plt.show()
# maker | line | color
if 0:
xpoints = np.array([1,2,3,6])
ypoints = np.array([2,3,1,4])
plt.plot(xpoints, ypoints, marker="+", linestyle="dotted", color="r")
plt.plot(xpoints, ypoints, "+:r") # 等价
plt.show()
if 0:
xpoints = np.array([1,2,3,6])
ypoints = np.array([2,3,1,4])
plt.plot(xpoints, ypoints, marker="o", mec="r", mfc="y") # mec marker edge color, mfc marker face color
plt.plot(xpoints, ypoints, marker="o", ms=20, mec="#4CAF50", mfc="#4CAF50") # 支持自定义颜色, ms marker size
plt.show()
if 0:
xpoints = np.array([1,2,3,6])
ypoints = np.array([2,3,1,4])
# 可以画多条线
plt.plot(xpoints, ypoints, linestyle="dashdot") #线条样式虚线, #solid(-), dashed(--), dashdot(-.), dotted(:) , linestyle(ls)
plt.plot(ypoints, xpoints, color="r", linewidth=20) # 线条颜色红色, color(c), linewidth线宽
x1 = np.array([1,2,3,6])
y1 = np.array([1,2,3,6])
x2=np.array([2,3,1,4])
y2=np.array([2,3,1,4])
plt.plot(x1,y1,x2,y2) # 调用一次plot()画多条线段
plt.show()
if 0:
xpoints = np.array([1,2,3,6])
ypoints = np.array([2,3,1,4])
matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.plot(xpoints, ypoints, linestyle="dashdot")
font1 = {'family' : 'SimHei', 'color':'red', 'size':'15'} # 设置字体属性
plt.xlabel("x标签", fontdict=font1, loc="left") # loc: left, right, center
plt.ylabel("y标签", fontdict=font1, loc="bottom") # 设置y轴标签, loc:bottom, top, center
plt.title("标题", fontdict=font1, loc="center") # 设置标题, loc: left, right, center
# plt.grid() # 添加网格 ,可以指定显示x、y网格线 plt.grid(axis='x'), x, y, both
plt.grid(axis='x', color='r', linestyle='-') # 设置网格的属性
plt.show()
if 0:
xpoints = np.array([1,2,3,6])
ypoints = np.array([2,3,1,4])
# 多图
plt.subplot(1,2,1) # (行, 列, 图),行1,2列,1图
plt.plot(xpoints, ypoints)
plt.title('Line Graph1')
# 多图
plt.subplot(1,2,2) # 行1,2列,2图
plt.plot(xpoints, ypoints)
plt.title('Line Graph2')
plt.suptitle('Line Graphs') # 整个加标题
plt.show()
if 0:
xpoints = np.array([1,2,3,6])
ypoints = np.array([2,3,1,4])
# 散列图
# plt.scatter(xpoints, ypoints, color='r')# 同线一样可以设置颜色
colors = np.array(['r','g','b','y']) #为每个点进行上色
plt.scatter(xpoints, ypoints, color=colors)
plt.show()
if 0:
xpoints = np.array([1,2,3,6])
ypoints = np.array([2,3,1,4])
colors = np.array([0,10,20,30]) # 颜色图0-100
# colors = np.random.randint(0,100,size=(4)) # 颜色随机
colors_size = np.array([20,10,50,30]) # 每个点的颜色大小
plt.scatter(xpoints, ypoints, c=colors, cmap='viridis', s=colors_size, alpha=0.5)# alpha 透明度
plt.colorbar()
plt.show()
if 0:
xpoints = np.array([1,2,3,6])
ypoints = np.array([2,3,1,4])
plt.bar(xpoints, ypoints, width=0.1) # 设置柱状图
plt.barh(xpoints, ypoints, color='r' , height=0.1) # 设置水平柱状图
plt.show()
if 0:
xpoints = np.array([1,2,3,6])
ypoints = np.array([2,3,1,4])
# 直方图
x = np.random.normal(100, 20, 200) # 随机生成200个数据,平均值100,标准差20
plt.hist(x)
plt.show()
if 0:
xpoints = np.array([1,2,3,6])
labels = ['A', 'B', 'C', 'D']
# plt.pie(xpoints, labels=labels, startangle=90) # 饼图,设置标签,设置起始角度90,默认0度为3点钟方向
# plt.pie(xpoints, labels=labels, explode=[0.2, 0,0,0]) # 第一个脱离饼的部分
# plt.pie(xpoints, labels=labels, explode=[0.2, 0,0,0], shadow=True) # 第一个脱离饼的部分加上阴影
plt.pie(xpoints, labels=labels, explode=[0.2, 0,0,0], colors=['r', 'g', 'b', 'y']) # 每个快度的颜色
plt.show()
if 1:
xpoints = np.array([1,2,3,6])
labels = ['A', 'B', 'C', 'D']
plt.pie(xpoints, labels=labels)
plt.legend(title="Legend", loc="lower left")# 设置图例
plt.show()
10-30
839
