简体   繁体   English

Pandas.plot(subplots = True)在每个子图中有3列

[英]Pandas.plot(subplots=True) with 3 columns in each subplot

I have a DataFrame with 700 rows and 6 columns: 我有一个700行和6列的DataFrame:

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

df = pd.DataFrame(np.random.rand(700,6))

I can plot all columns in a single plot by calling: 我可以通过调用以下命令在一个图中绘制所有列

df.plot()

And I can plot each column in a single plot by calling: 我可以通过调用以下命令在一个图中绘制每一列

df.plot(subplots=True)

How can I have two subplots with three columns each from my DataFrame?! 我怎么能从我的DataFrame中获得两个带有三列的子图?

Here's a general approach to plot a dataframe with n columns in each subplot: 这是一种在每个子图中绘制带有n列的数据框的一般方法:

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

df = pd.DataFrame(np.random.rand(700,6))

col_per_plot = 3
cols = df.columns.tolist()
# Create groups of 3 columns
cols_splits = [cols[i:i+col_per_plot] for i in range(0, len(cols), col_per_plot)]  

# Define plot grid.
# Here I assume it is always one row and many columns. You could fancier...
fig, axarr = plt.subplots(1, len(cols_splits))
# Plot each "slice" of the dataframe in a different subplot
for cc, ax in zip(cols_splits, axarr):
    df.loc[:, cc].plot(ax = ax)

This gives the following picture: 这给出了以下图片:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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