绘图库;帮助在子图中设置 ylimit

Matplotlib; Help setting ylimit in subplot

我搜索过但找不到我要找的东西。

如何在子图的y轴上设置y限制范围? 下面是我要完成的演示。现在范围是自动的。我想在第一个子图中设置一个特定的 y 限制范围,从 50 到 100。我尝试了一些没有运气的东西(你会看到它被注释掉了)。

import math
import matplotlib.pyplot as plt
import random


def graphIt(tupleList1, title1, List2, title2, List3, title3):
    x = []
    y = []
    x2 = []
    y2 = []
    x3 = []
    y3 = []
    for i in range(len(tupleList1)):
        x.append(tupleList1[i][0])
        y.append(tupleList1[i][1])
    for i in range(len(List2)):
        x2.append(List2[i])
        y2.append(1)
    for i in range(len(List3)):
        x3.append(List3[i])
        y3.append(1)
    f, plotarray = plt.subplots(3, sharex=True)
    #plt.ylim((50, 100)) #<--This sets the range of plotarray[2]
    #plotarray[0].ylim((50,100)) #<---No such attribute 'ylim'
    plotarray[0].plot(x,y,"o-")
    plotarray[0].set_title(title1)
    plotarray[1].bar(x2,y2,0.15,color='b',label=title2)
    plotarray[1].set_title(title2)
    plotarray[2].bar(x3,y3,0.15,color='r',label=title3)
    plotarray[2].set_title(title3)
    plt.gcf().autofmt_xdate()
    plt.show()

myTupleList = []
myList2 = []
myList3 = []
for x in range(100):
    y = random.random()*x
    myTupleList.append((x,y))
    if y > 5 and y <20:
        myList2.append(x)
    if y >20 and y <30:
        myList3.append(x)

graphIt(myTupleList,"MyTupleList",myList2,"MyList2",myList3,"MyList3")

以下是您的操作方法:

plotarray[0].set_ylim([50,100])

我用您的代码对其进行了测试以确保它可以正常工作,并且顶部子图的 y 限制已更改。