【问题标题】:How to avoid negative numbers on axis in matplotlib scatterplot如何避免matplotlib散点图中轴上的负数
【发布时间】:2015-03-26 09:51:23
【问题描述】:

我正在使用 Python 散点图做一个简单的散点图。但无论我如何设置轴,也无论我没有任何负值,我都会在 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')  

【问题讨论】:

    标签: python matplotlib scatter-plot


    【解决方案1】:

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

    请注意,如果您只是使用 pyplot 接口,即不使用figax,则命令为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()
    

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 2012-01-30
      • 2019-07-25
      • 1970-01-01
      • 2023-03-14
      • 2014-09-18
      • 2017-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多