3

My question directly relates to How to embed matplotlib in pyqt - for Dummies. The graph is a line chart in the original post. How do I change the type to area or bar chart?

I tried this ax.plot(data, kind="area") but it does not work.

Furthermore, I want to illustrate pandas pivot in PyQt. I usually use this code pivot_number.plot(kind='area', stacked=True) to show pivot on Matplotlib. How should I change the original post to pandas pivot?

This is the Pandas Pivot.

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)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

2

Typically the plot methods of other libraries such as pandas accept the axis as an argument:

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

If you want to draw an area without using pandas then you have to do the same as the official example indicates.

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

and 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

Or 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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • If you guys are having problems with importing FigureCanvas in these given codes, use "from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas" instead. – lloydyu24 May 21 '22 at 06:46