这篇文章主要为大家展示了matplotlib画双轴子图无法显示x轴怎么办,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。
主要问题
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轴怎么办的内容,如果你们有学习到知识或者技能,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。