首页 > 数据分析

matplotlib 画双轴子图无法显示x轴的解决方法

时间:2020-08-22 数据分析 查看: 1029

主要问题

1.介绍

如题,画双轴子图不能显示 x-轴坐标轴标签,似乎 “双轴”与“子图”存在冲突有关,当前版本是 anaconda 3.7.4。比较奇葩的是 我家里的电脑,3.7.3 却没这个问题。但我把公司电脑换成 3.7.3 问题依旧,崩溃。

import pandas as pd
import matplotlib.pyplot as plt

a = pd.date_range('2020-07-01','2020-07-20')
b = [2,3,4,5,7,9,20,20,11,13,1,2,3,9,23,2,6,7,7,7]
c = [0.20,0.1,0.13,0.1,0.2,0.3,0.9,0.23,0.2,0.6,0.7,0.7,0.7,0.2,0.3,0.4,0.5,0.7,0.9,0.2]

data = pd.DataFrame({'a':a,'b':b,'c':c})

data = data.groupby(['a'])['b','c'].sum()

nrow = 2
ncol = 1

fig = plt.figure(figsize=(ncol*10,nrow*4))

ax_1 = plt.subplot2grid((nrow, ncol), (0, 0), colspan=1, rowspan=1,facecolor = 'black')
ax_2 = plt.subplot2grid((nrow, ncol), (1, 0), colspan=1, rowspan=1,facecolor = 'black')

data['b'].plot(ax = ax_1,color = 'r')
data['c'].plot(ax = ax_1.twinx(),color = 'y')

2.只画双轴,正常显示

import pandas as pd
import matplotlib.pyplot as plt

a = pd.date_range('2020-07-01','2020-07-20')
b = [2,3,4,5,7,9,20,20,11,13,1,2,3,9,23,2,6,7,7,7]
c = [0.20,0.1,0.13,0.1,0.2,0.3,0.9,0.23,0.2,0.6,0.7,0.7,0.7,0.2,0.3,0.4,0.5,0.7,0.9,0.2]

data = pd.DataFrame({'a':a,'b':b,'c':c})

data = data.groupby(['a'])['b','c'].sum()

nrow = 1
ncol = 1

fig = plt.figure(figsize=(ncol*10,nrow*4))

ax_1 = plt.subplot2grid((nrow, ncol), (0, 0), colspan=1, rowspan=1,facecolor = 'black')
# ax_2 = plt.subplot2grid((nrow, ncol), (1, 0), colspan=1, rowspan=1,facecolor = 'black')

data['b'].plot(ax = ax_1,color = 'r')
data['c'].plot(ax = ax_1.twinx(),color = 'y')

3.只画子图,也能正常显示

import pandas as pd
import matplotlib.pyplot as plt

a = pd.date_range('2020-07-01','2020-07-20')
b = [2,3,4,5,7,9,20,20,11,13,1,2,3,9,23,2,6,7,7,7]
c = [0.20,0.1,0.13,0.1,0.2,0.3,0.9,0.23,0.2,0.6,0.7,0.7,0.7,0.2,0.3,0.4,0.5,0.7,0.9,0.2]

data = pd.DataFrame({'a':a,'b':b,'c':c})

data = data.groupby(['a'])['b','c'].sum()

nrow = 2
ncol = 1

fig = plt.figure(figsize=(ncol*10,nrow*4))

ax_1 = plt.subplot2grid((nrow, ncol), (0, 0), colspan=1, rowspan=1,facecolor = 'black')
ax_2 = plt.subplot2grid((nrow, ncol), (1, 0), colspan=1, rowspan=1,facecolor = 'black')

data['b'].plot(ax = ax_1,color = 'r')
data['c'].plot(ax = ax_2,color = 'y')

到此这篇关于matplotlib 画双轴子图无法显示x轴的解决方法的文章就介绍到这了,更多相关matplotlib 双轴子无法显示x轴内容请搜索python博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持python博客!

展开全文
上一篇:matplotlib subplot绘制多个子图的方法示例
下一篇:如何用Matplotlib 画三维图的示例代码
输入字:
相关知识
python数据挖掘使用Evidently创建机器学习模型仪表板

在本文中,我们将探索 Evidently 并创建交互式报告/仪表板。有需要的朋友欢迎大家收藏学习,希望能够有所帮助,祝大家多多进步早日升职加薪

Python多进程共享numpy 数组的方法

本文章主要介绍了Python多进程共享numpy 数组的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

python数据分析近年比特币价格涨幅趋势分布

这篇文章主要为大家介绍了python分析近年来比特币价格涨幅趋势的数据分布,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步

python调用matlab的方法详解

这篇文章主要为大家介绍了python调用matlab,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助