我要在同一图中绘制3个数据矩阵。我的代码如下所示,为了使问题更清楚地显示,我不得不将数据帧制作得很长:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
profile_features = ['Col1']
question_features = ['Col2']
fig = plt.figure(figsize=(10,5))
count = 1
for pf in profile_features:
for qf in question_features:
data1 = pd.DataFrame(columns=['Col1', 'Col2'])
data1['Col1'] = [0, 2, 1, 1, -1, 0, -2, 0, 0, 0, 0, -2, 0, 0, 0, 0, 1, -1, 1, 0, 1, -1, 1, 0, -1, 1, 2, 1, -2, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 0, 1, 0, 0, -1, -1, -1, -2, 1, -2, -1, 0, 0, -2, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0, 0, -2, 1, -1, 0, 1, -1, 0, 0, 1, -1, 0, 0, 0, 1, 0, -1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 2, 0, 0, 0, -2, 2, -1, 1, -2, 0, 0, 0, 1, 0, 0, 0, 1, -2, 0, 0, 0, 0, 0, 0, 0, 0, -1, -2, -1, 0, -1, 0, 1, -1, -1, -2, 0, -2, 0, 1, 0, -1, 1, 0, 0, 0, -2, -1, -2, -2, 0, 0, 1, 0, 0, 0, 1, 0, 0, -2, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0, 1, 1, 0, 1, -1, -1, 0, 0, 0, 0, -2, 1, 1, 0, 0, 0, 0, 0, 0, 2, 1, 1, 0, 2, -1, -2, 0, 0, 0, 1, 0, 1, 0, 0, -1, 0, 0, 1, 1, 0, -2, 0, 1, -1, -2, 0, 1, 0, 1, 0, 0, 1, -1, 0, 0, 0, 0, 2, 0, 0, 1, 0, 1, 1, 1, 1, -2, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, -2, 1, 1, 1, 0, 0, 0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, 1, 1, 1, 1, 1, 1, 2, 0, 2, 2, 1]
data3['Col2'] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 1, 0, 2, 0, 0, 3, 0, 1, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 8, 0, 0, 2, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 5, 0, 8, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 2, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 4, 1, 1]
if len(data1[pf].unique())<10:
nbins = len(data1[pf].unique())
else:
nbins = 10
_, bins = np.histogram(data1[pf], nbins)
for i in range(len(bins)):
bins[i] = int(bins[i])
labels = ['{}-{}'.format(i + 1, j) for i, j in zip(bins[:-1], bins[1:])] #dynamically create labels
labels[0] = '{}-{}'.format(bins[0], bins[1])
if len(data1[pf].unique())<10:
binned = pd.cut(data1[pf], bins=nbins)
else:
binned = pd.cut(data1[pf], bins=bins, labels=labels, include_lowest=True, precision=0)
df1 = data1.groupby(binned)[qf].mean().reset_index()
_, bins = np.histogram(data1[pf], nbins)
for i in range(len(bins)):
bins[i] = int(bins[i])
labels = ['{}-{}'.format(i + 1, j) for i, j in zip(bins[:-1], bins[1:])] #dynamically create labels
labels[0] = '{}-{}'.format(bins[0], bins[1])
if len(data2[pf].unique())<10:
binned = pd.cut(data2[pf], bins=nbins)
else:
binned = pd.cut(data2[pf], bins=bins, labels=labels, include_lowest=True, precision=0)
df2 = data2.groupby(binned)[qf].mean().reset_index()
_, bins = np.histogram(data1[pf], nbins)
for i in range(len(bins)):
bins[i] = int(bins[i])
labels = ['{}-{}'.format(i + 1, j) for i, j in zip(bins[:-1], bins[1:])] #dynamically create labels
labels[0] = '{}-{}'.format(bins[0], bins[1])
if len(data3[pf].unique())<10:
binned = pd.cut(data3[pf], bins=nbins)
else:
binned = pd.cut(data3[pf], bins=bins, labels=labels, include_lowest=True, precision=0)
df3 = data3.groupby(binned)[qf].mean().reset_index()
plt.subplot(len(profile_features),len(question_features),count)
sns.set_style("darkgrid")
plt.plot([str(i) for i in df1[pf]], df1[qf], 'o-')
plt.plot([str(i) for i in df2[pf]], df2[qf], 'd-')
plt.plot([str(i) for i in df3[pf]], df3[qf], 's-')
plt.xticks(rotation=45)
plt.xlabel(pf)
plt.ylabel('Mean '+qf)
plt.legend(['Plot 1', 'Plot 2', 'Plot 3'])
fig.subplots_adjust(hspace=0.5)
count += 1
这将产生以下情节:
图1和图2的x轴相同,但图3的x轴不同。
如果我按如下所示分别绘制3个图,则可以更清楚地理解这一点,您可以在其中看到第三个图具有不同的x轴:
fig = plt.figure(figsize=(5,10))
plt.subplot(3,1,1)
plt.plot([str(i) for i in df1[pf]], df1[qf], 'o-')
plt.subplot(3,1,2)
plt.plot([str(i) for i in df2[pf]], df2[qf], 'd-')
plt.subplot(3,1,3)
plt.plot([str(i) for i in df3[pf]], df3[qf], 's-')
可能是因为Col1
,data1
和data2
中data3
中的值范围不同:
print(max(data1['Col1']), max(data2['Col2']), max(data3['Col1']))
>>> 6 5 3
那么,如何使装箱保持一致,以便所有3个图在同一图中以相同的x轴显示?