In this article, you’ll learn what data visualization is, how to start learning data visualization in windows, using Python Windows GUI Builder with data visualization, How to use Matplotlib, Seaborn, Bokeh, NetworkX, and Plotly Python libraries for Data Visualization Tasks, Python GUI data visualization results, and many more.
Table of Contents
What is data visualization?
Data Visualization is an interdisciplinary field that deals with the graphic representation of data. It is a particularly efficient way of communication when the data is diverse and potentially complex.
Visualizing data, whether in charts, graphs, or some other form, is important because it can give meaning to the data for a broader audience. “Visualization gives us a way to parse and understand data so that we can add it to our stories, we can incorporate it into our thinking,” says Ellie Fields, Senior Vice President of product development at Tableau.
How do I start learning data visualization in Windows?
With a plethora of online tools and software packages for Data Visualization available, online classes, and resources to help you hone your skills, the entry barrier for creating compelling Data Visualization is lower than ever before. That means you can become proficient in presenting data visually “without a real background” in computer science, data science, or programming.
But with so many options, finding a place to start can be overwhelming. In this tutorial, we’ll introduce you to how to integrate any Python’s Data Visualization libraries with Embarcadero’s Delphi, using Python4Delphi (P4D) to get you started in creating Windows GUI apps.
How does Python4Delphi help with data visualization?
P4D empowers Python users with Delphi’s award-winning VCL functionalities for Windows which enables us to build native Windows apps 5x faster. This integration enables us to create a modern GUI with Windows 10 looks and responsive controls for our Python Data Visualization applications. Python4Delphi also comes with an extensive range of demos, use cases, and tutorials.
How to use Matplotlib, Seaborn, Bokeh, NetworkX, and Plotly Python libraries to perform data visualization tasks
All of the libraries we show in this demo can be integrated with Python4Delphi for you to easily create Windows Apps with data visualization capabilities.
Prerequisites: Before we begin to work, download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out the easy instructions found in the Getting Started With Python4Delphi video by Jim McKeeth.
Time to get started!
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.
1. How do I Visualize Data with Matplotlib in Windows?
Matplotlib is one of the most popular and oldest data visualization libraries in Python which is commonly used in exploratory data analysis and machine learning. In data science and machine learning, Matplotlib helps us to gain insights from a huge amount of data through different data visualization techniques.
With the growing demand for data science and analytics skill sets, visualizing data programmatically is one of the most crucial tasks these days. You can easily solve these tasks by combining the Matplotlib library with Python4Delphi (P4D). Matplotlib can be used in Python scripts, in the Python and IPython shell, on web application servers, and various graphical user interface toolkits (in this post, Python GUI by Delphi’s VCL using P4D)!
After installing Python4Delphi properly, you can get Matplotlib using pip or easy install to your command prompt:
1 |
pip install matplotlib |
and don’t forget to put the path where your Matplotlib installed, to the System Environment Variables, shown in this example:
1 2 3 |
C:/Users/YOUR_USERNAME/AppData/Local/Programs/Python/Python38/Lib/site-packages C:/Users/YOUR_USERNAME/AppData/Local/Programs/Python/Python38/Scripts C:/Users/YOUR_USERNAME/AppData/Local/Programs/Python/Python38 |
The following is a code example of Matplotlib to create an annotated heatmap (run this inside the lower Memo of Python4Delphi Demo01 GUI):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import numpy as np import matplotlib import matplotlib.pyplot as plt vegetables = ["cucumber", "tomato", "lettuce", "asparagus", "potato", "wheat", "barley"] farmers = ["Farmer Joe", "Upland Bros.", "Smith Gardening", "Agrifun", "Organiculture", "BioGoods Ltd.", "Cornylee Corp."] harvest = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0], [2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0], [1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0], [0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0], [0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0], [1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1], [0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]]) fig, ax = plt.subplots() im = ax.imshow(harvest) # We want to show all ticks... ax.set_xticks(np.arange(len(farmers))) ax.set_yticks(np.arange(len(vegetables))) # ... and label them with the respective list entries ax.set_xticklabels(farmers) ax.set_yticklabels(vegetables) # Rotate the tick labels and set their alignment. plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor") # Loop over data dimensions and create text annotations. for i in range(len(vegetables)): for j in range(len(farmers)): text = ax.text(j, i, harvest[i, j], ha="center", va="center", color="w") ax.set_title("Harvest of local farmers (in tons/year)") fig.tight_layout() plt.show() |
Here is the result in Python GUI
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.
2. How do I visualize data with Seaborn in Windows?
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.
Here is some of the functionality offered by Seaborn:
- 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
Do you want to improve Matplotlib plots using Seaborn?
For example, you could create a Scatterplot with varying point sizes and hues in the Windows GUI app?
This section will show you how to get started!
First, here is how you can get Seaborn :
1 |
pip install seaborn |
The following is an introductory example of Seaborn to a Scatterplot with varying point sizes and hues (run this inside the lower Memo of Python4Delphi Demo01 GUI):
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() |
Here is the Seaborn result in the Python GUI:
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.
3. How do I visualize data with Bokeh in Windows?
Bokeh is an interactive visualization library to be shown in modern web browsers. It provides elegant, concise construction of versatile graphics, and affords high-performance interactivity over large or streaming datasets. Bokeh can help anyone who would like to quickly and easily make interactive plots, dashboards, and data applications.
At a glance, Bokeh provides us with:
- Flexibility
Bokeh makes it simple to create common plots but also can handle custom or specialized use-cases.
- Interactivity
Tools and widgets let you and your audience probe “what if” scenarios or drill-down into the details of your data.
- It’s shareable
Plots, dashboards, and apps can be published on web pages or Jupyter notebooks.
- It increases productivity
Work in Python close to all the PyData tools you are already familiar with.
- Bokeh is powerful
You can always add custom JavaScript to support advanced or specialized cases.
- Bokeh is Open Source
Everything, including the Bokeh server, is licensed under the BSD license and available on GitHub.
Getting and installing Bokeh
This section will guide you to combine Python4Delphi with the Bokeh library, inside Delphi and C++Builder, from installing Bokeh with pip to how to make an interactive plot of the interactive of an unemployment rate.
First, here is how you can get Bokeh
1 |
pip install bokeh |
Using Bokeh for Python data visualization
The following is a code example of Bokeh to produce an interactive plot of the unemployment rate in Texas (run this inside the lower Memo of Python4Delphi Demo01 GUI):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
from bokeh.io import show from bokeh.models import LogColorMapper from bokeh.palettes import Viridis6 as palette from bokeh.plotting import figure from bokeh.sampledata.unemployment import data as unemployment from bokeh.sampledata.us_counties import data as counties palette = tuple(reversed(palette)) counties = { code: county for code, county in counties.items() if county["state"] == "tx" } county_xs = [county["lons"] for county in counties.values()] county_ys = [county["lats"] for county in counties.values()] county_names = [county['name'] for county in counties.values()] county_rates = [unemployment[county_id] for county_id in counties] color_mapper = LogColorMapper(palette=palette) data=dict( x=county_xs, y=county_ys, name=county_names, rate=county_rates, ) TOOLS = "pan,wheel_zoom,reset,hover,save" p = figure( title="Texas Unemployment, 2009", tools=TOOLS, x_axis_location=None, y_axis_location=None, tooltips=[ ("Name", "@name"), ("Unemployment rate", "@rate%"), ("(Long, Lat)", "($x, $y)") ]) p.grid.grid_line_color = None p.hover.point_policy = "follow_mouse" p.patches('x', 'y', source=data, fill_color={'field': 'rate', 'transform': color_mapper}, fill_alpha=0.7, line_color="white", line_width=0.5) show(p) |
Bokeh Python4Delphi results
4. How do I visualize data with NetworkX in Windows?
NetworkX is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks.
Briefly, NetworkX provides us with:
- Data structures for graphs, digraphs, and multigraphs.
- Many standard graph algorithms.
- Network structure and analysis measures.
- Generators for classic graphs, random graphs, and synthetic networks.
- Nodes can be “anything” (e.g., text, images, XML records).
- Edges can hold arbitrary data (e.g., weights, time-series).
- Open source 3-clause BSD license.
- Well tested with over 90% code coverage.
- Additional benefits from Python include fast prototyping, easy to teach, and multi-platform
Here is how you can get NetworkX
1 |
pip install networkx |
The following is a code example of NetworkX to create a Random Geometric Graph (run this inside the lower Memo of Python4Delphi Demo01 GUI):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import matplotlib.pyplot as plt import networkx as nx G = nx.random_geometric_graph(200, 0.125) # position is stored as node attribute data for random_geometric_graph pos = nx.get_node_attributes(G, "pos") # find node near center (0.5,0.5) dmin = 1 ncenter = 0 for n in pos: x, y = pos[n] d = (x - 0.5) ** 2 + (y - 0.5) ** 2 if d < dmin: ncenter = n dmin = d # color by path length from node near center p = dict(nx.single_source_shortest_path_length(G, ncenter)) plt.figure(figsize=(8, 8)) nx.draw_networkx_edges(G, pos, nodelist=[ncenter], alpha=0.4) nx.draw_networkx_nodes( G, pos, nodelist=list(p.keys()), node_size=80, node_color=list(p.values()), cmap=plt.cm.Reds_r, ) plt.xlim(-0.05, 1.05) plt.ylim(-0.05, 1.05) plt.axis("off") plt.show() |
NetworkX Data Visualization Result
5. How do I visualize data with Plotly in Windows?
Plotly or plotly.py is an interactive, open-source, and browser-based data visualization library for Python.
Built on top of plotly.js, plotly.py is a high-level, declarative charting library. plotly.js ships with over 30 chart types, including scientific charts, 3D graphs, statistical charts, SVG maps, financial charts, and more. plotly.py is MIT Licensed.
Are you looking for a simple, flexible, and powerful data visualization library, and build a nice GUI for it? You can deliver enterprise-grade and publication-quality graphs easily by combining Plotly and Python4Delphi library, inside Delphi and C++Builder.
Installing Plotly for Data Visualization
1 |
pip install plotly |
A Plotly Python code example
The following is a code example of Plotly to visualize the famous 1962-2006 Walmart Store Openings dataset (run this inside the lower Memo of Python4Delphi Demo01 GUI):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
import plotly.graph_objects as go import pandas as pd df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/1962_2006_walmart_store_openings.csv') df.head() data = [] layout = dict( title = 'New Walmart Stores per year 1962-2006<br> Source: <a href="http://www.econ.umn.edu/~holmes/data/WalMart/index.html"> University of Minnesota</a>', # showlegend = False, autosize = False, width = 1000, height = 900, hovermode = False, legend = dict( x=0.7, y=-0.1, bgcolor="rgba(255, 255, 255, 0)", font = dict( size=11 ), ) ) years = df['YEAR'].unique() for i in range(len(years)): geo_key = 'geo'+str(i+1) if i != 0 else 'geo' lons = list(df[ df['YEAR'] == years[i] ]['LON']) lats = list(df[ df['YEAR'] == years[i] ]['LAT']) # Walmart store data data.append( dict( type = 'scattergeo', showlegend=False, lon = lons, lat = lats, geo = geo_key, name = int(years[i]), marker = dict( color = "rgb(0, 0, 255)", opacity = 0.5 ) ) ) # Year markers data.append( dict( type = 'scattergeo', showlegend = False, lon = [-78], lat = [47], geo = geo_key, text = [years[i]], mode = 'text', ) ) layout[geo_key] = dict( scope = 'usa', showland = True, landcolor = 'rgb(229, 229, 229)', showcountries = False, domain = dict( x = [], y = [] ), subunitcolor = "rgb(255, 255, 255)", ) def draw_sparkline( domain, lataxis, lonaxis ): ''' Returns a sparkline layout object for geo coordinates ''' return dict( showland = False, showframe = False, showcountries = False, showcoastlines = False, domain = domain, lataxis = lataxis, lonaxis = lonaxis, bgcolor = 'rgba(255,200,200,0.0)' ) # Stores per year sparkline layout['geo44'] = draw_sparkline({'x':[0.6,0.8], 'y':[0,0.15]}, {'range':[-5.0, 30.0]}, {'range':[0.0, 40.0]} ) data.append( dict( type = 'scattergeo', mode = 'lines', lat = list(df.groupby(by=['YEAR']).count()['storenum']/1e1), lon = list(range(len(df.groupby(by=['YEAR']).count()['storenum']/1e1))), line = dict( color = "rgb(0, 0, 255)" ), name = "New stores per year<br>Peak of 178 stores per year in 1990", geo = 'geo44', ) ) # Cumulative sum sparkline layout['geo45'] = draw_sparkline({'x':[0.8,1], 'y':[0,0.15]}, {'range':[-5.0, 50.0]}, {'range':[0.0, 50.0]} ) data.append( dict( type = 'scattergeo', mode = 'lines', lat = list(df.groupby(by=['YEAR']).count().cumsum()['storenum']/1e2), lon = list(range(len(df.groupby(by=['YEAR']).count()['storenum']/1e1))), line = dict( color = "rgb(214, 39, 40)" ), name ="Cumulative sum<br>3176 stores total in 2006", geo = 'geo45', ) ) z = 0 COLS = 5 ROWS = 9 for y in reversed(range(ROWS)): for x in range(COLS): geo_key = 'geo'+str(z+1) if z != 0 else 'geo' layout[geo_key]['domain']['x'] = [float(x)/float(COLS), float(x+1)/float(COLS)] layout[geo_key]['domain']['y'] = [float(y)/float(ROWS), float(y+1)/float(ROWS)] z=z+1 if z > 42: break fig = go.Figure(data=data, layout=layout) fig.update_layout(width=800) fig.show() |
Using Python and Plotly for powerful data visualization
If you are interested in machine learning, artificial intelligence, or data science, then take a look at a new way to write chief Delphi code against Python libraries in this article.
Want to know some more? Then check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition