문제

나는 현재 전화하는 코드가 있습니다 matplotlib.pylab.plot 동일한 화면에 여러 데이터 세트를 표시하고 Matplotlib는 모든 플롯을 고려하여 각각 전역 최소 및 최대로 스케일링합니다. 각 플롯을 독립적으로, 특정 플롯의 최소와 최대로 확장하도록 요청하는 방법이 있습니까?

도움이 되었습니까?

해결책

이것에 대한 직접적인 지원은 없지만 여기에 몇 가지 코드가 있습니다. 메일 링리스트 게시 두 개의 독립적 인 수직 축을 실망시킵니다.

x=arange(10)
y1=sin(x)
y2=10*cos(x)

rect=[0.1,0.1,0.8,0.8]
a1=axes(rect)
a1.yaxis.tick_left()
plot(x,y1)
ylabel('axis 1')
xlabel('x')

a2=axes(rect,frameon=False)
a2.yaxis.tick_right()
plot(x,y2)
a2.yaxis.set_label_position('right')
ylabel('axis 2')
a2.set_xticks([])

다른 팁

이것은 단일 플롯 (add_subplot (1,1,1))을 생성하고 y 축의 스케일을 제한하는 방법입니다.

myFig = figure()
myPlot = self.figure.add_subplot(1,1,1)
myPlot.plot([1,2,3,4,5], [5,4,3,2,1], '+r')
myPlot.set_ylim(1,5) # Limit y-axes min 1, max 5

이와 같은 것이 필요하지만 대화식 쉘에 복사하여 붙여 넣을 수있는 예제를 만들고 싶었습니다. 여기에는 작업 솔루션이 필요한 사람들을위한 것입니다.

from numpy import arange
from math import sin, cos
import matplotlib.pyplot as plt

x = arange(10)
y1 = [sin(i) for i in x]
y2 = [10*cos(i) for i in x]

rect = [0.1, 0.1, 0.8, 0.8]
a1 = plt.axes(rect)  # Create subplot, rect = [left, bottom, width, height] in normalized (0, 1) units
a1.yaxis.tick_left()  # Use ticks only on left side of plot
plt.plot(x, y1)
plt.ylabel('axis 1')
plt.xlabel('x')

a2 = plt.axes(rect, frameon=False)  # frameon, if False, suppress drawing the figure frame
a2.yaxis.tick_right()
plt.plot(x, y2)
a2.yaxis.set_label_position('right')
plt.ylabel('axis 2')
a2.set_xticks([])

plt.show()

Python 2.7.6, Numpy 1.8.1, Matpotlib 1.3.1에서 테스트 및 작동합니다. 나는 오버레이 데이트 플롯으로 일할 수있는 깔끔한 방법을 찾고 있습니다. 내 발견을 다시 게시하겠습니다.

다음은 날짜 플롯을 사용하는 솔루션이며, Twinx ()를 사용하여 가장 최적화 된 솔루션을 두 번째 y 축을 추가하기 위해 짧은 손으로 생각합니다.

import matplotlib.pyplot as plt
import matplotlib.dates as md
import datetime
import numpy
numpy.random.seed(0)
t = md.drange(datetime.datetime(2012, 11, 1),
            datetime.datetime(2014, 4, 01),
            datetime.timedelta(hours=1))  # takes start, end, delta
x1 = numpy.cumsum(numpy.random.random(len(t)) - 0.5) * 40000
x2 = numpy.cumsum(numpy.random.random(len(t)) - 0.5) * 0.002
fig = plt.figure()
ax1 = fig.add_subplot(111)
fig.suptitle('a title', fontsize=14)
fig.autofmt_xdate()
plt.ylabel('axis 1')
plt.xlabel('dates')
ax2 = ax1.twinx()
ax1.plot_date(t, x1, 'b-', alpha=.65)
ax2.plot_date(t, x2, 'r-', alpha=.65)
plt.ylabel('axis 2')
plt.show()

문서에서 matplotlib.pyplot.twinx (ax = none)는 x 축을 공유하는 두 번째 축을 만듭니다. 새로운 축은 AX (또는 AX가 없으면 현재 축)를 오버레이합니다. AX2의 진드기는 오른쪽에 배치되고 AX2 인스턴스가 반환됩니다. 더 여기.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top