2017-06-15 4 views
0

複数の機能を持つ1つのdfから1つの図を作成したいとします。私は機能を別々に構築することができますが、それらを組み合わせるには問題があります。主な理由は、私がsubplots()とadd_subplot()を使用していて、それらを組み合わせる方法がわからないことだと思います。 これらの機能は次のとおりです。ヒストグラムとpandas:add_subplotとsubplots()を使用してマルチプロットグラフでx軸が破損する

  1. 4つのグラフが
  2. すべてのグラフ

でブレークをx軸、この関数はhere

import numpy as np 
import matplotlib.pyplot as plt 
def breakX(ax1,ax2): 
ax=ax1 
ax2=ax2 
ax.set_ylim(.78, 1.) 
ax2.set_ylim(0, .22) 
ax.spines['bottom'].set_visible(False) 
ax2.spines['top'].set_visible(False) 
ax.xaxis.tick_top() 
ax.tick_params(labeltop='off') 
ax2.xaxis.tick_bottom() 
d = .015 
kwargs = dict(transform=ax.transAxes, color='black', clip_on=False) 
ax.plot((-d, +d), (-d, +d), **kwargs) 
ax.plot((1 - d, 1 + d), (-d, +d), **kwargs) 
kwargs.update(transform=ax2.transAxes) 
ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs) 
ax2.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs) 

# breakX is used in this function to create a figure with three histograms: 

def figure2(): 
fig=plt.figure() 
pts = np.array([0.015, 0.166, 0.133, 0.159, 0.041, 0.024, 0.195, 0.039, 0.161, 0.018, 0.143, 0.056, 0.125, 0.096, 0.094, 0.051, 0.043, 0.021, 0.138, 0.075, 0.109, 0.195, 0.050, 0.074, 0.079, 0.155, 0.020, 0.010, 0.061, 0.008]) 
pts[[3, 14]] += .8 
ax=fig.add_subplot(221) 
ax2=fig.add_subplot(221) 
f, (ax, ax2) = plt.subplots(2, 1, sharex=True) 
ax.plot(pts) 
ax2.plot(pts) 
breakX(ax,ax2) 
ax3=fig.add_subplot(222) 
ax4=fig.add_subplot(222) 
f, (ax3, ax4) = plt.subplots(2, 1, sharex=True) 
ax3.plot(pts) 
ax4.plot(pts) 
breakX(ax3,ax4) 
ax5=fig.add_subplot(223) 
ax6=fig.add_subplot(223) 
f, (ax5, ax6) = plt.subplots(2, 1, sharex=True) 
ax5.plot(pts) 
ax6.plot(pts) 
breakX(ax5,ax6) 
plt.show()  

から構成されている私の問題は、私ですadd_subplot()とsubplots()が一緒に動作していないことを示す、1つではなく4つの数字を取得します。何がしたいことは4回2つのプロットとサブプロットのグリッドで、原理的には

enter image description here

+0

問題の[mcve]を入力して、問題が2つの方法を組み合わせている場所を正確に説明してください。 – ImportanceOfBeingErnest

+0

今は良いですか? – SGeuer

+0

[mcve]は、コードをコピーして実行し、問題のある動作を観察できることを意味します。なぜツイン軸が必要ですか?ヒストグラムが同じ場合は、それらを同じ軸にプロットしてみませんか?全体として、結果のプロットがどれだけ望ましくないかは実際にはわかりません。 – ImportanceOfBeingErnest

答えて

0

:私はこのように見ている3つのグラフと1の図のようになります。これはplt.subplots(nrows=4, ncols=2)を使用して作成できます。

import numpy as np 
import matplotlib.pyplot as plt 

def breakX(ax1,ax2): 
    ax=ax1 
    ax2=ax2 
    ax.set_ylim(.78, 1.) 
    ax2.set_ylim(0, .22) 
    ax.spines['bottom'].set_visible(False) 
    ax2.spines['top'].set_visible(False) 
    ax.xaxis.tick_top() 
    ax.tick_params(labeltop='off') 
    ax2.xaxis.tick_bottom() 
    d = .015 
    kwargs = dict(transform=ax.transAxes, color='black', clip_on=False) 
    ax.plot((-d, +d), (-d, +d), **kwargs) 
    ax.plot((1 - d, 1 + d), (-d, +d), **kwargs) 
    kwargs.update(transform=ax2.transAxes) 
    ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs) 
    ax2.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs) 


def figure2(): 
    fig, ((ax, ax3), (ax2, ax4), (ax5, ax_), (ax6, ax__)) = plt.subplots(nrows=4, ncols=2) 
    pts = np.array([0.015, 0.166, 0.133, 0.159, 0.041, 0.024, 0.195, 0.039, 0.161, 0.018, 0.143, 0.056, 0.125, 0.096, 0.094, 0.051, 0.043, 0.021, 0.138, 0.075, 0.109, 0.195, 0.050, 0.074, 0.079, 0.155, 0.020, 0.010, 0.061, 0.008]) 
    pts[[3, 14]] += .8 

    ax.plot(pts) 
    ax2.plot(pts) 
    breakX(ax,ax2) 

    ax3.plot(pts) 
    ax4.plot(pts) 
    breakX(ax3,ax4) 

    ax5.plot(pts) 
    ax6.plot(pts) 
    breakX(ax5,ax6) 

    ax_.axis("off") 
    ax__.axis("off") 

    plt.show() 

figure2() 

enter image description here

あなたがグリッドに空軸の別の行を導入し、他の行としてheigh第5この1を行うことができるスペースを追加するので、これは今、搾り少し見えるかもしれません。グリッドのより洗練されたデザインのために

enter image description here

def figure2(): 
    fig, ((ax, ax3), (ax2, ax4), (empty1, empty2), (ax5, ax_), (ax6, ax__)) = plt.subplots(nrows=5, ncols=2, gridspec_kw={"height_ratios" : [5,5,1,5,5]}) 
    pts = np.array([0.015, 0.166, 0.133, 0.159, 0.041, 0.024, 0.195, 0.039, 0.161, 0.018, 0.143, 0.056, 0.125, 0.096, 0.094, 0.051, 0.043, 0.021, 0.138, 0.075, 0.109, 0.195, 0.050, 0.074, 0.079, 0.155, 0.020, 0.010, 0.061, 0.008]) 
    pts[[3, 14]] += .8 

    ax.plot(pts) 
    ax2.plot(pts) 
    breakX(ax,ax2) 

    ax3.plot(pts) 
    ax4.plot(pts) 
    breakX(ax3,ax4) 

    ax5.plot(pts) 
    ax6.plot(pts) 
    breakX(ax5,ax6) 

    for axq in (ax_, ax__, empty1, empty2): 
     axq.axis("off") 

    plt.show() 

、あなたは GridSpec pageを見てもよいです。

+0

申し訳ありません、以前のrepliengのために。ソリューションは完璧です – SGeuer

関連する問題