gpt4 book ai didi

python - 图,穿过 y 轴

转载 作者:太空宇宙 更新时间:2023-11-04 06:25:35 25 4
gpt4 key购买 nike

如何用matplotlib绘制这样的图?

graph, that crosses y axis

最佳答案

检查 this example来自 this group

例如:

from matplotlib import pyplot as plt
import numpy as np

def line(x, slope=1, zero=0):
return zero + slope * x

x = np.array([-4,10])
y1 = line(x, 2, 2)
y2 = line(x, 1, 3)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y1)
ax.plot(x,y2)

ax.spines['left'].set_position(('data', 0))
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position(('data',0))
ax.spines['top'].set_color('none')
ax.spines['left'].set_smart_bounds(True)
ax.spines['bottom'].set_smart_bounds(True)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

plt.show()

enter image description here

或者更接近你的图片(这里我去掉了 set_smart_bounds 因为在 win7 中似乎对这个例子没有影响):

ax.spines['left'].set_position(('data', 0))
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position(('data',0))
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.ylim(ymin=0)
plt.show()

enter image description here

关于python - 图,穿过 y 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8582043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com