DelphiPythonPython GUIWindows

Learn To Work With Real-World Graphics Using The Python Matplotlib Library In A Delphi Windows App

blogbanner3 27

With the growing demand for Data Science and Analytics skill sets, drawing graphics programmatically is a very popular task these days. You can easily solve it by combining the Matplotlib library with Python4Delphi (P4D). P4D is a free set of powerful tools that allows you to work with Python scripts, modules, and types in Delphi and easily create Windows GUI with it.

Matplotlib is a comprehensive Python library for creating static, animated, and interactive visualizations. Matplotlib produces publication-quality figures in a variety of hardcopy formats and interactive environments across platforms. Matplotlib can be used in Python scripts, the Python and IPython shell, web application servers, and various graphical user interface toolkits (in this post, Python GUI by Delphi’s VCL using P4D!).

This post will guide you on how to run the Matplotlib library using Python for Delphi to display it in the Delphi Windows GUI app.

First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute script button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.

0 rundemo1 6813476

Python Matplotlib library provides various tools for working with graphics. With this library, you can create graphics, customize legends, style sheets, color schemes, and manipulate images.

Matplotlib enables us to:

Create:

  • Develop publication-quality plots with just a few lines of code
  • Use interactive figures that can zoom, pan, update, etc.

Customize:

  • Take full control of line styles, font properties, axes properties, etc.
  • Export and embed to various file formats and interactive environments

Extend:

  • Explore tailored functionality provided by third-party packages
  • Learn more about Matplotlib through the many external learning resources (very active learning ecosystem)

The following are some real-world use cases in plotting with Matplotlib:

1. Show Percentiles as Horizontal Bar Chart

Bar charts are useful for visualizing counts, or summary statistics with error bars.

This example comes from an application in which grade school gym teachers wanted to be able to show parents how their child did across a handful of fitness tests, and importantly, relative to how other children did. For demo purposes, we’ll just make up some data:

Run the code above in our Python GUI, we will get the following result:

1 1barchartdemo 2868688

2. Stacked Horizontal Bar Chart to Visualize Discrete Distribution

We can visualize discrete distributions as stacked bar charts.

In this Section, we will visualize the result of a survey in which people could rate their agreement to questions on a five-element scale. Let’s run the following code:

The result:

1 3horizontalbarchartdistributiondemo 7633309

3. Creating Annotated Heatmaps

It is often desirable to show data that depends on two independent variables as a color-coded image plot. This is often referred to as a heatmap.

If the data is categorical, this would be called a categorical heatmap.

The following examples show how to create a heatmap with annotations:

 

Run the code above in our Python GUI, we will get the following result:

1 4annotatedheatmap 5542070

Congratulations, now you have learned how to run the Matplotlib library using Python for Delphi to display it in the Delphi Windows GUI app! Now you can solve various real-world problems using plots created by the Matplotlib library and Python4Delphi. The only limitation is your imagination.


Check out the matplotlib data visualization library for Python and use it in your projects: https://pypi.org/project/matplotlib/ and

Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi: https://github.com/pyscripter/python4delphi


References & further readings

[1] Hakim, M. A. (2023).

Article02 – Matplotlib. pythongui.orgRepo_Python4Delphi-Python-Libraries GitHub. github.com/MuhammadAzizulHakim/pythongui.org Repo_Python4Delphi-Python-Libraries/tree/main/Article02%20-%20Matplotlib

[2] Hunter, J., Dale, D., Firing, E., Droettboom, M., and the Matplotlib development team. (2002-2023).

Creating annotated heatmaps: A simple categorical heatmap. The Matplotlib development team. matplotlib.org/stable/gallery/images_contours_and_ fields/image_annotated_heatmap.html#sphx-glr-gallery-images-contours-and-fields-image-annotated-heatmap-py

[3] Hunter, J., Dale, D., Firing, E., Droettboom, M., and the Matplotlib development team. (2002-2023).

Discrete distribution as horizontal bar chart. The Matplotlib development team. matplotlib.org/stable/gallery/lines_bars_ and_markers/horizontal_barchart_distribution.html# sphx-glr-gallery-lines-bars-and-markers-horizontal-barchart-distribution-py

[4] Hunter, J., Dale, D., Firing, E., Droettboom, M., and the Matplotlib development team. (2002-2023).

Percentiles as horizontal bar chart. The Matplotlib development team. matplotlib.org/stable/gallery/statistics/ barchart_demo.html#sphx-glr-gallery-statistics-barchart-demo-py

Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Matplotlib Library?

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Pillow Library?

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Weather App With The Python Delphi Ecosystem and Weatherstack API

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Music Player With The Python Delphi Ecosystem

Leave a Reply

Your email address will not be published. Required fields are marked *