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中获得两个包含三列的子图?
1 个解决方案
#1
1
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:
这给出了以下图片:
#1
1
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:
这给出了以下图片: