我正在尝试在条形图上创建不连续的y轴。我可以根据需要断开轴,但是结果图看起来像是数据的线图,而不是原始的条形图。在所附的图像中,我希望顶部坐标轴上的条形图显示在底部图形的坐标轴上。
谢谢。
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame(np.c_[PURO, MYC, HRAS, CYCD, AURKB], index=Phenotype)
df.plot.bar()
plt.xlabel('Phenotype (# of poles, total # of centrioles)')
plt.ylabel('# of cells')
fig, (ax, ax2) = plt.subplots(2, 1, sharex=True, sharey=False)
ax.plot(df)
ax2.plot(df)
ax.set_ylim(40,100)
ax2.set_ylim(0,20)
ax.spines['bottom'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax.xaxis.tick_top()
ax.tick_params(labeltop='off') # don't put tick labels at the top
ax2.xaxis.tick_bottom()
plt.show()
问题在于,您首先绘制条形图,然后使用ax.plot()
和ax2.plot()
绘制两次DataFrames ,从而简单地绘制连接线。
解决方案:删除以下行
df.plot.bar()
和替换ax.plot(df)
和ax2.plot(df)
通过
df.plot.bar(ax=ax)
df.plot.bar(ax=ax2)
以下是使用虚拟DataFrame的最小工作答案(不包括导入)。同样适用于您的情况。
fig = plt.figure(figsize=(8, 6))
df = pd.DataFrame({'count': {0: 88, 1: 67, 2: 10, 3: 16, 4: 18}}).reset_index()
fig, (ax, ax2) = plt.subplots(2, 1, sharex=True, sharey=False)
df.plot.bar(x='index', y='count', legend=False, ax=ax)
df.plot.bar(x='index', y='count', legend=False, ax=ax2)
ax.set_ylim(50,100)
ax2.set_ylim(0,20)
ax.spines['bottom'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax.xaxis.tick_top()
ax.tick_params(labeltop='off') # don't put tick labels at the top
ax2.xaxis.tick_bottom()
您可以添加对角线/小线以突出显示虚线,只需复制此官方示例中的线即可
输出量
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句