วิธีตั้งค่า 'อัตโนมัติ' สำหรับขีด จำกัด บน แต่ให้คงขีด จำกัด ล่างไว้ด้วย matplotlib.pyplot


112

ฉันต้องการตั้งค่าขีด จำกัด บนของแกน y เป็น 'อัตโนมัติ' แต่ฉันต้องการให้ขีด จำกัด ล่างของแกน y เป็นศูนย์เสมอ ฉันลอง 'auto' และ 'autorange' แต่ดูเหมือนจะไม่ได้ผล ขอบคุณล่วงหน้า.

นี่คือรหัสของฉัน:

import matplotlib.pyplot as plt

def plot(results_plt,title,filename):

    ############################
    # Plot results

    # mirror result table such that each parameter forms an own data array
    plt.cla()
    #print results_plt
    XY_results = []

    XY_results = zip( *results_plt)

    plt.plot(XY_results[0], XY_results[2], marker = ".")

    plt.title('%s' % (title) )
    plt.xlabel('Input Voltage [V]')
    plt.ylabel('Input Current [mA]')

    plt.grid(True)
    plt.xlim(3.0, 4.2)  #***I want to keep these values fixed"
    plt.ylim([0, 80]) #****CHANGE**** I want to change '80' to auto, but still keep 0 as the lower limit 
    plt.savefig(path+filename+'.png')

คำตอบ:


104

คุณสามารถส่งผ่านเพียงleftหรือrightไปที่set_xlim:

plt.gca().set_xlim(left=0)

สำหรับแกน y ใช้bottomหรือtop:

plt.gca().set_ylim(bottom=0)

1
ฉันได้รับข้อผิดพลาดเมื่อฉันผ่านไปทางซ้ายสำหรับ set_ylim ฉันใช้สิ่งนี้แทน: plt.gca (). set_ylim (ymin = 0) ขอบคุณสำหรับความช่วยเหลือ
vietnastee

คุณยังสามารถใช้plt.xlimหรือplt.ylimเพื่อตั้งค่าขีด จำกัด สำหรับแกนปัจจุบัน
คริส

26
เมื่อฉันทำสิ่งนี้ขีด จำกัด บนจะยึดติดกับค่าใดก็ตามที่หน้าต่างสร้างอินสแตนซ์เข้ามามันจะไม่คงการปรับขนาดอัตโนมัติ
Elliot

9
ปัญหาเดียวกันกับ @Elliot ที่นี่ สามารถแก้ไขได้โดยการตั้งค่า ylim / xlim (ด้านเดียว) หลังจากพล็อตค่า
fabianfuchs

5
ตรวจสอบให้แน่ใจว่าได้ตั้งค่าขีด จำกัด หลังจากการพล็อตข้อมูลมิฉะนั้นขีด จำกัด บนจะเริ่มต้นเป็น 1
Banana

37

เพียงตั้งค่าxlimสำหรับขีด จำกัด ข้อใดข้อหนึ่ง:

plt.xlim(xmin=0)

5
xminและxmaxได้รับการเลิกใช้เพื่อสนับสนุนleftและrightใน Matplotlib 3.0
onewhaleid

12

ดังที่ได้กล่าวมาแล้วและตามเอกสารของ matplotlib axสามารถตั้งค่าขีด จำกัด x ของแกนที่กำหนดได้โดยใช้set_xlimวิธีการของmatplotlib.axes.Axesคลาส

ตัวอย่างเช่น

>>> ax.set_xlim(left_limit, right_limit)
>>> ax.set_xlim((left_limit, right_limit))
>>> ax.set_xlim(left=left_limit, right=right_limit)

ขีด จำกัด หนึ่งอาจไม่เปลี่ยนแปลง (เช่นขีด จำกัด ด้านซ้าย):

>>> ax.set_xlim((None, right_limit))
>>> ax.set_xlim(None, right_limit)
>>> ax.set_xlim(left=None, right=right_limit)
>>> ax.set_xlim(right=right_limit)

การตั้งค่า x-ข้อ จำกัด ของแกนปัจจุบันmatplotlib.pyplotโมดูลที่มีxlimฟังก์ชั่นที่เพิ่งตัด และmatplotlib.pyplot.gcamatplotlib.axes.Axes.set_xlim

def xlim(*args, **kwargs):
    ax = gca()
    if not args and not kwargs:
        return ax.get_xlim()
    ret = ax.set_xlim(*args, **kwargs)
    return ret

ในทำนองเดียวกันสำหรับ y ที่ จำกัด การใช้งานหรือmatplotlib.axes.Axes.set_ylim matplotlib.pyplot.ylimข้อโต้แย้งคำหลักและtopbottom


โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.