gpt4 book ai didi

python - 如何使用 Pandas 在 pyqt 中将图表类型更改为面积图或条形图?

转载 作者:行者123 更新时间:2023-12-04 07:21:56 27 4
gpt4 key购买 nike

我的问题与How to embed matplotlib in pyqt - for Dummies 直接相关.该图是原始帖子中的折线图。如何将类型更改为面积条形图表?

我试过这个 ax.plot(data, kind="area") 但它不起作用。

此外,我想在 PyQt 中说明 pandas pivot。我通常使用此代码 pivot_number.plot(kind='area', stacked=True) 在 Matplotlib 上显示枢轴。我应该如何将原始帖子更改为 pandas pivot?

这是 Pandas 枢轴。

pivot_number = cohorts.pivot(columns="cohort_group", index="year", values='total_customers')

cohort_group 2010 2011 2012 2013 ... 2016 2017 2018 2019
year ...
2010 552.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
2011 496.0 128.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
2012 448.0 92.0 152.0 0.0 ... 0.0 0.0 0.0 0.0
2013 408.0 80.0 116.0 132.0 ... 0.0 0.0 0.0 0.0
2014 388.0 64.0 104.0 128.0 ... 0.0 0.0 0.0 0.0
2015 360.0 64.0 68.0 108.0 ... 0.0 0.0 0.0 0.0
2016 348.0 56.0 72.0 100.0 ... 100.0 0.0 0.0 0.0
2017 320.0 52.0 64.0 76.0 ... 88.0 188.0 0.0 0.0
2018 300.0 44.0 68.0 64.0 ... 80.0 172.0 64.0 0.0
2019 284.0 32.0 56.0 56.0 ... 72.0 156.0 52.0 88.0

pivot_number.plot(kind='area', stacked=True)

最佳答案

通常 pandas 等其他库的 plot 方法接受轴作为参数:

import sys
from PyQt5.QtWidgets import QApplication

from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure

import pandas as pd


def main():
app = QApplication(sys.argv)

canvas = FigureCanvas(Figure(figsize=(5, 3)))
canvas.resize(640, 480)
canvas.show()

ax = canvas.figure.subplots()

df = pd.DataFrame(
{
"foo": ["one", "one", "one", "two", "two", "two"],
"bar": ["A", "B", "C", "A", "B", "C"],
"baz": [1, 2, 3, 4, 5, 6],
"zoo": ["x", "y", "z", "q", "w", "t"],
}
)
pivot_number = df.pivot(index="foo", columns="bar", values="baz")
pivot_number.plot(kind="area", stacked=True, ax=ax)

sys.exit(app.exec_())


if __name__ == "__main__":
main()

enter image description here

如果你想在不使用 pandas 的情况下绘制一个区域,那么你必须执行与 the official example 相同的操作。表示。

import sys
from PyQt5.QtWidgets import QApplication

from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure

import numpy as np


def main():
app = QApplication(sys.argv)

canvas = FigureCanvas(Figure(figsize=(5, 3)))
canvas.resize(640, 480)
canvas.show()

ax = canvas.figure.subplots()

x = np.arange(0.0, 2, 0.01)
y1 = np.sin(2 * np.pi * x)
ax.fill_between(x, y1)
ax.set_title("fill between y1 and 0")

sys.exit(app.exec_())


if __name__ == "__main__":
main()

enter image description here

this

import sys
from PyQt5.QtWidgets import QApplication

from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure


def main():
app = QApplication(sys.argv)

canvas = FigureCanvas(Figure(figsize=(5, 3)))
canvas.resize(640, 480)
canvas.show()

ax = canvas.figure.subplots()

names = ["group_a", "group_b", "group_c"]
values = [1, 10, 100]
ax.bar(names, values)

sys.exit(app.exec_())


if __name__ == "__main__":
main()

enter image description here

this :

import sys
from PyQt5.QtWidgets import QApplication

from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure


def main():
app = QApplication(sys.argv)

canvas = FigureCanvas(Figure(figsize=(5, 3)))
canvas.resize(640, 480)
canvas.show()

ax = canvas.figure.subplots()

labels = ["G1", "G2", "G3", "G4", "G5"]
men_means = [20, 35, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]
men_std = [2, 3, 4, 1, 2]
women_std = [3, 5, 2, 3, 3]
width = 0.35 # the width of the bars: can also be len(x) sequence

ax.bar(labels, men_means, width, yerr=men_std, label="Men")
ax.bar(labels, women_means, width, yerr=women_std, bottom=men_means, label="Women")

ax.set_ylabel("Scores")
ax.set_title("Scores by group and gender")
ax.legend()

sys.exit(app.exec_())


if __name__ == "__main__":
main()

enter image description here

关于python - 如何使用 Pandas 在 pyqt 中将图表类型更改为面积图或条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68447458/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com