Learning to plot with python and AI

In this exercise we use a chatbot to make python plots. This follows on from the section on python plotting. Once you have python installed on your computer and you are comfortable with making basic python plots with the Module 1 flow data.

Visualising Module 1 Flow Data

Setting up

Creating a .py file with VS Code

  1. Refer to the Getting Started section, to install python and its packages, as well as Visual Studio Code (VSC). Refer to the Chatbot section of Getting Started to set up Copilot in VSC.
  2. Open Visual Studio Code, the go File -> Open Folder then open the folder where you will store your files for this exercise (ENVT3362_workshop_2)
  3. Download the spreadsheet for this workshop here
  4. Move this to your ENVT3362_workshop_2 directory
  5. Make a new python file in VSC. Go File -> New File -> New python file and save a file as a ‘.py’
  6. Open two new terminals in VSC. Open one as python and one as Powershell.
  7. To use python in the python terminal, use a python command (e.g. ‘plot’). To use it in the Powershell terminal, first write ‘python’ and then the python command (e.g. ‘python plot’). If VSC cannot find python, write the whole path to where python is installed on your computer (e.g. ‘C:.exe plot’).

Importing and formatting the data

Load the necessary packages

This section should be the same as the setup for the python plotting exercise. Add these commands to the top of your python file. These are python packages that we are loading with the command ‘import’. This is similar to the R function ‘library()’. The ‘as’ command gives a shorter name to the package, which we can choose, for future reference throughout the script.

import pandas as pd
import matplotlib.pyplot as plt 
import matplotlib.dates as mdates 
import matplotlib.patches as patches

from datetime import datetime

Run these lines by pressing Shift Enter, one by one. This is the equivalent of Ctrl Enter in R.

If importing these packages does not work, it may be that the location of your python files is not being found. To fix this, in the VSC Powershell terminal, use a python command starting with the location of your python program location. For example:

C:\Users\Jimmy\AppData\python\python.exe 
import pandas as pd

Similarly you may need to specify the whole file path to pip in powershell, for example:

C:\Users\Jimmy\AppData\python\pip3.exe 

Import the spreadsheet

Drag the ‘envFlowData.xlsx’ spreadsheet into the Copilot chat window. Copliot can now read the spreadsheet and it will offer you some options for how to summarise this data. It should be able to read the column headings. (There is a limit to the file size and the number of data points that the chat can handle.)

Inspect the data

Display a header of the data, by asking Copilot to display a header of the data.

Plotting with matplotlib via Copilot

Follow the steps of the previous exercises to generate python scripts that plot this dataset.