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 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

close
Related posts
DelphiDelphiVCLPythonPython GUIWindows

What is DelphiVCL.Application.CreateHandle?

DelphiDelphiVCLPythonPython GUIWindows

Creating A New Application With DelphiVCL.Application.Create

DelphiDelphiVCLPythonPython GUIWindows

All You Need To Know About Application.DialogHandle

DelphiDelphiVCLPythonPython GUIWindows

What is the FieldAddress Property in DelphiVCL.Application?

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

it_ITItalian