目录
1.python基本对象介绍
1.ndarray函数
1.创建方法
2.函数:tolist函数
2.list对象
3.dict对象
4.image对象
1.python基本对象介绍
1.ndarray函数
1.创建方法
1.创建方法
a=np.array([1, 2, 3], dtype=int)
print(a)
输出:[1, 2, 3]
2.函数:tolist函数
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(a)
b = a.tolist()
print(len(b))
print(b)
2.list对象
a = [1, 2, 3, 4]
c = [0, 1, 'aaa']
1.常用函数
len(a):计算list的长度
+方法:a = [1] ,d = a+['ddd'],d ------[1, 'ddd']
*方法:a=[1, 3], d=a*3, d------[1,3,1,3,1,3]
访问元素:a=[1,2,3,4], a[0]-----访问第一个元素;a[-1]访问最后一个元素;
a[:] 取出所有列表元素;a[0:n] 取出0到n-1号元素
迭代访问:for i in a: i++ # a列表中的每个元素+1
插入元素:a.append(13) 在列表最后插入元素
list1.insert(0, 'R') 在对应位置插入元素
删除元素: del e[3] # 删除对应e[3]处元素,后面元素前移,无空位
最大最小值:max(a) # a列表里的最大值; min(a)
统计次数:print(a.count(3)) #统计a列表中3出现的次数
查找索引:print(a.index(3)) # 从列表找出第一个为3的索引位置
移除:a.remove(3) # 移除列表中第一个为3的元素
反向操作:b = reverse(a) # b列表与a列表中元素完全相反
排序操作:a=[2,31,10,7] a.sort() # 得到a=[2, 7, 10, 31] 从小到大排序
3.dict对象
4.tuple对象
5.image对象
2.python基本函数介绍
1.随机下标
random.randrange(num): 返回0到num-1之间的一个随机数字
2.绘制图像 1.绘制柱状图# data1是list类型
x = list(range(0, 360, 5))
def plot_bar_color(x, data1, data2, data3, data4, data5, data6):
colors = []
for i in range(len(data1)):
if i % 2 == 0:
colors.append('lightcoral')
else:
colors.append('lightseagreen')
# 创建x轴显示的参数
x_tick = ['7:50', '7:51', '7:52', '7:53', '7:54', '7:55', '7:56',
'7:57', '7:58', '7:59', '8:00', '8:01', '8:02', '8:03', '8:04', '8:05', '8:06']
y_tick = ['0', '5', '10', '15', '20', '25', '30', '35']
plt.figure(figsize=(300, 100), dpi=10)
font2 = {'family': 'Times New Roman', 'weight': 'normal', 'size': 100}
ax1 = plt.subplot(2, 3, 1, frameon=False)
plt.title('position=500 lane=1', font2)
plt.xlabel('time', font2)
plt.ylabel('volume(pcu/min)', font2)
plt.grid()
plt.bar(range(len(data1)), data1, color=colors, width=1)
plt.xticks([index - 0.5 for index in range(len(x_tick))], x_tick, rotation=45, size=100)
plt.ylim(0, 35)
plt.yticks(size=200)
ax2 = plt.subplot(2, 3, 2, frameon=False)
plt.title('position=500 lane=2', font2)
plt.xlabel('time', font2)
plt.ylabel('volume(pcu/min)', font2)
plt.grid()
plt.bar(range(len(data2)), data2, color=colors, width=1)
plt.xticks([index - 0.5 for index in range(len(x_tick))], x_tick, rotation=45, size=100)
plt.ylim(0, 35)
plt.yticks(size=200)
ax3 = plt.subplot(2, 3, 3, frameon=False)
plt.title('position=500 lane=3', font2)
plt.xlabel('time', font2)
plt.ylabel('volume(pcu/min)', font2)
plt.grid()
plt.bar(range(len(data3)), data3, color=colors, width=1)
plt.xticks([index - 0.5 for index in range(len(x_tick))], x_tick, rotation=45, size=100)
plt.ylim(0, 35)
plt.yticks(size=200)
ax4 = plt.subplot(2, 3, 4, frameon=False)
plt.title('position=500 lane=4', font2)
plt.xlabel('time', font2)
plt.ylabel('volume(pcu/min)', font2)
plt.grid()
plt.bar(range(len(data4)), data4, color=colors, width=1)
plt.xticks([index - 0.5 for index in range(len(x_tick))], x_tick, rotation=45, size=100)
plt.ylim(0, 35)
plt.yticks(size=200)
ax5 = plt.subplot(2, 3, 5, frameon=False)
plt.title('position=500 lane=5', font2)
plt.xlabel('time', font2)
plt.ylabel('volume(pcu/min)', font2)
plt.grid()
plt.bar(range(len(data5)), data5, color=colors, width=1)
plt.xticks([index - 0.5 for index in range(len(x_tick))], x_tick, rotation=45, size=100)
plt.ylim(0, 35)
plt.yticks(size=200)
ax6 = plt.subplot(2, 3, 6, frameon=False)
plt.title('position=500 lane=6', font2)
plt.xlabel('time', font2)
plt.ylabel('volume(pcu/min)', font2)
plt.grid()
plt.bar(range(len(data6)), data6, color=colors, width=1)
plt.xticks([index - 0.5 for index in range(len(x_tick))], x_tick, rotation=45, size=100)
plt.ylim(0, 35)
plt.yticks(size=200)
plt.savefig('plt1.png')
plt.show()
2.绘制折线图
# data1是list类型
x = list(range(0, 360, 5))
def plot_line(x, data1, data2, data3, data4, data5):
# 创建x轴显示的参数
x_tick = ['7:50', '7:51', '7:52', '7:53', '7:54', '7:55', '7:56',
'7:57', '7:58', '7:59', '8:00', '8:01', '8:02', '8:03', '8:04', '8:05']
x = range(len(x_tick))
plt.figure(figsize=(300, 100), dpi=10)
font2 = {'family': 'Times New Roman', 'weight': 'normal', 'size': 100}
ax1 = plt.subplot(2, 3, 1, frameon=False)
plt.plot(x, data1, marker='.', mec='r', mfc='w', linewidth=6)
plt.xticks(x, x_tick, rotation=45, size=100)
plt.ylim(0, 75)
plt.yticks(size=100)
plt.margins(0) # 设置缩放
plt.title('position=500 lane=1', font2)
plt.xlabel('time', font2)
plt.ylabel('space mean speed(fit/s)', font2)
plt.grid()
ax2 = plt.subplot(2, 3, 2, frameon=False)
plt.plot(x, data2, marker='.', mec='r', mfc='w', linewidth=6)
# plt.legend() # 显示变化曲线的名称
plt.xticks(x, x_tick, rotation=45, size=100)
plt.ylim(0, 75)
plt.yticks(size=100)
plt.margins(0) # 设置缩放
plt.title('position=500 lane=2', font2)
plt.xlabel('time', font2)
plt.ylabel('space mean speed(fit/s)', font2)
plt.grid()
ax3 = plt.subplot(2, 3, 3, frameon=False)
plt.plot(x, data3, marker='.', mec='r', mfc='w', linewidth=6)
plt.xticks(x, x_tick, rotation=45, size=100)
plt.ylim(0, 75)
plt.yticks(size=100)
plt.margins(0) # 设置缩放
plt.title('position=500 lane=3', font2)
plt.xlabel('time', font2)
plt.ylabel('space mean speed(fit/s)', font2)
plt.grid()
ax4 = plt.subplot(2, 3, 4, frameon=False)
plt.plot(x, data4, marker='.', mec='r', mfc='w', linewidth=6)
plt.xticks(x, x_tick, rotation=45, size=100)
plt.ylim(0, 75)
plt.yticks(size=100)
plt.margins(0) # 设置缩放
plt.title('position=500 lane=4', font2)
plt.xlabel('time', font2)
plt.ylabel('space mean speed(fit/s)', font2)
plt.grid()
ax5 = plt.subplot(2, 3, 5, frameon=False)
plt.plot(x, data5, marker='.', mec='r', mfc='w', linewidth=6)
plt.xticks(x, x_tick, rotation=45, size=100)
plt.ylim(0, 75)
plt.yticks(size=100)
plt.margins(0) # 设置缩放
plt.title('position=500 lane=5', font2)
plt.xlabel('time', font2)
plt.ylabel('space mean speed(fit/s)', font2)
plt.grid()
'''
ax6 = plt.subplot(2, 3, 6, frameon=False)
plt.plot(x, data6, marker='.', mec='r', mfc='w', linewidth=6)
plt.xticks(x, x_tick, rotation=45, size=100)
plt.ylim(0, 75)
plt.yticks(size=100)
plt.margins(0) # 设置缩放
plt.title('position=500 lane=6', font2)
plt.xlabel('time', font2)
plt.ylabel('space mean speed(fit/s)', font2)
plt.grid()
'''
plt.savefig('plt3.png')
plt.show()



