简体   繁体   English

绘图 X 轴上的日期过度拥挤:Python

[英]Dates Overcrowding on X-Axis of Plot :Python

I am currently having an issue with the displayed x labels overlapping on the x axis causing the labels to be illegible.我目前在 x 轴上重叠显示的 x 标签存在问题,导致标签难以辨认。

What I have already tried:我已经尝试过的:

1) The axis has been rotated the ticks to 60 degrees, but some plots still overlap 1) 轴已将刻度旋转到 60 度,但有些图仍然重叠

2) Locator_params has been tried, but it throws an error of " AttributeError: 'List' object has no attribute 'locator_params' " 2) Locator_params 已经试过了,但是会抛出“AttributeError: 'List' object has no attribute 'locator_params'”的错误

3)using mdates to access year and month locator has been unsuccessful as well. 3)使用mdates访问年月定位器也没有成功。 Following is the example code I tried;以下是我尝试过的示例代码; from source;从源头; Matplotlib date manipulation so that the year tick show up every 12 months 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)

I believe I may be misunderstanding how to use locator_params & mdates.我相信我可能误解了如何使用 locator_params 和 mdates。

Notes:笔记:

1) I am using a loop to make ~200 plots that have varying amount of xticks, some with 400+, others with only 15. For this reason, I want to try and set the maximum number of ticks at ~30-40 if possible. 1) 我正在使用一个循环来制作 ~200 个具有不同 xticks 数量的图,一些具有 400+,其他只有 15。因此,我想尝试将最大刻度数设置为 ~30-40,如果可能的。 However, I am also open to other ideas and suggestions.但是,我也愿意接受其他想法和建议。

Segment of Code:代码段:

 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)

Example of Issue: The Issue image with overlapping x axis labels问题示例:具有重叠 x 轴标签的问题图像

Example of well spaced, ideal case间隔良好的理想情况示例

Thank you everyone for any help in advance.提前感谢大家的任何帮助。

If there is some overlapping still after rotation maybe if you use plt.tight_layout() it get's better.如果旋转后仍有一些重叠,也许如果您使用plt.tight_layout()会更好。 So try this所以试试这个

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

Is the problem that the tick labels are too large?是不是刻度标签太大的问题? You can adjust the size to something smaller using "labelsize" in set_tick_params您可以使用 set_tick_params 中的“labelsize”将大小调整为更小

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

from the matplotlib tutorial :来自 matplotlib 教程

or, this is what I use in my own code:或者,这就是我在自己的代码中使用的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM