在共享x轴的子图上进行绘制时,x勾号消失 [英] x-ticks disappear when plotting on subplots sharing x-axis

查看:26
本文介绍了在共享x轴的子图上进行绘制时,x勾号消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在同一子图上绘制一条线和一个区域时,会发生这种情况.我发现我调用ay = ax.twinx()并在ay上绘制后,我的x勾号消失了.

这是我导致此错误的代码.

fig, axes = plt.subplots(nrows=2, ncols=1, figsize=[12,12])数据= pd.DataFrame([[1,2,3],[2,3,4],[3,2,4]])ix = np.unravel_index(0,axes.shape)ax=轴[ix]y = pd.DataFrame(data.iloc [:,0] -data.iloc [:,1])ax2 = ax.twinx()data.plot(ax=ax,color=['navy','red'])ax2.plot(y.values,线宽= 2.0)

如您所见,x勾号消失了.但是,如果继续绘图,您会发现最后一个子图不受影响.

  fig,axes = plt.subplots(nrows = 2,ncols = 1,figsize = [12,12])data=pd.DataFrame([[1,2,3],[2,3,4],[3,2,4]])ix = np.unravel_index(0, axes.shape)ax = axes [ix]y = pd.DataFrame(data.iloc [:,0] -data.iloc [:,1])ax2 = ax.twinx()data.plot(ax=ax,color=['navy','red'])ax2.plot(y.values, linewidth=2.0)ix = np.unravel_index(1, axes.shape)ax = axes [ix]y=pd.DataFrame(data.iloc[:,0]-data.iloc[:,1])ax2 = ax.twinx()data.plot(ax = ax,color = ['navy','red'])ax2.plot(y.values,线宽= 2.0)

解决方案

有两个选项.一个基于此问题的答案:

现在有时候上述选项可能不是一个选择,因此第二种可能的解决方案是在生成完整图后将刻度线设置为再次可见.

  [ax.get_xticklabels()中t的t.set_visible(True)]

完整示例:

 将matplotlib.pyplot导入为plt将熊猫作为pd导入图,轴 = plt.subplots(nrows=2, ncols=1)data=pd.DataFrame([[1,2,3],[2,3,4],[3,2,4]])轴=轴[0]y = pd.DataFrame(data.iloc [:,0] -data.iloc [:,1])ax2 = ax.twinx()data.plot(ax = ax)ax2.plot(y.values)ax3=轴[1]y = pd.DataFrame(data.iloc [:,0] -data.iloc [:,1])ax4=ax3.twinx()data.plot(ax = ax3)ax4.plot(y.values)[ax.get_xticklabels()中t的t.set_visible(True)]plt.show()

This happens when I try to plot a line and an area on the same subplot. I found the my x-ticks disappear after I call ay=ax.twinx() and plot on ay.

Here's my code that causes this error.

fig, axes = plt.subplots(nrows=2, ncols=1, figsize=[12,12])
data=pd.DataFrame([[1,2,3],[2,3,4],[3,2,4]])
ix = np.unravel_index(0, axes.shape)
ax=axes[ix]
y=pd.DataFrame(data.iloc[:,0]-data.iloc[:,1])
ax2=ax.twinx()
data.plot(ax=ax,color=['navy','red'])
ax2.plot(y.values, linewidth=2.0)

As you can see, the x-ticks disappear. However, if you continue plotting, you can find the last subplot isn't affected.

fig, axes = plt.subplots(nrows=2, ncols=1, figsize=[12,12])
data=pd.DataFrame([[1,2,3],[2,3,4],[3,2,4]])
ix = np.unravel_index(0, axes.shape)
ax=axes[ix]
y=pd.DataFrame(data.iloc[:,0]-data.iloc[:,1])
ax2=ax.twinx()
data.plot(ax=ax,color=['navy','red'])
ax2.plot(y.values, linewidth=2.0)
ix = np.unravel_index(1, axes.shape)
ax=axes[ix]
y=pd.DataFrame(data.iloc[:,0]-data.iloc[:,1])
ax2=ax.twinx()
data.plot(ax=ax,color=['navy','red'])
ax2.plot(y.values, linewidth=2.0)

解决方案

There are two options. One is based on the answer to this question: matplotlib - pandas - No xlabel and xticks for twinx axes in subploted figures which is to reverse the order of plotting. First plot to the two subplots, then create the twin axes for both.

import matplotlib.pyplot as plt
import pandas as pd

fig, axes = plt.subplots(nrows=2, ncols=1)
data=pd.DataFrame([[1,2,3],[2,3,4],[3,2,4]])

ax=axes[0]
y=pd.DataFrame(data.iloc[:,0]-data.iloc[:,1])
data.plot(ax=ax)

ax3=axes[1]
y=pd.DataFrame(data.iloc[:,0]-data.iloc[:,1])
data.plot(ax=ax3)

ax2=ax.twinx()
ax2.plot(y.values)
ax4=ax3.twinx()
ax4.plot(y.values)

plt.show()

Now sometimes the above may not be an option, so the second possible solution would be to set the ticks visible again after the complete plot has been generated.

[t.set_visible(True) for t in ax.get_xticklabels()]

Complete example:

import matplotlib.pyplot as plt
import pandas as pd


fig, axes = plt.subplots(nrows=2, ncols=1)
data=pd.DataFrame([[1,2,3],[2,3,4],[3,2,4]])

ax=axes[0]
y=pd.DataFrame(data.iloc[:,0]-data.iloc[:,1])
ax2=ax.twinx()
data.plot(ax=ax)
ax2.plot(y.values)

ax3=axes[1]
y=pd.DataFrame(data.iloc[:,0]-data.iloc[:,1])
ax4=ax3.twinx()
data.plot(ax=ax3)
ax4.plot(y.values)

[t.set_visible(True) for t in ax.get_xticklabels()]

plt.show()

这篇关于在共享x轴的子图上进行绘制时,x勾号消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆