matplotlib基础设置

1.把xlim和ylim替换成

plt.axis([0, 150, 0, 5])

这样子就可以相交了

2. 在同一个图上画多条曲线

不管多少图,直接plt.plot,最后面再plt.show()

当然你的每一个曲线的X和Y数据列表的size应该是一样的。目前采用比较原始的办法,之后有时间了再改进:

# 绘制异质交通流基本图(密度、流量曲线)
# 定义不同跟驰模式比例
# 假设CAV渗透率为P,那么HDV_HDV=(1-p)(1-p);HDV_CAV=(1-p)P;CAV_HDV=P(1-P);CAV_CAV=P**2
import matplotlib.pyplot as plt
import numpy as np

# 模型基本参数
L=5;a=1;v0=33.3;s0=2;T=1.5;b=2;ta=1.1;tc=0.6

#只有密度和速度的函数,没有密度和流量的函数,所以先在0~150均匀取v,然后算得K,继而求得q
#先假设HDV不管跟随什么车都没有区别
V=np.linspace(0,33.3,20)
Q1=[];Q2=[];Q3=[];Q4=[];Q5=[];Q6=[]
K1=[];K2=[];K3=[];K4=[];K5=[];K6=[]
for v in V:
    P=0
    P1 = (1 - P) * (1 - P)
    P2 = (1 - P) * P
    P3 = P * (1 - P)  # ACC
    P4 = P ** 2  # CACC
    k=1000*(P1*((s0+T*v)/(1-(v/v0)**4)**(0.5)+L)+P2*((s0+T*v)/(1-(v/v0)**4)**(0.5)+L)+P3*(ta*v+s0+L)+P4*(tc*v+s0+L))**(-1)
    q=k/1000*v/1000*3600
    K1.append(k)
    Q1.append(q)
plt.plot(K1, Q1, linestyle='dashed', label='P={}'.format(P))

for v in V:
    P=0.2
    P1 = (1 - P) * (1 - P)
    P2 = (1 - P) * P
    P3 = P * (1 - P)  # ACC
    P4 = P ** 2  # CACC
    k=1000*(P1*((s0+T*v)/(1-(v/v0)**4)**(0.5)+L)+P2*((s0+T*v)/(1-(v/v0)**4)**(0.5)+L)+P3*(ta*v+s0+L)+P4*(tc*v+s0+L))**(-1)
    q=k/1000*v/1000*3600
    K2.append(k)
    Q2.append(q)
plt.plot(K2, Q2, linestyle='dashed', label='P={}'.format(P))

for v in V:
    P=0.4
    P1 = (1 - P) * (1 - P)
    P2 = (1 - P) * P
    P3 = P * (1 - P)  # ACC
    P4 = P ** 2  # CACC
    k=1000*(P1*((s0+T*v)/(1-(v/v0)**4)**(0.5)+L)+P2*((s0+T*v)/(1-(v/v0)**4)**(0.5)+L)+P3*(ta*v+s0+L)+P4*(tc*v+s0+L))**(-1)
    q=k/1000*v/1000*3600
    K3.append(k)
    Q3.append(q)
plt.plot(K3, Q3, linestyle='dashed', label='P={}'.format(P))

for v in V:
    P=0.6
    P1 = (1 - P) * (1 - P)
    P2 = (1 - P) * P
    P3 = P * (1 - P)  # ACC
    P4 = P ** 2  # CACC
    k=1000*(P1*((s0+T*v)/(1-(v/v0)**4)**(0.5)+L)+P2*((s0+T*v)/(1-(v/v0)**4)**(0.5)+L)+P3*(ta*v+s0+L)+P4*(tc*v+s0+L))**(-1)
    q=k/1000*v/1000*3600
    K4.append(k)
    Q4.append(q)
plt.plot(K4, Q4,linestyle='dashed', label='P={}'.format(P))

for v in V:
    P=0.8
    P1 = (1 - P) * (1 - P)
    P2 = (1 - P) * P
    P3 = P * (1 - P)  # ACC
    P4 = P ** 2  # CACC
    k=1000*(P1*((s0+T*v)/(1-(v/v0)**4)**(0.5)+L)+P2*((s0+T*v)/(1-(v/v0)**4)**(0.5)+L)+P3*(ta*v+s0+L)+P4*(tc*v+s0+L))**(-1)
    q=k/1000*v/1000*3600
    K5.append(k)
    Q5.append(q)
plt.plot(K5, Q5, linestyle='dashed', label='P={}'.format(P))

for v in V:
    P=1.0
    P1 = (1 - P) * (1 - P)
    P2 = (1 - P) * P
    P3 = P * (1 - P)  # ACC
    P4 = P ** 2  # CACC
    k=1000*(P1*((s0+T*v)/(1-(v/v0)**4)**(0.5)+L)+P2*((s0+T*v)/(1-(v/v0)**4)**(0.5)+L)+P3*(ta*v+s0+L)+P4*(tc*v+s0+L))**(-1)
    q=k/1000*v/1000*3600
    K6.append(k)
    Q6.append(q)
plt.plot(K6, Q6, linestyle='dashed', label='P={}'.format(P))

x=np.linspace(0,37.06,150)
y3=33.3*3.6*x/1000
plt.plot(x,y3,linestyle='solid',label='Free Flow Rate')

plt.legend()
plt.axis([0, 150, 0, 5])

"""设置标签"""
plt.ylabel('flow rate /(${10^3}$ veh·$\mathregular{h^{-1}}$)')
plt.xlabel('density/(veh·$\mathregular{{km}^{-1}}$))')


'''去掉边框'''
ax = plt.gca()
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')

plt.show()

如图所示重复画了七条曲线。

3. 标签等加上上标

如果需要再标签里面添加这种上标,则需要用${}$,比如下面的10^3,但是如果上标是两位数甚至多位数,则再加上mathregular,如下:

 

"""设置标签"""
plt.ylabel('flow rate /(${10^3}$ veh·$\mathregular{h^{-1}}$)')
plt.xlabel('density/(veh·$\mathregular{{km}^{-1}}$))')

4. 去掉边框

'''去掉边框'''
ax = plt.gca()
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')

可以选择去掉上下左右的框框

5. 把整数型数字等放入字符串中表示

用'{}'.format()的形式

plt.plot(K5, Q5, linestyle='dashed', label='P={}'.format(P))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值