简体   繁体   中英

pandas: x-axes breaks in a multi-plot graph using add_subplot and subplots()

I want to create one figures out of one df with multiple features. I am able to build in the features seperatly but have problems to combine them. I think the main reason is that I'm using subplots() and add_subplot() and don't know how to combine them. These are the features:

  1. four graphs with histograms
  2. x-axes breaks in all graphs

this function is adapted from 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()    

my problem is that I get four figures instead of one, showing that add_subplot() and subplots() are not working together. I would like one figure with three graphs looking like this:

在此处输入图片说明

IN principle what you want is a subplot grid with 4 times 2 plots. This can be created using 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()

在此处输入图片说明

This may now look a bit squeezed, so to add space you may introduce another row of empty axes in the grid and make this one fifth as heigh as the other rows.

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

在此处输入图片说明

For more sophisticated designs of grids, you may look at the GridSpec page .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM