我有这段代码可以在数组u定义的x和y轴上绘制曲线
titulo = "lambda=0.5 M=200 N=10"
M = 200
N = 10
u = calcula_u_a1(M,N)
for i in range (0, M+1, int(M*0.1)):
# if i == 0 or i == round(0.1*M) or i == round(0.2*M) or i == round(0.3*M) or i == round(0.4*M) or i == round(0.5*M):
plt.plot(u[i], label= "t=" + str(i/M))
plt.legend(loc='best')
plt.title(titulo)
plt.xlabel('Distancia')
plt.ylabel('Temperatura')
plt.show()
我需要做的是将x比例除以[0,...,10],而不是[0,...,1](将x比例除以N)
是否这样做?
谢谢!
答案 0 :(得分:1)
实际上我用另一种方法解决了这个问题,我创建了一个线性空间并将其设为我的X轴:
titulo = "lambda=0.5 M=800 N=20"
M = 800
N = 20
X = np.linspace(0, 1, N+1)
u = calcula_u_a1(M,N)
for i in range (0, M+1, int(M*0.1)):
plt.plot(X, u[i], label= "t=" + str(i/M))
plt.legend(loc='best')
plt.title(titulo)
plt.xlabel('Distancia')
plt.ylabel('Temperatura')
plt.show()
答案 1 :(得分:0)
您可以简单地将xticks更改为您需要的样子!
我还从循环中删除了图的标题和标签,因为您不需要为每条绘制的线分配它,而只需为整个图形/子图分配它!
titulo = "lambda=0.5 M=200 N=10"
M = 200
N = 10
u = calcula_u_a1(M,N)
# Added to be able to manipulate easly XTicks of the plot
fig = plt.figure()
ax = fig.add_subplot()
for i in range (0, M+1, int(M*0.1)):
# if i == 0 or i == round(0.1*M) or i == round(0.2*M) or i == round(0.3*M) or i == round(0.4*M) or i == round(0.5*M):
ax.plot(u[i], label= "t=" + str(i/M))
# Ticks are placed at positions 0 to 10 but the values displayed are from 0 to 1
ax.xaxis.set_ticks(np.arange(0, 11, 1))
ax.xaxis.set_ticklabels(np.arange(0, 1.1, 0.1).round(1))
# Moved these out of the loop
plt.legend(loc='best')
plt.title(titulo)
plt.xlabel('Distancia')
plt.ylabel('Temperatura')
plt.show()