With the growing demand for Data Science and Analytics skill sets, drawing graphics programmatically is a very popular task these days. Especially the need for high quality, real-time, and interactive graphics. You can easily create a GUI app for that purpose by combining the power of the Plotly 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 Data Visualization Tools Python.
Table of Contents
What is Plotly?
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.
This post will guide you on how to run the Plotly library using Python for Delphi to display it in the Delphi Windows GUI app.
How do I enable Plotly for data Visualization inside Python4Delphi on Windows?
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.
How do I install Plotly in Python?
Here is how you can get Plotly using the pip command:
1 |
pip install plotly |
To test the installation, let’s run this script to visualize the Iris dataset using a basic scatterplot:
1 2 3 4 5 6 7 |
import plotly.express as px df = px.data.iris() fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species") fig.show() # http://127.0.0.1:63764/ |
Let’s see the result:
How can I visualize real-world data using Plotly inside a Windows GUI app?
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() |
What does Plotly look like when it is running?
Let’s see the result:
Check out the full source code here!
Congratulations, now you have learned how to run the Plotly 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 Plotly library and Python4Delphi.
Do you have more examples of using Plotly and other types of data visualizations in Windows GUI apps?
Check out the Plotly data visualization library for Python and use it in your projects: https://pypi.org/project/plotly/ and
Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi: https://github.com/pyscripter/python4delphi
Or read our collections of Data Visualization articles:
Matplotlib:
Bokeh:
Seaborn:
NetworkX: