【问题标题】:How to use matplotlib to plot complex bar graphs–multiple subplots with multiple rows of bars?如何使用 matplotlib 绘制复杂的条形图——具有多行条形的多个子图?
【发布时间】:2020-07-20 23:32:33
【问题描述】:

我有一个如下示例数据框:

import pandas as pd

df = pd.DataFrame(np.random.randint(
    0, 10, size=(1000, 11)), columns=list('ABCDEFGHIJK'))

所需但未经修饰的输出如下所示:

数据框中每一列的数据被绘制为一个带有五行条形的子图。

我更喜欢使用 matplotlib,因为我可以相对轻松地使图表看起来不错。但它的性能似乎很慢。

【问题讨论】:

  • 你的阴谋如何?
  • @gcoronel99 我使用 pyqtgraph 生成此图,但我对包不是很熟悉。
  • 您愿意使用 Seaborn 吗?
  • @gcoronel99 是的!那就更好了。

标签: python pandas matplotlib plot


【解决方案1】:

您可以使用barbottom 参数来偏移各个行。
以下未优化的示例演示了这种方法:

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

df = pd.DataFrame(np.random.randint(0, 10, size=(1000, 11)), columns=list('ABCDEFGHIJK'))

fig = plt.figure()

for i,c in enumerate(df.columns):
    ax = fig.add_subplot(3, 4, i+1)
    for x,h,b in zip((df.index.to_numpy() % 200).reshape(-1, 200), df[c].to_numpy().reshape(-1, 200), (df.index.to_numpy() // 200 * 10).reshape(-1, 200)):
        ax.set_title(c)
        ax.bar(x, h, bottom=b, color='k' )

【讨论】:

    猜你喜欢
    • 2019-06-24
    • 1970-01-01
    • 2022-01-09
    • 2019-09-28
    • 2021-04-22
    • 2022-07-07
    • 1970-01-01
    • 2017-09-06
    • 1970-01-01
    相关资源
    最近更新 更多