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

时间:2017-05-17 10:13:59

标签: python pandas plot

我有一个包含700行和6列的DataFrame:

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

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

我可以通过调用

来绘制所有列的单个图
df.plot()

我可以通过调用

在中绘制每一列
df.plot(subplots=True)

如何从我的DataFrame中获得两个包含三列的子图?

1 个答案:

答案 0 :(得分:1)

这是绘制每个子图中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)

这给出了以下图片:

enter image description here