Python 3.x Matplotlib:在一个地物中创建多个子地块
我有一个数据框,它有列Python 3.x Matplotlib:在一个地物中创建多个子地块,python-3.x,pandas,matplotlib,Python 3.x,Pandas,Matplotlib,我有一个数据框,它有列x1、x2、x3、x4、x5、x6、my_y。我在为每一席西做一个散点图: %matplotlib notebook import matplotlib.pyplot as plt import matplotlib matplotlib.style.use('ggplot') my_df.plot(x='x1', y='my_y', kind = 'scatter', marker = 'x', color = 'black', ylim = [0, 10]) 我对x1
x1、x2、x3、x4、x5、x6、my_y
。我在为每一席西做一个散点图:
%matplotlib notebook
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')
my_df.plot(x='x1', y='my_y', kind = 'scatter', marker = 'x', color = 'black', ylim = [0, 10])
我对x1、x2、x3、x4、x5、x6重复上述代码6次,以创建6个图形。我想知道是否有可能使一个数字有6个分散子图?谢谢
df = pd.DataFrame(
np.random.randint(10, size=(5, 7)),
columns='x1 x2 x3 x4 x5 x6 my_y'.split()
)
df
x1 x2 x3 x4 x5 x6 my_y
0 0 8 3 2 7 5 8
1 0 6 2 5 8 4 9
2 4 7 1 2 6 4 5
3 8 5 4 0 5 7 4
4 5 6 0 1 8 7 2
选项1
从
轴
元素中使用分散
方法
fig, axes = plt.subplots(2, 3, figsize=(6, 4), sharex=True, sharey=True)
y = df.my_y.values
for i in range(6):
axes[i//3, i%3].scatter(df.iloc[:, i].values, y)
fig.tight_layout()
选项2
使用
pandas.DataFrame.plot
fig, axes = plt.subplots(2, 3, figsize=(6, 4), sharex=True, sharey=True)
y = df.my_y.values
for i in range(6):
df.plot(x='x' + str(i+1),
y='my_y',
kind='scatter',
marker='x',
color='black',
ylim=[0, 10],
ax=axes[i//3, i%3])
fig.tight_layout()
评论回复
无
sharex=True
fig, axes = plt.subplots(2, 3, figsize=(6, 4), sharey=True)
y = df.my_y.values
for i in range(6):
df.plot(x='x' + str(i+1),
y='my_y',
kind='scatter',
marker='x',
color='black',
ylim=[0, 10],
ax=axes[i//3, i%3])
fig.tight_layout()
如果您执行
x=['x1',x2',x3',x4',x5',x6']
会发生什么?谢谢!还想知道在使用选项2时,是否可以为第一行子批次设置x轴标签(x1、x2、x3)?关闭mysharex=True