我想有一个3x3网格的子图,以可视化每个系列单独。我首先创建了一些玩具数据:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style='whitegrid', rc={"figure.figsize":(14,6)})
rs = np.random.RandomState(444)
dates = pd.date_range(start="2009-01-01", end='2019-12-31', freq='1D')
values = rs.randn(4017,12).cumsum(axis=0)
data = pd.DataFrame(values, dates, columns =['a','b','c','d','e','f','h','i','j','k','l','m'])这是我写的第一个代码:
fig, ax = plt.subplots(3, 3, sharex=True, sharey=True)
for col in n_cols:
ax = data[col].plot()使用这些代码行,问题是我得到了3x3的网格,但所有的列都绘制在相同的subplotsAxes上,在右下角。Bottom Right Corner with all Lines
这是我尝试的第二件事:
n_cols = ['a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'j']
fig, ax = plt.subplots(3, 3, sharex=True, sharey=True)
for col in n_cols:
for i in range(3):
for j in range(3):
ax[i,j].plot(data[col])但是现在我可以在每个subplotAxes上绘制所有的列。All AxesSubplot with same lines
如果我试着这样做:
fig, ax = plt.subplots(sharex=True, sharey=True)
for col in n_cols:
for i in range(3):
for j in range(3):
ax[i,j].add_subplot(data[col])但是我得到: TypeError:'AxesSubplot‘对象是不可订阅的
我很抱歉,但我不知道该怎么办。
发布于 2019-12-29 00:56:44
目前,您正在绘制每个子图中的每个系列:
for col in n_cols:
for i in range(3):
for j in range(3):
ax[i,j].plot(data[col])按照您的示例代码,这里有一种方法,可以在每个子图中只绘制一个系列:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
rs = np.random.RandomState(444)
dates = pd.date_range(start="2009-01-01", end='2019-12-31', freq='1D')
values = rs.randn(4017,12).cumsum(axis=0)
data = pd.DataFrame(values, dates, columns =['a','b','c','d','e','f','h','i','j','k','l','m'])
n_cols = ['a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'j']
fig, ax = plt.subplots(3, 3, sharex=True, sharey=True)
for i in range(3):
for j in range(3):
col_name = n_cols[i*3+j]
ax[i,j].plot(data[col_name])
plt.show()https://stackoverflow.com/questions/59512068
复制相似问题