witt.S
witt.S

Reputation: 31

Using matplotlib to draw discrete lines plot

I am trying to use matplotlib to draw a plot for a discrete function with connecting lines between those discrete lines, but the generated plot is very strange. Though I have already set all discrete lines (blue ones) to have equal length, they still seems different in the plot. Furthermore, there are blank spaces between connecting lines and discrete lines even though I set that they should intersect at the same point.

Generated plot

import numpy as np
from matplotlib import pyplot as plt

x=np.linspace(0,17)
plt.ylim(-5,2)

plt.plot(x[x < 1.0], 0.0 + x[x < 1.0]*0, color='blue', linewidth=3.0)
plt.plot(x[(x > 2.0) & (x < 3.0)], 1.31 + x[(x>2.0) & (x<3.0)]*0, color='blue', linewidth=3.0)
plt.plot(x[(x > 4.0) & (x < 5.0)], 0.16 + x[(x>4.0) & (x<5.0)]*0, color='blue', linewidth=3.0)
plt.plot(x[(x > 6.0) & (x < 7.0)], -1.94 + x[(x>6.0) & (x<7.0)]*0, color='blue', linewidth=3.0)
plt.plot(x[(x > 8.0) & (x < 9.0)], -3.77 + x[(x>8.0) & (x<9.0)]*0, color='blue', linewidth=3.0)
plt.plot(x[(x > 10.0) & (x < 11.0)], -3.82 + x[(x>10.0) & (x<11.0)]*0, color='blue', linewidth=3.0)
plt.plot(x[(x > 12.0) & (x < 13.0)], -2.75 + x[(x>12.0) & (x<13.0)]*0, color='blue', linewidth=3.0)
plt.plot(x[(x > 14.0) & (x < 15.0)], -1.64 + x[(x>14.0) & (x<15.0)]*0, color='blue', linewidth=3.0)

plt.plot([1,2],[0.0,1.31], color='black')
plt.plot([3,4],[1.31,0.16], color='black')
plt.plot([5,6],[0.16,-1.94], color='black')
plt.plot([7,8],[-1.94,-3.77], color='black')
plt.plot([9,10],[-3.77,-3.82], color='black')
plt.plot([11,12],[-3.82,-2.75], color='black')
plt.plot([13,14],[-2.75,-1.64], color='black')

plt.show()

Upvotes: 0

Views: 2213

Answers (1)

SpghttCd
SpghttCd

Reputation: 10860

What you see is the (low) resolution of your x-data, as it only consists of 50 points, which is the default of np.linspace.

Try

x = np.linspace(0, 17, 1000)

for example.

Upvotes: 1

Related Questions