为什么在此图中未显示水平(y = 0)线?

时间:2019-02-27 20:22:06

标签: python matplotlib

为什么只显示垂直线而不显示水平线?

我看过另一篇文章,其中使用了用于绘制垂直和水平线的相同代码行,并且对它们有用。

图形

# Variables
x_stop = 1
x = 0
y = -1
h = 0.5 # Stepsize
x_i = x # starting point, x-coordinate
y_i = y #starting point, y-coordinate

# Calculations #

# Diff.Eq
def dydx(x,y):
    f = y*y - x*y
    return f

print("-----------------------------")
while x < x_stop:
    print("x-value: " + str(x) + " y-value: " + str(round(y,4)))
    yprime = dydx(x,y) # y'
    delta_y = h*yprime # Δy
    x_values.append(x) # append x-coordinate
    corr_y_values.append(y) # append corresponding y-coordinate
    x += h
    y += delta_y

# PLOTTING THE SOLUTION #

# Plotting the points  
plt.plot(x_values, corr_y_values, color="blue", lw=1.2)

# Naming the x axis 
plt.xlabel('x - axis') 
# Naming the y axis 
plt.ylabel('y - axis')   
# Giving a title 
plt.title("y\' = " + diff)

# setting x and y axis range 
plt.ylim((y_i-0.2), (y+0.2)) 
plt.xlim((x_i-0.2), (x_stop+0.2))

# Showing the graph
plt.grid(True)

# Horizontal line
plt.hlines(y=0, xmin=(x_i-0.2), xmax=(x_stop+0.2), color='black', lw=1.0)

# Vertical line
plt.vlines(x=0, ymin=(y_i-0.2), ymax=(y+0.2), color='black', lw=1.0)

plt.savefig('plot.png')

1 个答案:

答案 0 :(得分:0)

您的y轴值为负且小于零。

尝试一下:

plt.hlines(y=-0.2, xmin=(x_i-0.2), xmax=(x_stop+0.2), color='black', lw=1.0)