首页 > 解决方案 > 绘图 X 轴上的日期过度拥挤:Python

问题描述

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

我已经尝试过的:

1) 轴已将刻度旋转到 60 度,但有些图仍然重叠

2) Locator_params 已经尝试过了,但是它会抛出一个错误“AttributeError: 'List' object has no attribute 'locator_params'”

3) 使用 mdates 访问年月定位器也不成功。以下是我尝试过的示例代码;从源头;Matplotlib 日期操作,以便每 12 个月显示一次年份刻度

        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)

问题示例: 具有重叠 x 轴标签的问题图像

间隔良好的理想情况示例

提前感谢大家的帮助。

标签: pythonmatplotlibaxis-labels

解决方案


如果旋转后仍有一些重叠,也许如果你使用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)

推荐阅读