Python 如何在matplotlib散点图中避免轴上出现负数

Python 如何在matplotlib散点图中避免轴上出现负数,python,matplotlib,scatter-plot,Python,Matplotlib,Scatter Plot,我正在用蟒蛇散点图做一个简单的散点图。但是不管我如何设置轴,不管我没有任何负值,我在x轴上得到负值。如何强制轴从0开始 我的代码: fig, ax = plt.subplots(1) ax.scatter(lengths,breadths, alpha=0.3, color="#e74c3c", edgecolors='none') spines_to_remove = ['top', 'right'] for spine in spines_to_remove: ax.spines[s

我正在用蟒蛇散点图做一个简单的散点图。但是不管我如何设置轴,不管我没有任何负值,我在x轴上得到负值。如何强制轴从0开始

我的代码:

fig, ax = plt.subplots(1)
ax.scatter(lengths,breadths, alpha=0.3, color="#e74c3c", edgecolors='none')
spines_to_remove = ['top', 'right']
for spine in spines_to_remove:
    ax.spines[spine].set_visible(False)
ax.xaxis.set_ticks_position('none')
ax.yaxis.set_ticks_position('none')
ax.xaxis.set_view_interval(0,400)
ax.yaxis.set_view_interval(0,90)
figname = 'scatterlengthsbreadths.pdf'
fig.savefig(figname, bbox_inches='tight')  
您可以使用来选择x限制。请注意,有一个类似的命令ax.set_ylim用于y限制

请注意,如果您仅使用pyplot接口,即不使用fig和ax,则命令为plt.xlim

例如:

import matplotlib.pyplot as plt

x = [1,2,3]
y = [4,5,6]

fig, ax = plt.subplots()

ax.plot(x, y)

ax.set_xlim(0, 10)

plt.show()
您可以使用来选择x限制。请注意,有一个类似的命令ax.set_ylim用于y限制

请注意,如果您仅使用pyplot接口,即不使用fig和ax,则命令为plt.xlim

例如:

import matplotlib.pyplot as plt

x = [1,2,3]
y = [4,5,6]

fig, ax = plt.subplots()

ax.plot(x, y)

ax.set_xlim(0, 10)

plt.show()