قطع متداخلة متعددة مع التوسع مستقل في Matplotlib

StackOverflow https://stackoverflow.com/questions/647443

  •  22-07-2019
  •  | 
  •  

سؤال

ولدي حاليا التعليمات البرمجية الذي يستدعي matplotlib.pylab.plot عدة مرات لعرض مجموعات متعددة من البيانات على نفس الشاشة، وMatplotlib جداول كل لمين العالمي والحد الأقصى، والنظر في جميع المؤامرات. هل هناك طريقة ليطلب منها لتوسيع نطاق كل قطعة بشكل مستقل، إلى الحد الأدنى والأقصى لتلك المؤامرة معينة؟

هل كانت مفيدة؟

المحلول

وليس هناك دعم مباشر لهذا، ولكن هنا بعض التعليمات البرمجية من البريدية نشر قائمة أن illlustrates محورين العمودية المستقلة:

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)) والحد من نطاق على ص محاور.

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()

واختبارها وتعمل في بيثون 2.7.6، 1.8.1 نمباي، matpotlib 1.3.1. انا ذاهب الى مواصلة اللعب معها، وتبحث عن وسيلة نظيفة للعمل مع تتراكب مؤامرات التاريخ. أنا ما بعد الظهر استنتاجاتي.

وهنا هو الحل باستخدام قطع التاريخ، وأعتقد أن معظم الأمثل الحل باستخدام twinx () ويدها قصيرة لإضافة لمحور ص الثاني.

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 (الفأس = لا يوجد) جعل محاور الثانية التي تشترك في محور س. سوف محاور جديدة تراكب الفأس (أو المحاور الحالية إذا الفأس هو بلا). سيتم وضع القراد لAX2 على الحق، ويتم إرجاع المثال AX2. المزيد هنا .

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top