Matplotlib无意中沿着x轴移动线图

时间:2017-08-01 00:43:18

标签: python pandas matplotlib

我试图在不同的Pandas Dataframes中绘制三条线到matplotlib中的同一个子图。但是,当我这样做时,我发现其中一个图沿着x轴移动(每个线的xrange是不同的)。但是,当我单独绘制每条线时,xlimits是正确的,并且它们都没有移位。我试图在这里重现我的问题:

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是Pandas数据帧的列表。我无法上传图片。但如果你运行它你会看到问题。有谁知道为什么其中一条线沿着x轴移动了?

2 个答案:

答案 0 :(得分:0)

如果您能提供最小的工作示例,您将更快,更可靠地获得答案。您提供的代码缺少几个重命名的导入,标题定义,以及几条注释行混乱的东西。使用下面的代码,我看到所有行都以相同的x shift开始:

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偏移量,并由行offset的{​​{1}}参数控制。将其设置为零会使所有线条与y轴相邻:

no offset picture

答案 1 :(得分:0)

不,数据帧b被实际移位。看看蓝色曲线。它的索引定义为索引[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)开始,这意味着它被移位。