我有一个像这样的数据框:
df = pd.DataFrame({'A': [0.3, 0.2, 0.5, 0.2], 'B': [0.1, 0.0, 0.3, 0.1], 'C': [0.2, 0.5, 0.0, 0.7], 'D': [0.6, 0.3, 0.4, 0.6]}, index=list('abcd'))
A B C D
a 0.3 0.1 0.2 0.6
b 0.2 0.0 0.5 0.3
c 0.5 0.3 0.0 0.4
d 0.2 0.1 0.7 0.6
现在我想将每一行绘制为条形图,其中 y 轴和 x-tick-labels 使用共享add_subplot
.
Until now, I can only produce a plot that looks like this:
有一个问题:
轴不共享,使用后如何做到这一点add_subplot
? Here https://stackoverflow.com/questions/6963035/pyplot-axes-labels-for-subplots,这个问题是通过创建一个巨大的子图来解决的;有没有办法以不同的方式做到这一点?
我想要的结果看起来像上面的图,唯一的区别是上行没有 x 刻度标签,而右列现在有 y 刻度标签。
我目前的尝试如下:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df = pd.DataFrame({'A': [0.3, 0.2, 0.5, 0.2], 'B': [0.1, 0.0, 0.3, 0.1], 'C': [0.2, 0.5, 0.0, 0.7], 'D': [0.6, 0.3, 0.4, 0.6]}, index=list('abcd'))
fig = plt.figure()
bar_width = 0.35
counter = 1
index = np.arange(df.shape[0])
for indi, rowi in df.iterrows():
ax = fig.add_subplot(2, 2, counter)
ax.bar(index, rowi.values, width=bar_width, tick_label=df.columns)
ax.set_ylim([0., 1.])
ax.set_title(indi, fontsize=20)
ax.set_xticks(index + bar_width / 2)
counter += 1
plt.xticks(index + bar_width / 2, df.columns)