2011-10-11 15 views
37

अब तक मेरे पास निम्न कोड है:matplotlib: विभिन्न तराजू के साथ ओवरले भूखंड?

colors = ('k','r','b') 
ax = [] 
for i in range(3): 
    ax.append(plt.axes()) 
    plt.plot(datamatrix[:,0],datamatrix[:,i],colors[i]+'o') 
    ax[i].set(autoscale_on=True) 

प्रत्येक धुरी के लिए autoscale_on=True विकल्प के साथ, मैंने सोचा कि प्रत्येक साजिश में अपनी स्वयं की वाई-अक्ष सीमा होनी चाहिए, लेकिन ऐसा लगता है कि वे सभी एक ही मूल्य साझा करते हैं (भले ही वे विभिन्न अक्ष साझा करें)। मैं प्रत्येक datamatrix[:,i] की सीमा दिखाने के लिए स्केल करने के लिए कैसे सेट करूं (केवल .set_ylim() के लिए एक स्पष्ट कॉल?) और साथ ही, मैं तीसरे चर (datamatrix[:,2]) के लिए ऑफ़सेट वाई-अक्ष कैसे बना सकता हूं जो ऊपर की आवश्यकता हो सकती है? सबको शुक्रीया।

उत्तर

94

ऐसा लगता है कि आप जो चाहते हैं वह सबप्लॉट्स है ... अब आप जो कर रहे हैं वह ज्यादा समझ में नहीं आता है (या मैं किसी भी दर पर आपके कोड स्निपेट से बहुत उलझन में हूं ...)।

इस तरह कुछ और प्रयास करें:

import matplotlib.pyplot as plt 
import numpy as np 

fig, axes = plt.subplots(nrows=3) 

colors = ('k', 'r', 'b') 
for ax, color in zip(axes, colors): 
    data = np.random.random(1) * np.random.random(10) 
    ax.plot(data, marker='o', linestyle='none', color=color) 

plt.show() 

enter image description here

संपादित करें:

आप subplots नहीं करना चाहते हैं, अपने कोड स्निपेट एक बहुत अधिक समझ में आता है।

आप एक दूसरे के शीर्ष पर तीन अक्ष जोड़ना चाहते हैं। Matplotlib यह स्वीकार कर रहा है कि आंकड़े पर बिल्कुल सही आकार और स्थान में पहले से ही एक सबप्लॉट है, और इसलिए यह हर बार उसी अक्ष ऑब्जेक्ट को वापस कर रहा है। दूसरे शब्दों में, यदि आप अपनी सूची ax देखते हैं, तो आप देखेंगे कि वे सभी एक ही ऑब्जेक्ट हैं।

यदि आप वास्तव में ऐसा करना चाहते हैं, तो आपको प्रत्येक बार अक्ष जोड़ने पर fig._seen को रिक्त स्थान पर रीसेट करने की आवश्यकता होगी। हालांकि, आप वास्तव में ऐसा नहीं करना चाहते हैं।

एक दूसरे पर तीन स्वतंत्र भूखंड लगाने की जगह, इसके बजाय twinx का उपयोग करने पर एक नज़र डालें।

उदा।

import matplotlib.pyplot as plt 
import numpy as np 
# To make things reproducible... 
np.random.seed(1977) 

fig, ax = plt.subplots() 

# Twin the x-axis twice to make independent y-axes. 
axes = [ax, ax.twinx(), ax.twinx()] 

# Make some space on the right side for the extra y-axis. 
fig.subplots_adjust(right=0.75) 

# Move the last y-axis spine over to the right by 20% of the width of the axes 
axes[-1].spines['right'].set_position(('axes', 1.2)) 

# To make the border of the right-most axis visible, we need to turn the frame 
# on. This hides the other plots, however, so we need to turn its fill off. 
axes[-1].set_frame_on(True) 
axes[-1].patch.set_visible(False) 

# And finally we get to plot things... 
colors = ('Green', 'Red', 'Blue') 
for ax, color in zip(axes, colors): 
    data = np.random.random(1) * np.random.random(10) 
    ax.plot(data, marker='o', linestyle='none', color=color) 
    ax.set_ylabel('%s Thing' % color, color=color) 
    ax.tick_params(axis='y', colors=color) 
axes[0].set_xlabel('X-axis') 

plt.show() 

enter image description here

+0

धन्यवाद - वास्तव में मैं सभी अंकों के साथ एक पैनल चाहते हैं, लेकिन प्रत्येक के पास एक अलग वाई पैमाने है ... – hatmatrix

+0

उम्मीद है कि संपादन मदद करते हैं। मुझे लगता है कि आप वास्तविक श्रेणियों के साथ लेबल किए गए वाई-अक्ष चाहते हैं।यदि आप नहीं करते (और मूल रूप से केवल वाई-मानों को अर्थहीन होना चाहते हैं), तो आप इसे और अधिक आसानी से कर सकते हैं। –

+0

आह, 'कताई' वह विशेषता है जिसे मैं चाहता था। लेकिन रीढ़ की हड्डी को मैन्युअल रूप से समायोजित करने के तरीके की तरह लगता है, यहां तक ​​कि एक "फ़्लोटिंग" धुरी के साथ जो डेटा के साथ जरूरी नहीं है, जब तक मैं सुनिश्चित करता हूं कि तराजू सही तरीके से छेड़छाड़ की जाती हैं! धन्यवाद। – hatmatrix

7

Bootstrapping कुछ तेजी से@joe-kington's जवाब का उपयोग कर एक x- अक्ष को साझा करने के लिए कई y अक्षों चार्ट बनाने के लिए: enter image description here

# d = Pandas Dataframe, 
# ys = [ [cols in the same y], [cols in the same y], [cols in the same y], .. ] 
def chart(d,ys): 

    from itertools import cycle 
    fig, ax = plt.subplots() 

    axes = [ax] 
    for y in ys[1:]: 
     # Twin the x-axis twice to make independent y-axes. 
     axes.append(ax.twinx()) 

    extra_ys = len(axes[2:]) 

    # Make some space on the right side for the extra y-axes. 
    if extra_ys>0: 
     temp = 0.85 
     if extra_ys<=2: 
      temp = 0.75 
     elif extra_ys<=4: 
      temp = 0.6 
     if extra_ys>5: 
      print 'you are being ridiculous' 
     fig.subplots_adjust(right=temp) 
     right_additive = (0.98-temp)/float(extra_ys) 
    # Move the last y-axis spine over to the right by x% of the width of the axes 
    i = 1. 
    for ax in axes[2:]: 
     ax.spines['right'].set_position(('axes', 1.+right_additive*i)) 
     ax.set_frame_on(True) 
     ax.patch.set_visible(False) 
     ax.yaxis.set_major_formatter(matplotlib.ticker.OldScalarFormatter()) 
     i +=1. 
    # To make the border of the right-most axis visible, we need to turn the frame 
    # on. This hides the other plots, however, so we need to turn its fill off. 

    cols = [] 
    lines = [] 
    line_styles = cycle(['-','-','-', '--', '-.', ':', '.', ',', 'o', 'v', '^', '<', '>', 
       '1', '2', '3', '4', 's', 'p', '*', 'h', 'H', '+', 'x', 'D', 'd', '|', '_']) 
    colors = cycle(matplotlib.rcParams['axes.color_cycle']) 
    for ax,y in zip(axes,ys): 
     ls=line_styles.next() 
     if len(y)==1: 
      col = y[0] 
      cols.append(col) 
      color = colors.next() 
      lines.append(ax.plot(d[col],linestyle =ls,label = col,color=color)) 
      ax.set_ylabel(col,color=color) 
      #ax.tick_params(axis='y', colors=color) 
      ax.spines['right'].set_color(color) 
     else: 
      for col in y: 
       color = colors.next() 
       lines.append(ax.plot(d[col],linestyle =ls,label = col,color=color)) 
       cols.append(col) 
      ax.set_ylabel(', '.join(y)) 
      #ax.tick_params(axis='y') 
    axes[0].set_xlabel(d.index.name) 
    lns = lines[0] 
    for l in lines[1:]: 
     lns +=l 
    labs = [l.get_label() for l in lns] 
    axes[0].legend(lns, labs, loc=0) 

    plt.show() 
3

धन्यवाद जो Kington का जवाब देने के लिए मैं ऊपर आ सकता है मेरी आवश्यकता के समाधान के साथ कि सभी अतिरिक्त वाई-अक्ष ग्राफ के बाईं ओर हैं।

मैं अभी भी पता है कि कैसे यह सही करना चाहते हैं क्योंकि यह सिर्फ एक के आसपास काम है:

import matplotlib.pyplot as plt 
import numpy as np 
# To make things reproducible... 
np.random.seed(1977) 

fig, ax = plt.subplots() 

# Twin the x-axis twice to make independent y-axes. 
axes = [ax, ax.twinx(), ax.twinx()] 

# Make some space on the right side for the extra y-axis. 
fig.subplots_adjust(right=0.75) 

# Move the last y-axis spine over to the right by 20% of the width of the axes 
axes[1].spines['right'].set_position(('axes', -0.25)) 
axes[2].spines['right'].set_position(('axes', -0.5)) 

# To make the border of the right-most axis visible, we need to turn the frame 
# on. This hides the other plots, however, so we need to turn its fill off. 
axes[-1].set_frame_on(True) 
axes[-1].patch.set_visible(False) 

# And finally we get to plot things... 
colors = ('Green', 'Red', 'Blue') 
intAxNo = 0 
for ax, color in zip(axes, colors): 
    intAxNo += 1 
    data = np.random.random(1) * np.random.random(10) 
    ax.plot(data, marker='o', linestyle='none', color=color) 
    if (intAxNo > 1): 
     if (intAxNo == 2): 
      ax.set_ylabel('%s Thing' % color, color=color, labelpad = -40) 
     elif (intAxNo == 3): 
      ax.set_ylabel('%s Thing' % color, color=color, labelpad = -45) 
     ax.get_yaxis().set_tick_params(direction='out') 
    else: 
     ax.set_ylabel('%s Thing' % color, color=color, labelpad = +0) 

    ax.tick_params(axis='y', colors=color) 
axes[0].set_xlabel('X-axis') 


plt.show() 

enter image description here

संबंधित मुद्दे