Python/Matplotlib - Change the relative size of a subplot

后端 未结 3 1625
盖世英雄少女心
盖世英雄少女心 2020-12-13 13:38

I have two plots

import matplotlib.pyplot as plt
plt.subplot(121)
plt.subplot(122)

I want plt.subplot(122) to be half as wide

3条回答
  •  -上瘾入骨i
    2020-12-13 14:10

    Yes, and if you want to reduce your code to a single line, you can put all kwargs that are to be passed to matplotlib.gridspec.GridSpec(), into the gridspec_kw parameter of plt.subplots():

    import matplotlib.pyplot as plt
    import pandas as pd
    import numpy as np
    
    fig, axs = plt.subplots(nrows=2, ncols=2, gridspec_kw={'width_ratios':[2,1], 'height_ratios':[2,1]})
    
    df = pd.DataFrame(np.random.randn(10, 3), columns=['A', 'B', 'C'])
    df.plot.bar(ax=axs[0][0])
    df.boxplot(ax=axs[0][1])
    df.plot.line(ax=axs[1][0])
    df.plot.kde(ax=axs[1][1])
    

提交回复
热议问题