2017-08-01 82 views
0

我想绘制从不同熊猫数据框到matplotlib中相同子图的三条线。但是,当我这样做时,我发现其中一个图沿着x轴移动(每行的xrange不同)。但是,当我单独绘制每条线时,xlimits是正确的,它们都不会被移位。我试图在这里重现我的问题:Matplotlib无意中沿x轴移动线图

def plot_dH1(title1, title_sim, a, b, c): 
    fig = plt.figure(figsize=(4,4)) 
    plt.style.use(‘ggplot’) 
    sns.set_style(‘ticks’) 
    plt.rcParams[‘font.size’] = 15 

    n = 0 
    for i in range(len(a)): 
     ax = fig.add_subplot(1,1,n+1) 

     ax = a[i].groupby(level=(0, 1)).mean()[0].plot(label=‘$\chi_{180}$‘) 
     ax = b[i].groupby(level=(0, 1)).mean()[0].plot(label=‘All’) 
     ax = c[i].groupby(level=(0, 1)).mean()[0].plot(label=‘$\chi_{90}$‘) 

     ax.set_ylabel(‘$dH$‘) 
     ax.set_xlabel(‘$\lambda_{0}$, $\lambda_{1}$‘) 
     ax.set_title(title_sim[i]) 

     title = title_sim[i] 
     sns.despine(offset=10, ax=ax) 
     plt.xticks(rotation=90) 
#   plt.yticks(range(0,350,20),range(0,350,20)) 

     n = n+1 

    lgd = ax.legend(loc=‘upper center’, bbox_to_anchor=(0.35, -0.8),fancybox=True, shadow=True, ncol=3) 
#  plt.tight_layout() 
#  fig.savefig(‘{}.pdf’.format(‘dHdl_all’)) 
    fig.savefig(‘{}.pdf’.format(‘dHdl_all’),bbox_extra_artists=(lgd,), bbox_inches=‘tight’) 

array = [range(10), range(10,20)] 
tuples = list(zip(*array)) 
index = pd.MultiIndex.from_tuples(tuples) 

a = [pd.DataFrame(np.random.randn(10,1), index=index)] 
b = [pd.DataFrame(np.random.randn(5,1), index=index[5:])] 
c = [pd.DataFrame(np.random.randn(8,1), index=index[2:])] 

plot_dH1(title1, title_sim, a, b, c) 

A,B,C是熊猫数据帧的名单。我无法上传图片。但是如果你运行它,你会看到问题。有谁知道为什么其中一条线沿x轴移动?

回答

0

如果您可以提供最简单的工作示例,您将可以更快速,更可靠地得到答案。您提供的代码缺少重命名的几个导入,标题定义,并且有几条评论行混乱了。使用下面的代码,我看到所有的线开始具有相同x转变:

import matplotlib.pyplot as plt 
import seaborn as sns 
import numpy as np 
import pandas as pd 

def plot_dH1(title1, title_sim, a, b, c): 
    fig = plt.figure(figsize=(4,4)) 
    plt.style.use('ggplot') 
    sns.set_style('ticks') 
    plt.rcParams['font.size'] = 15 

    n = 0 
    for i in range(len(a)): 
     ax = fig.add_subplot(1,1,n+1) 

     ax = a[i].groupby(level=(0, 1)).mean()[0].plot(label='$\chi_{180}$') 
     ax = b[i].groupby(level=(0, 1)).mean()[0].plot(label='All') 
     ax = c[i].groupby(level=(0, 1)).mean()[0].plot(label='$\chi_{90}$') 

     ax.set_ylabel('$dH$') 
     ax.set_xlabel('$\lambda_{0}$, $\lambda_{1}$') 
     ax.set_title(title_sim[i]) 

     sns.despine(offset=10, ax=ax) 
     plt.xticks(rotation=90) 
     n = n+1 

    lgd = ax.legend(loc='upper center', bbox_to_anchor=(0.35, -0.8),fancybox=True, shadow=True, ncol=3) 
    fig.savefig('{}.pdf'.format('dHdl_all'),bbox_extra_artists=(lgd,), bbox_inches='tight') 

array = [range(10), range(10,20)] 
tuples = list(zip(*array)) 
index = pd.MultiIndex.from_tuples(tuples) 

a = [pd.DataFrame(np.random.randn(10,1), index=index)] 
b = [pd.DataFrame(np.random.randn(5,1), index=index[5:])] 
c = [pd.DataFrame(np.random.randn(8,1), index=index[2:])] 
title_sim = np.arange(10) 
title1 = '' 

plot_dH1(title1, title_sim, a, b, c) 

生产使用Python 3.5.2以下情节:

offset picture

我没有看到三条曲线中的一条曲线相对于另外两条的x移动。定义了x偏移量,并由行sns.despine(offset=10, ax=ax)offset参数控制。其设置为零,使所有的线邻近于y轴:

no offset picture

0

否,数据帧b的acutually移位。看看蓝色的曲线。它的索引被定义为索引[5:],这意味着它应具有这些值:

[    0 
5 15 1.398019 
6 16 0.325211 
7 17 0.113059 
8 18 0.814993 
9 19 0.402437] 

所以应该从(5 15)沿着X轴开始,但它实际上是从(2,12开始),这意味着它被移位了。