programing

행렬도:lib getylim 값

lovejava 2023. 6. 8. 19:05

행렬도:lib getylim 값

사용 중matplotlib데이터를 표시하는 방법(사용)plot그리고.errorbar함수)를 사용할 수 있습니다.저는 완전히 별개의 독립적인 플롯 세트를 그리고 그들의 플롯을 조정해야 합니다.ylim값을 시각적으로 쉽게 비교할 수 있습니다.

어떻게 검색합니까?ylim각 그림의 값을 사용하여 하한 및 상한 값의 최소값과 최대값을 각각 취하고 그림을 시각적으로 비교할 수 있도록 조정할 수 있습니까?

물론, 데이터를 분석하고 나만의 맞춤형 솔루션을 만들 수도 있습니다.ylim값...하지만 저는 사용하고 싶습니다.matplotlib나를 위해 그렇게 하는 것.이 작업을 쉽게(그리고 효율적으로) 수행할 수 있는 방법에 대한 제안이 있습니까?

다음은 다음을 사용하여 플롯하는 Python 함수입니다.matplotlib:

import matplotlib.pyplot as plt

def myplotfunction(title, values, errors, plot_file_name):

    # plot errorbars
    indices = range(0, len(values))
    fig = plt.figure()
    plt.errorbar(tuple(indices), tuple(values), tuple(errors), marker='.')

    # axes
    axes = plt.gca()
    axes.set_xlim([-0.5, len(values) - 0.5])
    axes.set_xlabel('My x-axis title')
    axes.set_ylabel('My y-axis title')

    # title
    plt.title(title)

    # save as file
    plt.savefig(plot_file_name)

    # close figure
    plt.close(fig)

그냥 사용하기axes.get_ylim()와 매우 유사합니다.set_ylim문서에서:

get_ylim ()

y축 범위 가져오기 [하단, 상단]

 ymin, ymax = axes.get_ylim()

사용 중인 경우pltapi에서 직접, 당신은 전화를 피할 수 있습니다.axes전체:

def myplotfunction(title, values, errors, plot_file_name):

    # plot errorbars
    indices = range(0, len(values))
    fig = plt.figure()
    plt.errorbar(tuple(indices), tuple(values), tuple(errors), marker='.')

    plt.ylim([-0.5, len(values) - 0.5])
    plt.xlabel('My x-axis title')
    plt.ylabel('My y-axis title')

    # title
    plt.title(title)

    # save as file
    plt.savefig(plot_file_name)

   # close figure
    plt.close(fig)

위의 좋은 답변을 활용하여 plt를 사용하고 있다고 가정합니다.

import matplotlib.pyplot as plt

그러면 다음을 사용하여 4개의 그림 한계를 모두 얻을 수 있습니다.plt.axis()다음 예와 같이

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5, 6, 7, 8]  # fake data
y = [1, 2, 3, 4, 3, 2, 5, 6]

plt.plot(x, y, 'k')

xmin, xmax, ymin, ymax = plt.axis()

s = 'xmin = ' + str(round(xmin, 2)) + ', ' + \
    'xmax = ' + str(xmax) + '\n' + \
    'ymin = ' + str(ymin) + ', ' + \
    'ymax = ' + str(ymax) + ' '

plt.annotate(s, (1, 5))

plt.show()

위의 코드는 다음과 같은 출력 플롯을 생성해야 합니다.여기에 이미지 설명 입력

그냥 사용하기plt.ylim()최소 및 최대 한계를 설정하거나 얻는 데 사용할 수 있습니다.

ymin, ymax = plt.ylim()

저는 위에서 언급한 방법들을 사용하여 함께 만들었습니다.ax대신에plt

import numpy as np
import matplotlib.pyplot as plt

x = range(100)
y = x

fig, ax = plt.subplots(1, 1, figsize=(7.2, 7.2))
ax.plot(x, y);

# method 1
print(ax.get_xlim())
print(ax.get_xlim())
# method 2
print(ax.axis())

여기에 이미지 설명 입력

오래된 질문이지만, 세부 사항에 따라, 그것은 언급되지 않은 것 같습니다.sharey옵션을 사용하면 축 한계, 여백 등을 파내는 대신 이 모든 작업을 수행할 수 있습니다.문서에 사용 방법을 보여주는 데모가 있습니다.sharex하지만 y-transmit도 마찬가지입니다.

언급URL : https://stackoverflow.com/questions/26131607/matplotlib-get-ylim-values