我有一个包含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中获得两个包含三列的子图?
答案 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)
这给出了以下图片: