Reputation: 21
I have this code which plots curves on x and y axis defined by an array u
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()
What I need to do is divide the x scale, so instead of [0, ..., 10] it would be [0, ..., 1] (divide x scale by N)
Anyway of doing that?
Thanks in advance!
Upvotes: 1
Views: 8983
Reputation: 11
You can create the set of x values (from 0 to 1 adding by 0.1) :
delta = 0.1
distancia = np.arange(0, 1 + delta, delta)
then plot :
plt.plot(distancia, u[i], label= "t=" + str(i/M))
plt.legend(loc='best')
plt.title(titulo)
plt.xlabel('Distancia')
plt.ylabel('Temperatura')
plt.show()
Or if inside a loop :
plt.plot(distancia[i], u[i], label= "t=" + str(i/M))
Upvotes: 0
Reputation: 129
You can simply change the xticks to look like you need them!
I also took the plot titling and labeling out of the loop as you don't need to assign it for every plotted line but just once for the entire figure/subplot!
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()
Upvotes: 0
Reputation: 21
Actually I solved it another way, i created a linearspace and made it my X axis:
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()
Upvotes: 1