【问题标题】:Matplotlib axes only with values on Pandas DataframeMatplotlib 轴仅具有 Pandas Dataframe 上的值
【发布时间】:2021-04-18 12:39:05
【问题描述】:

我从去年开始制作积压图表,现在是新年,现在我面临这个问题:

我必须乘以年份来保持 X 轴向右滚动。 但在那之后,我在 X 轴上得到了这个从 202052(连接年份 + 年份中的星期数)到 202099~ 的空白区间。

我的索引没有这些值。如下:

(Int64Index([202026, 202027, 202028, 202029, 202030, 202031, 202032, 202033,
             202035, 202036, 202037, 202038, 202040, 202041, 202043, 202044,
             202045, 202046, 202047, 202048, 202049, 202050, 202051, 202052,
             202101, 202102],
            dtype='int64'),
 Int64Index([202026, 202027, 202028, 202029, 202030, 202031, 202032, 202033,
             202034, 202035, 202036, 202037, 202038, 202040, 202041, 202043,
             202044, 202045, 202046, 202047, 202048, 202049, 202050, 202051,
             202052, 202101, 202102],
            dtype='int64'),
 Int64Index([202026, 202027, 202028, 202029, 202030, 202031, 202032, 202033,
             202034, 202035, 202036, 202037, 202038, 202040, 202041, 202043,
             202044, 202045, 202046, 202047, 202048, 202049, 202050, 202051,
             202052, 202101, 202102],
            dtype='int64'))

如何删除这些值?

谢谢!

编辑:添加完整代码


import matplotlib.pyplot as plt
import pandas as pd
from datetime import datetime, timedelta
from matplotlib.lines import Line2D
import matplotlib.dates as mdates
import matplotlib.cbook as cbook
from matplotlib.ticker import MaxNLocator

%matplotlib inline

df = pd.read_csv(
    "/home/eklon/Downloads/Venturus/NetSuite/Acompanhamento/130121/MelhoriasNetSuite130121.csv", delimiter=';')


df.columns = df.columns.str.replace(' ', '')    

df['CreatedDate'] = pd.to_datetime(df['CreatedDate'])
df['CompletedDate'] = pd.to_datetime(df['CompletedDate'])
df['DayCompleted'] = df['CompletedDate'].dt.dayofweek
df['DayCreated'] = df['CreatedDate'].dt.dayofweek
df['WeekCreated'] = df['CreatedDate'].dt.isocalendar().week
df['WeekCompleted'] = df['CompletedDate'].dt.isocalendar().week
df['YearCreated'] = df['CreatedDate'].dt.year
df['YearCompleted'] = df['CompletedDate'].dt.year
df['firstCompletedDate'] = df.CompletedDate - df.DayCompleted * timedelta(days=1)
df['firstCreatedDate'] = df.CreatedDate - df.DayCreated * timedelta(days=1)

df['YearWeekCreated'] = df['YearCreated']*100 + df['WeekCreated']
df['YearWeekCompleted'] = df['YearCompleted']*100 + df['WeekCompleted']


df_done = df[df['Progress'] == 'Completed']
df_open = df[df['Progress'] != 'Completed']
df_todo = df[df['BucketName'] == 'To do']
df_doing = df[df['BucketName'] == 'Doing']
df_consult = df[df['BucketName'] == 'Em andamento RSM']
df_open['Priority'].value_counts().sort_index()
df['Priority'].sort_index()

df_backlog_created = df['YearWeekCreated'].value_counts().sort_index()
df_backlog_completed = df['YearWeekCompleted'].value_counts().sort_index()
df_backlog = df_backlog_created.cumsum() - df_backlog_completed.cumsum()




#============================================================================


qtd_created = df['YearWeekCreated'].value_counts().sort_index()
idx_created = qtd_created.index
qtd_completed = df['YearWeekCompleted'].value_counts().sort_index()
idx_completed = qtd_completed.index 
qtd_backlog = df_backlog
idx_backlog = qtd_backlog.index

idx_completed = idx_completed.astype(int)


fig, ax = plt.subplots(figsize=(14,10))



#plt.figure(figsize=(14,10))
ax.plot(idx_created, list(qtd_created), label="Iniciadas", color="r")
ax.plot(idx_completed, list(qtd_completed), label="Completadas", color="y", linewidth=3)
ax.bar(idx_backlog, qtd_backlog, label="Backlog", color="b")
ax.legend(['Novas', 'Fechadas', 'Backlog'])



x=[1,2,3]
y=[9,8,7]


for a,b in zip(idx_created, qtd_created): 
    plt.text(a, b, str(b), fontsize=12, color='w', bbox=dict(facecolor='red', alpha=0.5), horizontalalignment='center')




for a,b in zip(idx_backlog, qtd_backlog): 
    plt.text(a, b, str(b), fontsize=12, color='w', bbox=dict(facecolor='blue', alpha=0.5), horizontalalignment='center')



for a,b in zip(idx_completed, qtd_completed): 
    plt.text(a, b, str(b), fontsize=12, color='black', bbox=dict(facecolor='yellow', alpha=0.5))


plt.title('Backlog', fontsize= 20)


【问题讨论】:

  • 你能把你的代码贴出来详细看看吗?
  • 否则,您需要创建一个虚拟索引列作为每个 x-tick 标签的顺序。氪
  • 请看一下解决方案。您将需要采用该方法,而不是直接采用代码。希望这可以帮助。克罗。

标签: python pandas matplotlib charts


【解决方案1】:

您想要做的称为索引绘图(只需将 y 值传递给 plot,没有 x 值),因此您应该使用 IndexLocator。在以下示例中,您每 4 行设置一个刻度:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mt

np.random.seed(0)
idx = [202026, 202027, 202028, 202029, 202030, 202031, 202032, 202033,
             202035, 202036, 202037, 202038, 202040, 202041, 202043, 202044,
             202045, 202046, 202047, 202048, 202049, 202050, 202051, 202052,
             202101, 202102]
df = pd.DataFrame(np.random.rand(len(idx)), index=idx, columns=['col1'])

fig,ax = plt.subplots()
ax.plot(df.col1.to_numpy())
ax.xaxis.set_major_locator(mt.IndexLocator(4,0))
ax.xaxis.set_ticklabels(df.iloc[ax.get_xticks()].index)

另一种可能性是使用FuncFormatter,特别是如果您想缩放图表,因为它会动态格式化自动定位器刻度:

ax.xaxis.set_major_formatter(mt.FuncFormatter(lambda x,_: f'{df.index[int(x)]}' if x in range(len(df)) else ''))

【讨论】:

    【解决方案2】:

    这不是直接修复您的代码,但原理应该相同。 我将创建一个假数据框并说明问题和解决方案。

    当前空白问题:

    labels = [202026, 202027, 202028, 202029, 202030, 202031, 202032, 202033,
                 202034, 202035, 202036, 202037, 202038, 202040, 202041, 202043,
                 202044, 202045, 202046, 202047, 202048, 202049, 202050, 202051,
                 202052, 202101, 202102]
    y = np.random.rand(len(labels))
    
    # old approach, will have empty space
    _, ax = plt.subplots(1,1)
    ax.plot(labels, y)
    

    建议的解决方案:

    labels = [202026, 202027, 202028, 202029, 202030, 202031, 202032, 202033,
                 202034, 202035, 202036, 202037, 202038, 202040, 202041, 202043,
                 202044, 202045, 202046, 202047, 202048, 202049, 202050, 202051,
                 202052, 202101, 202102]
    y = np.random.rand(len(labels))
    
    # suggested by dummy index
    x_idx = range(len(labels))
    _, ax = plt.subplots(1,1)
    ax.plot(x_idx, y)
    ax.set_xticks(x_idx[::5])
    ax.set_xticklabels(labels[::5])
    

    希望这对您有用。氪

    【讨论】:

    • 您的解决方案有两个主要问题:1)刻度标签与数据不对应(数据上升到 202102,而轴标签在 202032 之后结束),2)如果您平移/缩放你的轴,你会得到奇怪的 x 轴标签。所以最好不要在自动定位器上设置固定标签。
    猜你喜欢
    • 2019-01-19
    • 2015-09-22
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多