[Python] Matplotlib vertical grid lines not match points

Discussão em 'Python' iniciado por Stack, Abril 21, 2025.

  1. Stack

    Stack Membro Participativo

    Could you please explain why vertical grid lines not match points?

    [​IMG]

    Here is my data for plot:

    {datetime.datetime(2025, 4, 15, 19, 23, 50, 658000, tzinfo=datetime.timezone.utc): 68.0, datetime.datetime(2025, 4, 16, 19, 31, 1, 367000, tzinfo=datetime.timezone.utc): 72.0, datetime.datetime(2025, 4, 17, 19, 34, 21, 507000, tzinfo=datetime.timezone.utc): 75.0, datetime.datetime(2025, 4, 18, 19, 50, 28, 446000, tzinfo=datetime.timezone.utc): 80.0, datetime.datetime(2025, 4, 19, 19, 57, 15, 393000, tzinfo=datetime.timezone.utc): 78.0, datetime.datetime(2025, 4, 20, 19, 57, 49, 60000, tzinfo=datetime.timezone.utc): 77.0, datetime.datetime(2025, 4, 21, 20, 28, 51, 127710, tzinfo=datetime.timezone.utc): 73.0}

    And here is my code:

    fig, ax = plt.subplots(figsize=(12, 6))

    ax.plot(df['Дата'], df['Вес'], marker='o', linestyle='-', color='royalblue', label='Вес')

    ax.scatter(df['Дата'], df['Вес'], color='red', zorder=5)

    ax.set_title('График изменения веса по дням', fontsize=16)
    ax.set_xlabel('Дата', fontsize=12)
    ax.set_ylabel('Вес (кг)', fontsize=12)
    ax.xaxis.set_major_locator(mdates.AutoDateLocator()) # Раз в день

    ax.xaxis.set_major_formatter(mdates.DateFormatter('%d.%m.%Y'))

    plt.setp(ax.xaxis.get_majorticklabels(), rotation=45, ha="right")

    ax.grid(True, linestyle='--', alpha=0.6)

    plt.tight_layout()

    Continue reading...

Compartilhe esta Página