找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 3393|回复: 1

[求助] matplotlib画图,横坐标轴label*诡异*只显示一半

1

主题

1

帖子

1

积分

贫民

积分
1
CasUal000 发表于 2018-4-17 12:37:12 | 显示全部楼层 |阅读模式
#试过即使不自定义坐标,按默认的数字坐标刻度显示也只显示一部分,而图形则能画出全部数据,真心求解!代码如下import pandas as pd
import os
import numpy as np
import matplotlib.pyplot as plt
import pylab as pyl
os.chdir(r'E:\TYCloud\办公室\考勤\2018\data')
df0=pd.read_excel(r'20180417.xls',skiprows=1)
#print(df0)
df0.fillna(method='pad',inplace=True)


def namelist_ini(name_info):
    name_file=pd.read_excel(name_info)
    name_file=name_file.drop(['帐号','英文名','职务','性别','手机','座机','个人邮箱'],axis=1)
    name_file['迟到次数']=0
    name_file['早退次数']=0
    name_file['迟到早退合计']=0
    return name_file

def count_data(df0,name_file):
    for index_ in df0.index:
        if len(df0.loc[index_,'姓名'])==0:
            df0.drop(index_,axis=0,inplace=True)
        else:
            name=df0.loc[index_,'姓名']
            type_=df0.loc[index_,'类别']
            if type_=='迟到':
                name_file['迟到次数'][name_file.姓名==name]+=1
            elif type_=='早退':
                name_file['早退次数'][name_file.姓名 == name] += 1
    for index_ in name_file.index:
        name_file.loc[index_,'迟到早退合计']=name_file.loc[index_,'迟到次数']+name_file.loc[index_,'早退次数']
        if name_file.loc[index_,'迟到早退合计']==0:
            name_file.drop(index_,axis=0,inplace=True)
    name_file=name_file.sort_values(by='迟到早退合计',ascending=False)
    name_file=name_file.reset_index(drop=True)
    return name_file

def plot_data(df_sum):
    df_sum1=df_sum[df_sum.迟到次数>0]
    df_sum1=df_sum1.sort_values(by='迟到次数',ascending=False)
    df_sum1=df_sum1.reset_index(drop=True)
    df_sum2=df_sum[df_sum.早退次数>0]
    df_sum2=df_sum2.sort_values(by='早退次数',ascending=False)
    df_sum2=df_sum2.reset_index(drop=True)

    plt.rcParams['font.sans-serif'] = ['SimHei']
    names=['迟到频次','早退频次']
    x_label1=list(df_sum1.姓名)
    x_label2=list(df_sum2.姓名)

    frequency=[list(df_sum1.迟到次数),list(df_sum2.早退次数)]
    index1 = np.arange(len(x_label1))
    index2 = np.arange(len(x_label2))
    bar_width=0.35

    print(x_label1)
    print(frequency[0])
    print('================')
    print(x_label2)
    print(frequency[1])

    fig,(ax1,ax2)= plt.subplots(nrows=2)

    #xmajorlocator=pyl.MultipleLocator(0.1)
    rects1 = ax1.bar(index1,frequency[0],bar_width, color='#0072BC', label=names[0])
    #ax1.xaxis.set_major_locator(xmajorlocator)
    #ax1.xaxis.grid(True,which='major')

    rects2 = ax2.bar(index2,frequency[1],bar_width, color='#0072BC', label=names[1])


    print(len(frequency[0]))
    #rects2 = ax2.bar(index2 , frequency[1], bar_width, color='#ED1C24', label=names[1])
    ax1.set_xticks(index1)
    ax1.set_xticklabels(x_label1)
    ax1.set_xticks(index2)
    ax2.set_xticklabels(x_label2)
    #ax1.ylim(ymax=frequency[0][0], ymin=0)
    #ax2.ylim(ymax=frequency[1][0], ymin=0)
    plt.title(u'*************')
    ax1.set_title('迟到情况')
    ax2.set_title('早退情况')
    plt.show()

name_file=namelist_ini(name_info=r'name_info.xls')
df_sum=count_data(df0=df0,name_file=name_file)
#df_sum.to_excel(r'df_sum.xls')
plot_data(df_sum)

显示如图

显示如图
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表