matplotlib- pandas -细分图中没有用于孪生轴的xlabel和xticks [英] matplotlib - pandas - No xlabel and xticks for twinx axes in subploted figures

查看:121
本文介绍了matplotlib- pandas -细分图中没有用于孪生轴的xlabel和xticks的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的问题,之前已经回答过.但是,它与Pandas软件包的用法不同.

I had a similar question, which was answered previously. However, it differs in usage of Pandas package with it.

这是我以前的问题: matplotlib -细分图中没有用于孪生轴的xlabel和xticks

所以,像上一个问题一样,我的问题是,为什么在使用此Python代码时第一行图不显示xlabel和xticks.

So, my question like last one is that why it does not show xlabel and xticks for first row diagrams when using this Python code.

两个注意事项:

  1. 我还使用了subplots而不是gridspec,但是结果相同.
  2. 如果您取消注释此代码中的任何行,这与在每个图中的轴上使用熊猫有关,则xlabel和xticks将消失!
  1. I also used subplots instead of gridspec but same result.
  2. If you uncomment any of the commented lines in this code, which is related to using the Pandas on the axes in each diagram, the xlabel and xticks will disappear!


import matplotlib.pyplot as plt
import matplotlib.gridspec as gspec
import numpy as np
import pandas as pd
from math import sqrt

fig = plt.figure() 
gs = gspec.GridSpec(2, 2)
gs.update(hspace=0.7, wspace=0.7)
ax1 = plt.subplot(gs[0, 0])
ax2 = plt.subplot(gs[0, 1])
ax3 = plt.subplot(gs[1, 0])
ax4 = plt.subplot(gs[1, 1])


x1 = np.linspace(1,10,10)


ax12 = ax1.twinx()
ax1.set_xlabel("Fig1")
ax12.set_xlabel("Fig1")
ax1.set_ylabel("Y1")
ax12.set_ylabel("Y2")
# pd.Series(range(10)).plot(ax=ax1)
ax12.plot(x1, x1**3)




ax22 = ax2.twinx()
ax2.set_xlabel("Fig2")
ax22.set_xlabel("Fig2")
ax2.set_ylabel("Y3")
ax22.set_ylabel("Y4")
# pd.Series(range(10)).plot(ax=ax2)
ax22.plot(x1, x1**0.5)


ax32 = ax3.twinx()
ax3.set_xlabel("Fig3")
ax32.set_xlabel("Fig3")
ax3.set_ylabel("Y5")
ax32.set_ylabel("Y6")
# pd.Series(range(200)).plot(ax=ax3)



ax42 = ax4.twinx()
ax4.set_xlabel("Fig4")
ax42.set_xlabel("Fig4")
ax4.set_ylabel("Y7")
ax42.set_ylabel("Y8")
# pd.Series(range(10)).plot(ax=ax42)



plt.subplots_adjust(wspace=0.8, hspace=0.8)
plt.show()

推荐答案

我遇到了同样的问题,因为我正在混合使用matplotlib和熊猫制作的地块.

I just got the same issue because I was mixing plots made with matplotlib and made with Pandas.

您不应使用Pandas进行绘图,这是您可以替换的方法:

You should not plot with Pandas, here is how you could replace:

pd.Series(range(10)).plot(ax=ax42)

使用

ax42.plot(pd.Series(range(10))

这篇关于matplotlib- pandas -细分图中没有用于孪生轴的xlabel和xticks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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