我正在努力保持2个子条形图的x标签的顺序。 问题是,当我运行脚本时,第二个子图标签会更新第一个,以便伪造值...
df1 = pd.DataFrame({"myindex":["Tuesday", "Thursday", "Monday", "Wednesday", "Friday"], "myvalues":[20000,18000,16000,12000, 10000]})
df2 = pd.DataFrame({"myindex":["Tuesday", "Thursday", "Friday", "Monday", "Wednesday"], "myvalues":[600,580,350,200,150]})
f, axes = plt.subplots(figsize=(12, 12), ncols=2, sharex=True)
sns.despine(left=True)
x, y1 = np.array(df1.myindex), (df1.myvalues)
g1 = sns.barplot(x, y1, ax=axes[0])
axes[0].set_xticklabels(labels=x, rotation=90)
x, y1 = np.array(df2.myvalues), (df2.myvalues)
g2 = sns.barplot(x, y1, ax=axes[1])
axes[1].set_xticklabels(labels=x, rotation=90)
这是由于我认为set_xticklabels
函数不适合为两个子图设置x
标签。
因此,1st
图的标签必须为:["Monday", "Tuesday", "Wednesday"]
,而2nd
图的标签必须为:["Tuesday", "Wednesday", "Monday"]
。但最后,1st
图的标签相当于2nd
图标的标签......
答案 0 :(得分:1)
创建图形时,您指定轴使用sharex = True
共享x轴。这意味着,只有在axes.set_tickslabels()
拨打 last 的时间才能显示在两个地块上。
解决方案是从图初始化中删除sharex=True
。一个包含假数据的完整示例:
import seaborn as sns
import matplotlib.pyplot as plt
f, axes = plt.subplots(figsize=(12, 12), ncols=2)
sns.despine(left=True)
x1 = ["Monday", "Tuesday", "Wednesday", "Thursday"]
y1 = [5,2,6,8]
g1 = sns.barplot(x1, y1, ax=axes[0])
axes[0].set_xticklabels(labels=x1, rotation=90)
x2 = [2,65,9,0]
y2 = [5,2,6,8]
g2 = sns.barplot(x2, y2, ax=axes[1])
axes[1].set_xticklabels(labels=x2, rotation=90)
plt.show()
给出了: