X坐标图上的日期过分拥挤:Python

时间:2018-08-13 18:09:01

标签: python matplotlib axis-labels

我目前遇到问题,显示的x标签在x轴上重叠,导致标签难以辨认。

我已经尝试过的:

1)轴已旋转刻度线至60度,但某些图仍重叠

2)Locator_params已经尝试过,但是会引发错误“ AttributeError:'List'对象没有属性'locator_params'“

3)使用mdates访问年份和月份定位器也没有成功。以下是我尝试的示例代码;从来源Matplotlib date manipulation so that the year tick show up every 12 months

        years = mdates.YearLocator()
        months = mdates.MonthLocator()
        monthsFmt = mdates.DateFormatter('%b-%y')
        yearsformat=mdates.DateFormatter('\n\n%Y')
        ###
        ax.xaxis.set_minor_locator(months)
        ax.xaxis.set_minor_formatter(monthsFmt)
        ####
        plt.setp(ax.xaxis.get_minorticklabels(), rotation=90)
        ax.xaxis.set_major_formatter(years)
        ax.xaxis.set_major_formatter(yearsformat)

我相信我可能会误会如何使用locator_params和mdates。

注意:

1)我正在使用循环制作〜200个具有不同数量xticks的图,其中一些具有400+,另一些只有15。由于这个原因,我想尝试将最大刻度数设置为〜30如果可能,则为-40。但是,我也欢迎其他想法和建议。

代码段:

 def plotme(df,string,ref):
        for i in range(0,len(df.columns)):
             #Scatter plot the active ingredient data. 
             plotdf=df.iloc[:,i].dropna()
             #Store length of df
             length=len(plotdf.index) 



    #Decide on length to cutoff. If index is shorter then 10 datapoints, 
        exclude it
        if length > 10:
            #Dataframes setup for Statistics Fucntion
            statss=pd.to_numeric(df.iloc[:,i],errors='coerce')
            #Last value in the dataframe, aka the most recent
            lastvalue=statss[-1]
            lastvalue=round(lastvalue,2)
            #String Manipulations
            #Possibly make a function for this
            temp5=str(ref[i])
            table = str.maketrans(dict.fromkeys("([)]'"))
            temp5=temp5.translate(table)
            temp6=temp5.split(',')

            #The Iterative Dict split up for accessing
            low=float(temp6[1])
            low=round(low,2)
            high=float(temp6[2])
            high=round(high,2)
            active=str(temp6[0])
            uom=str(temp6[3])
            usl=' USL ' + str(high)
            lsl=' LSL ' + str(low)

            #Call to fucntion for Statistical analysis; a list is returned
            #List order; 0==Cp, 1==Cpk, 2==Mean, 3==Standard          
            stat=statistics(statss,high,low)
            mean=stat[2]
            standard=stat[3]
            cp=stat[0]
            cpk=stat[1]
            meanstr=str(mean) + ' $\mu$'
            title=string + ' ' + active
            subtitle='The Cp is ' +str(cp) + ' while the CpK is ' +str(cpk)
            ylabel=active + '  (' + uom +')'
            plt.figure(0)
            #Index is date logged currently, change when index is greater than 100, as overcrowding occurs
            ax,=plt.plot(plotdf.index,plotdf, marker='o', linewidth=1, markersize=3)
            #ax.locator_params(nbins=4)
            #Changing Plot Axis and adding the reference line
            plt.title(title)
            plt.ylabel(ylabel)
            plt.xlabel('Date Logged',fontsize=8)
            plt.rc('xtick', labelsize=7)
            plt.plot([0,length],[low,low],'r-',lw=3)
            plt.plot([0,length],[high,high],'r-',lw=3)
            plt.xlim(0,length)
            plt.xticks(rotation=60)
            #plt.xticks(plt.xticks()[0][1::2], 
            #plt.xticks()[1][1::2])
            #plt.gca().xaxis.set_major_locator(mdates.DayLocator((1,15)))
            #plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%d %b %Y"))
            ax.locator_params(axis='x',nbins=40)

            plt.plot([0,length],[mean,mean],'g',lw=2,label='$\mu$')
            #Add the USL and LSL
            #Possibly use Hline subclass instead for efficiency(?)
            plt.text((length+0.5),high,usl,fontsize=7,color='red')
            plt.text((length+0.5),low,lsl,fontsize=7,color='red')
            plt.text((length+0.5),mean,meanstr,fontsize=7,color='green')
            #For loop to plot the sigma lines
            for i in range(1,5):
                minsig=mean-i*standard
                adsig=mean+i*standard
                cond1=math.isnan(adsig)
                cond2=math.isnan(minsig)
               ####Figure out later####
                # if (minsig or adsig) 
                ##Some of the minsig and adsig calculations are nan resulting in an error converting nan to an integer.
                if ((cond1==False) and (cond2==False)):
                    plt.plot([0,length],[minsig,minsig],'k--',lw=1,label='+' + str(i)+' ' + '$\sigma$')
                    plt.plot([0,length],[adsig,adsig],'k--',lw=1,label='-' + str(i)+ ' ' + '$\sigma$')
                    plt.text((length+0.1),minsig,'-' + str(i)+' ' + '$\sigma$',fontsize=6)
                    plt.text((length+0.1),adsig,'+' + str(i)+' ' + '$\sigma$',fontsize=6)
            #Save the plots as the name of the title and delete the white space in order to maximize viewability
            plt.savefig(title + '.png', bbox_inches='tight',format='png',dpi=800)   
            plt.show()    
            plt.figure(1)
            plt.rc('xtick', labelsize=8)
            sns.distplot(a=plotdf,bins=20,hist_kws=dict(edgecolor='k',linewidth=2))
            plt.axvline(low, color='r', linestyle='solid', linewidth=2)
            plt.axvline(mean, color='g', linestyle='dashed', linewidth=2)
            plt.axvline(high, color='r', linestyle='solid', linewidth=2)

            plt.xlabel(ylabel)
            plt.title(title + '\n' + subtitle)
            plt.savefig(title + ' Normal' +'.png',bbox_inches='tight',format='png',dpi=800)

问题示例: The Issue image with overlapping x axis labels

Example of well spaced, ideal case

感谢大家提前提供帮助。

2 个答案:

答案 0 :(得分:0)

如果旋转后仍然有一些重叠,也许如果使用plt.tight_layout()会更好。所以试试这个

plt.xlabel(ylabel)
plt.title(title + '\n' + subtitle)
plt.tight_layout()
plt.savefig(title + ' Normal' +'.png',bbox_inches='tight',format='png',dpi=800)

答案 1 :(得分:0)

问号是否太大? 您可以使用set_tick_params中的“ labelsize”将尺寸调整为较小的尺寸

ax.xaxis.set_tick_params(rotation=30, labelsize=3)

来自matplotlib tutorial

或者,这就是我在自己的代码中使用的:

plt.gca().xaxis.set_tick_params(rotation = 30, labelsize=3)