Using this, you can directly import the necessary data from your database and plot it on the fly.
In this post, we will plot the Tesla supercharging stations on the world map. We will use a MySQL database containing this data.
Download the Plotly Database Connector App
If you are on Windows, make sure the application folder is in your C:/Program Files folder.
Launch the application by executing it. You may need to start the application as an administrator.
Go for the Open Session option.
Setup the MySQL database
Download the SQL schema file (tesla.sql) that contains the details of Tesla supercharger stations.
Login to your MySQL service with the suitable user (root for this post).
$ mysql -u root -p
Enter the password for user root, when asked.
Create a database named tesla and select it, as explained below.
mysql> CREATE DATABASE tesla;
Query OK, 1 row affected (0.01 sec)
mysql> use tesla;
Source the schema file to populate the database. It will create a table named superchargers in the database tesla, and insert the data rows.
mysql> source tesla.sql
It will take some time to insert all the rows.
Connect to the database
Select the database type in the application, MySQL is selected by default.
Fill in the credentials and press the Connect button.
We can leave the host and password fields, because it will automatically use the default values for local MySQL service.
Once connected, we can preview the database content in the application.
Query the database
Go to the Plotly 2.0, and click on the Import button in the top-right area.
Select the SQL option and press Connect.
Database Connector runs on plain HTTP protocol as of now, you must follow insecure connections in order to use it.
Just make sure your browser’s safe scripting setting is turned off. While using Chrome Browser, you will have to load unsafe scripts.
Once connected, you can see a SQL Editor. Select the appropriate database and run any query of your choice.
Plot the supercharging stations
Once we have connected the database with Plotly 2.0, visualizing the data is just the matter of correct database queries and selecting chart attributes according to the query result.
For example, we will query the geographic details (latitude, longitude, and location columns) to plot all the supercharging station around the world.
We are going to use Atlas Maps as the Chart type.
As you can see in the left panel, here are the chart attributes we have selected for the plot.
- Latitude – latitude
- Longitude – longitude
- Hover Text – location
You can see the chart preview as you change the attributes. Options are available to save and share the chart.
Here is the resultant map, embedded in the post.
Satellite map of tesla supercharging stations
Plotly also supports Satellite Maps using Mapbox.
Here is the satellite map using the same data.
Charging station distribution per country
Type the following query in the SQL Editor. It will collect the no. of stations per country.
SELECT country, COUNT(country) as stations FROM superchargers
GROUP BY country
ORDER BY stations
Select the chart attributes as shown below.
Geographic distribution of Supercharging stations
Using the same query, we can generate a Choropleth map to show the geographic distribution of stations per country.
Similarly, you can use Plotly Database Connector to plot your database content easily.