With the growing demand for Data Science and Analytics skill sets, drawing stunning graphics programmatically is one of the most popular tasks these days. Especially the high-quality plots that will easily give insights and effectively tell stories visually to the users. You can easily solve it by combining the Seaborn 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 GUIs with Python Windows GUI Builder.
Table of Contents
What is Seaborn?
Seaborn is a library for making statistical graphics in Python. It is built on top of matplotlib and closely integrated with pandas data structures. Seaborn will enhance the matplotlib plotting functionalities.
What can the Seaborn library do for my apps?
Here is some of the functionality that Seaborn offers:
- A dataset-oriented API for examining relationships between multiple variables
- Specialized support for using categorical variables to show observations or aggregate statistics
- Options for visualizing univariate or bivariate distributions and for comparing them between subsets of data
- Automatic estimation and plotting of linear regression models for different kinds of dependent variables
- Convenient views onto the overall structure of complex datasets
- High-level abstractions for structuring multi-plot grids that let you easily build complex visualizations
- Concise control over matplotlib figure styling with several built-in themes
- Tools for choosing color palettes that faithfully reveal patterns in your data
Seaborn aims to make visualization a central part of exploring and understanding data. Its dataset-oriented plotting functions operate on data frames and arrays containing whole datasets and internally perform the necessary semantic mapping and statistical aggregation to produce informative plots.
Can you show me a demo of how to use the Seaborn library in my apps?
This post will guide you on how to run the Seaborn 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.
Here is the sample of how seaborn would enhance matplotlib and pandas scatterplot, inside Python4Delphi GUI:
1. Scatterplot with Multiple Semantics
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import seaborn as sns import matplotlib.pyplot as plt sns.set_theme(style="whitegrid") # Load the example diamonds dataset diamonds = sns.load_dataset("diamonds") # Draw a scatter plot while assigning point colors and sizes to different # variables in the dataset f, ax = plt.subplots(figsize=(6.5, 6.5)) sns.despine(f, left=True, bottom=True) clarity_ranking = ["I1", "SI2", "SI1", "VS2", "VS1", "VVS2", "VVS1", "IF"] sns.scatterplot(x="carat", y="price", hue="clarity", size="depth", palette="ch:r=-.2,d=.3_r", hue_order=clarity_ranking, sizes=(1, 8), linewidth=0, data=diamonds, ax=ax) plt.show() |
The output in Python GUI:
2. Scatterplot with Continuous Hues and Sizes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import seaborn as sns sns.set_theme(style="whitegrid") # Load the example planets dataset planets = sns.load_dataset("planets") cmap = sns.cubehelix_palette(rot=-.2, as_cmap=True) g = sns.relplot( data=planets, x="distance", y="orbital_period", hue="year", size="mass", palette=cmap, sizes=(10, 200), ) g.set(xscale="log", yscale="log") g.ax.xaxis.grid(True, "minor", linewidth=.25) g.ax.yaxis.grid(True, "minor", linewidth=.25) g.despine(left=True, bottom=True) plt.show() |
The output in Python GUI:
3. Scatterplot with Varying Point Sizes and Hues
1 2 3 4 5 6 7 8 9 10 11 12 |
import seaborn as sns sns.set_theme(style="white") # Load the example mpg dataset mpg = sns.load_dataset("mpg") # Plot miles per gallon against horsepower with other semantics sns.relplot(x="horsepower", y="mpg", hue="origin", size="weight", sizes=(40, 400), alpha=.5, palette="muted", height=6, data=mpg) plt.show() |
The output in Python GUI:
Congratulations, now you have learned how to run the Seaborn 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 Seaborn library and Python4Delphi.
Check out the Seaborn data visualization library for Python and use it in your projects: https://pypi.org/project/seaborn/ and
Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi: https://github.com/pyscripter/python4delphi