pandas 使用子图绘制熊猫数据框(子图 = True):放置图例并使用紧密布局

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33959124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:16:54  来源:igfitidea点击:

Plot pandas dataframe with subplots (subplots=True): Place legend and use tight layout

pythonpandasmatplotlib

提问by Cord Kaldemeyer

I really like pandas to handle and analyze big datasets. So far, I have mostly used matplotlib for plotting but now want to use pandas own plot functionalities (based on matplotlib) since it needs less code and seems to be sufficient for me in most cases. Especially the subplots to have a guick glance at big dataframes like in the following example..

我真的很喜欢用Pandas来处理和分析大数据集。到目前为止,我主要使用 matplotlib 进行绘图,但现在想使用 Pandas 自己的绘图功能(基于 matplotlib),因为它需要的代码较少,并且在大多数情况下对我来说似乎已经足够了。特别是子图可以快速浏览大数据框,如下例所示..

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Generate random data
df = pd.DataFrame(np.random.randn(96,12),
                  columns=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
                           'K', 'L'])

# Plotting
df.plot(kind='line', subplots=True, grid=True, title="Sample Data (Unit)",
        layout=(4, 3), sharex=True, sharey=False, legend=True,    
        style=['r', 'r', 'r', 'g', 'g', 'g', 'b', 'b', 'b', 'r', 'r', 'r'],
        xticks=np.arange(0, len(df), 16))

enter image description here

在此处输入图片说明

..which brings me to my questions:

..这让我想到了我的问题:

1.) How can I place all legends in the subplots at the same place (e. g. centered, outside, topright)?

1.) 如何将子图中的所有图例放置在同一位置(例如居中、外部、顶部)?

2.) Can I somehow use matplotlibs "Tight Layout" (http://matplotlib.org/users/tight_layout_guide.html) for the plot?

2.) 我可以以某种方式使用 matplotlibs“紧密布局”( http://matplotlib.org/users/tight_layout_guide.html) 来绘制情节吗?

Thanks in advance!

提前致谢!

回答by tmdavison

  1. You can have all the legends in the same place, but you would have to create them in a separate step.

    # Plotting
    df.plot(kind='line', subplots=True, grid=True, title="Sample Data (Unit)",
        layout=(4, 3), sharex=True, sharey=False, legend=False,    
        style=['r', 'r', 'r', 'g', 'g', 'g', 'b', 'b', 'b', 'r', 'r', 'r'],
        xticks=np.arange(0, len(df), 16))
    
    [ax.legend(loc=1) for ax in plt.gcf().axes]
    
  2. Sure. just use plt.tight_layout()before you showor savefig. Compare the two examples below created with and without tight_layout.

  1. 您可以将所有图例放在同一位置,但您必须在单独的步骤中创建它们。

    # Plotting
    df.plot(kind='line', subplots=True, grid=True, title="Sample Data (Unit)",
        layout=(4, 3), sharex=True, sharey=False, legend=False,    
        style=['r', 'r', 'r', 'g', 'g', 'g', 'b', 'b', 'b', 'r', 'r', 'r'],
        xticks=np.arange(0, len(df), 16))
    
    [ax.legend(loc=1) for ax in plt.gcf().axes]
    
  2. 当然。只plt.tight_layout()在你show或之前使用savefig。比较下面使用和不使用tight_layout.

Without tight_layout():

没有tight_layout()

enter image description here

在此处输入图片说明

With tight_layout():

tight_layout()

enter image description here

在此处输入图片说明