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

问题描述 投票:-1回答:1

怎么只显示垂直线而不是水平线呢?

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

图表

# 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')
python matplotlib
1个回答
0
投票

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

试试这个:

plt.hlines(y=-0.2, xmin=(x_i-0.2), xmax=(x_stop+0.2), color='black', lw=1.0)
© www.soinside.com 2019 - 2024. All rights reserved.