在 Python pandas 中对齐折线图和条形图

迈克尔

我正在尝试使用 Python 的 pandas 库在条形图上绘制折线图。条形图是来自 DataFrame 的聚集图表,在单独绘制时看起来像这样:

簇状条形图

折线图是整个数据集的平均值:

折线图

如果使用所有折线图,我可以成功地在同一个轴对象上绘制两组数据:

所有折线图

但是,当我将聚集条形的 DataFrame 切换为实际使用条形图并将其与折线图一起绘制时,折线图希望从第二个索引位置开始绘制,从而导致偏移。

带偏移的条和线

这个问题的答案对matplotlib 处理条形图和折线图的 x 轴的方式有一个有趣的评论,这可能与此处相关,但我无法弄清楚如何处理该见解。

解决此对齐问题的好方法是什么?

重现代码:

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

table_data = {2019: {1: np.nan,
2: np.nan,
3: np.nan,
  4: 1.4200000000000002,
  5: 0.8193548387096775,
  6: 1.420689655172414,
  7: 0.4645161290322581,
  8: 0.10322580645161289,
  9: 0.29333333333333333,
  10: 1.7741935483870968,
  11: 0.32,
  12: 6.703225806451614},
 2020: {1: 5.52,
  2: 12.613793103448277,
  3: 0.9428571428571428,
  4: 0.1793103448275862,
  5: 0.39354838709677414,
  6: 1.3866666666666667,
  7: 1.9800000000000002,
  8: 0.6689655172413793,
  9: 0.19333333333333336,
  10: 5.896774193548388,
  11: 0.6896551724137931,
  12: 4.103225806451613},
 2021: {1: 2.7935483870967746,
  2: 5.15,
  3: 9.696774193548388,
  4: 3.74,
  5: 2.8967741935483873,
  6: 0.9103448275862069,
  7: 1.6516129032258065,
  8: 0.3,
  9: 0.38571428571428573,
  10: 5.141935483870968,
  11: 8.58,
  12: 6.052173913043479},
 2022: {1: 2.3923076923076922,
  2: 31.678571428571427,
  3: 8.761290322580646,
  4: np.nan,
  5: np.nan,
  6: np.nan,
  7: np.nan,
  8: np.nan,
  9: np.nan,
  10: np.nan,
  11: np.nan,
  12: np.nan}}

means = {1: 3.6137931034482755,
 2: 16.435294117647057,
 3: 7.132530120481928,
 4: 1.797752808988764,
 5: 1.3698924731182796,
 6: 1.240909090909091,
 7: 1.358695652173913,
 8: 0.3522727272727273,
 9: 0.28863636363636364,
 10: 4.2709677419354835,
 11: 3.2247191011235956,
 12: 5.578823529411765}

df_bars = pd.DataFrame(table_data)
df_means = pd.DataFrame.from_dict(means, orient = 'index', columns = ['Mean'])

# Clustered bar chart by itself
df_bars.plot(kind = 'bar',
           title = 'Average Daily Rainfall by Month',
           ylabel = 'Average Daily Rainfall (mm)',
           figsize = (10, 6)
          )

# Line chart by itself
df_means.plot(
    kind = 'line',
    title = 'Average Daily Rainfall by Month',
    ylabel = 'Average Daily Rainfall (mm)',
    y = 'Mean'
)

# Show all data as line charts. This works OK
ax_avg = df_bars.plot(kind = 'line',
           title = 'Average Daily Rainfall by Month',
           ylabel = 'Average Daily Rainfall (mm)',
           figsize = (10, 6)
          )

df_means.plot(
    ax = ax_avg,
    kind = 'line',
    y = 'Mean'
)
plt.show()

# Show bar data and line chart on the one plot. The line chart is offset!
ax_avg2 = df_bars.plot(kind = 'bar',
           title = 'Average Daily Rainfall by Month',
           ylabel = 'Average Daily Rainfall (mm)',
           figsize = (10, 6)
          )

df_means.plot(
    ax = ax_avg2,
    kind = 'line',
    y = 'Mean'
)
plt.show()
斯科特波士顿

您可以使用reset_index将行数据帧的索引更改回从零开始。

这将允许您的条形图与从零开始的索引对齐,如下所示:

ax_avg2 = df_bars.plot(kind = 'bar',
           title = 'Average Daily Rainfall by Month',
           ylabel = 'Average Daily Rainfall (mm)',
           figsize = (10, 6)
          )

df_means.reset_index().plot(
    ax = ax_avg2,
    kind = 'line',
    y = 'Mean'
)

输出:

在此处输入图像描述

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

pandas DataFrame如何混合不同比例的条形图和折线图

Python绘图条形图和百分比折线图在同一图形上

如何在ZingChart中的折线图和条形图之间对齐垂直参考线?

在JavaScript中组合条形图和折线图

无法使用pandas plot()函数组合条形图和折线图

在ggplot2中组合条形图和折线图(双轴)

Python使用辅助y轴制作条形图和折线图

在python中水平对齐条形图图例

分组条形图和折线图

python中的折线图

Seaborn和Pandas:使用python中的多索引数据制作多个x类别条形图

ggplot组合的条形图和折线图

在一个图中具有组的Python条形图和折线图

python pandas在pandas.plot hbar中绘制折线图

使用pandas和matplotlib绘制叠加图(折线图和条形图)

Python Pandas条形图-更改特定条形的颜色

如何对齐条形图和两个折线图?

组合条形图和折线图(python)时出现问题

如何在ggplot2中绘制组合的条形图和折线图

如何通过迭代python中的pandas列制作折线图?

如何使用ggplot在一个图表中创建条形图和折线图

如何在双Y轴图中的ggvis中对齐条形图和折线图?

Highcharts获得组合的条形图和折线图

Python:使用 DataFrame 绘制折线图和条形图时 DatetimeIndex 的不同行为

D3.js 组合条形图和折线图 x 轴未对齐问题

Flot:在一张图表中组合堆积的条形图和折线图

在 Python 中给定 Pandas 数据框绘制多个堆叠条形图

Python Pandas xlsxriter 堆积折线图类型在Excel中设置标准折线图

在 pandas python 中处理折线图