top of page
Search
  • Writer's pictureLaura Caicedo

Setting-up a python virtual env and plot a graph by Zahara Miriam.

Could you teach our Lovelace Series audience something technical in less than 5 minutes?


Sure! Here is how to set-up a python virtual env and plot a graph:


Setting up a Virtual Environment:


1. Open your terminal or command prompt.

2. Navigate to the directory where you want to create your project using the `cd` command.

3. Create a new virtual environment by running:


```
python -m venv myenv
```

This creates a new directory called `myenv` containing the virtual env.


4. Activate the virtual env:

   - On Windows: `myenv\Scripts\activate`

   - On Unix/Linux: `source myenv/bin/activate`


Your terminal now shows `(myenv)` at the start to show the virtual env is active.


Installing Jupyter Notebook and Libraries:


Now we want to install soem libraries to help us plot a graph:


5. Install Jupyter Notebook, pandas, and matplotlib by running:


```
pip install jupyter pandas matplotlib
```

Creating a Notebook with "Hello World" and Plotting CSV data into a graph:


6. Run the following command to start the Jupyter Notebook server:


```
jupyter notebook
```

This should open a new tab or window in your browser with Jupyter labs interface.


7. In the Jupyter interface, click "New" > "Python 3" to create a new notebook.


8. In the new notebook, create a code cell by clicking on the "+" icon or using the keyboard shortcut.


9. In the code cell, type the following:


```python
print("Hello World!")
```

10. Run the code cell by clicking the "Run" button or pressing Shift+Enter.


You should see the output "Hello World!" printed below the code cell.


11. Create a new code cell and follow these steps to plot a graph using CSV data:


    a. Import the required libraries:


    ```python
    import pandas as pd
    import matplotlib.pyplot as plt
    ```

    b. Read the CSV data into a pandas DataFrame:


    ```python
    df = pd.read_csv('data.csv')
    ```

    Replace `'data.csv'` with the name of your CSV file.


    c. Create a new figure with a specific size:


    ```python
    plt.figure(figsize=(8, 6))
    ```

    d. Create a scatter plot using the data from the DataFrame:


    ```python
    plt.scatter(df['x'], df['y'])
    ```

    Replace `'x'` and `'y'` with the column names in your CSV file that contain the x and y data, respectively.


    e. Add labels to the x and y axes:


    ```python
    plt.xlabel('X Label')
    plt.ylabel('Y Label')
    ```

    f. Add a title to the plot:


    ```python
    plt.title('My Scatter Plot')
    ```

    g. Display the plot:


    ```python
    plt.show()
    ```

12. Run the code cell with the plotting code, and you’ll see the scatter plot rendered in the output area of your notebook.


Enjoy! 


Note: Make sure your CSV data is in the same directory as your notebook or use the correct file path when reading the data using `pd.read_csv()`.

20 views0 comments

Comments


bottom of page