[英]pandas: x-axes breaks in a multi-plot graph using add_subplot and subplots()
我想用一个具有多个功能的df创建一个数字。 我能够分别构建功能,但在组合它们时遇到问题。 我认为主要原因是我正在使用subplots()和add_subplot(),但不知道如何将它们组合在一起。 这些是功能:
此功能从这里改编
import numpy as np
import matplotlib.pyplot as plt
def breakX(ax1,ax2):
ax=ax1
ax2=ax2
ax.set_ylim(.78, 1.)
ax2.set_ylim(0, .22)
ax.spines['bottom'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax.xaxis.tick_top()
ax.tick_params(labeltop='off')
ax2.xaxis.tick_bottom()
d = .015
kwargs = dict(transform=ax.transAxes, color='black', clip_on=False )
ax.plot((-d, +d), (-d, +d), **kwargs)
ax.plot((1 - d, 1 + d), (-d, +d), **kwargs)
kwargs.update(transform=ax2.transAxes)
ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs)
ax2.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs)
# breakX is used in this function to create a figure with three histograms:
def figure2():
fig=plt.figure()
pts = np.array([0.015, 0.166, 0.133, 0.159, 0.041, 0.024, 0.195, 0.039, 0.161, 0.018, 0.143, 0.056, 0.125, 0.096, 0.094, 0.051, 0.043, 0.021, 0.138, 0.075, 0.109, 0.195, 0.050, 0.074, 0.079, 0.155, 0.020, 0.010, 0.061, 0.008])
pts[[3, 14]] += .8
ax=fig.add_subplot(221)
ax2=fig.add_subplot(221)
f, (ax, ax2) = plt.subplots(2, 1, sharex=True)
ax.plot(pts)
ax2.plot(pts)
breakX(ax,ax2)
ax3=fig.add_subplot(222)
ax4=fig.add_subplot(222)
f, (ax3, ax4) = plt.subplots(2, 1, sharex=True)
ax3.plot(pts)
ax4.plot(pts)
breakX(ax3,ax4)
ax5=fig.add_subplot(223)
ax6=fig.add_subplot(223)
f, (ax5, ax6) = plt.subplots(2, 1, sharex=True)
ax5.plot(pts)
ax6.plot(pts)
breakX(ax5,ax6)
plt.show()
我的问题是我得到了四个数字而不是一个数字,这表明add_subplot()和subplots()不能一起工作。 我想要一个带有三个图形的图,如下所示:
原则上,您想要的是4乘2的子图网格。 这可以使用plt.subplots(nrows=4, ncols=2)
。
import numpy as np
import matplotlib.pyplot as plt
def breakX(ax1,ax2):
ax=ax1
ax2=ax2
ax.set_ylim(.78, 1.)
ax2.set_ylim(0, .22)
ax.spines['bottom'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax.xaxis.tick_top()
ax.tick_params(labeltop='off')
ax2.xaxis.tick_bottom()
d = .015
kwargs = dict(transform=ax.transAxes, color='black', clip_on=False )
ax.plot((-d, +d), (-d, +d), **kwargs)
ax.plot((1 - d, 1 + d), (-d, +d), **kwargs)
kwargs.update(transform=ax2.transAxes)
ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs)
ax2.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs)
def figure2():
fig, ((ax, ax3), (ax2, ax4), (ax5, ax_), (ax6, ax__)) = plt.subplots(nrows=4, ncols=2)
pts = np.array([0.015, 0.166, 0.133, 0.159, 0.041, 0.024, 0.195, 0.039, 0.161, 0.018, 0.143, 0.056, 0.125, 0.096, 0.094, 0.051, 0.043, 0.021, 0.138, 0.075, 0.109, 0.195, 0.050, 0.074, 0.079, 0.155, 0.020, 0.010, 0.061, 0.008])
pts[[3, 14]] += .8
ax.plot(pts)
ax2.plot(pts)
breakX(ax,ax2)
ax3.plot(pts)
ax4.plot(pts)
breakX(ax3,ax4)
ax5.plot(pts)
ax6.plot(pts)
breakX(ax5,ax6)
ax_.axis("off")
ax__.axis("off")
plt.show()
figure2()
现在这看起来有些紧缩,因此要增加空间,您可以在网格中引入另一排空轴,并使该空轴的高度为其他行的五分之一。
def figure2():
fig, ((ax, ax3), (ax2, ax4), (empty1, empty2), (ax5, ax_), (ax6, ax__)) = plt.subplots(nrows=5, ncols=2, gridspec_kw={"height_ratios" : [5,5,1,5,5]})
pts = np.array([0.015, 0.166, 0.133, 0.159, 0.041, 0.024, 0.195, 0.039, 0.161, 0.018, 0.143, 0.056, 0.125, 0.096, 0.094, 0.051, 0.043, 0.021, 0.138, 0.075, 0.109, 0.195, 0.050, 0.074, 0.079, 0.155, 0.020, 0.010, 0.061, 0.008])
pts[[3, 14]] += .8
ax.plot(pts)
ax2.plot(pts)
breakX(ax,ax2)
ax3.plot(pts)
ax4.plot(pts)
breakX(ax3,ax4)
ax5.plot(pts)
ax6.plot(pts)
breakX(ax5,ax6)
for axq in (ax_, ax__, empty1, empty2):
axq.axis("off")
plt.show()
对于更复杂的网格设计,您可以查看GridSpec页面 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.