banner



how to draw a line in python

In this brusk guide, yous'll meet how to plot a Line nautical chart in Python using Matplotlib.

To start, hither is a template that you may apply to plot your Line nautical chart:

import matplotlib.pyplot as plt  plt.plot(xAxis,yAxis) plt.title('title proper noun') plt.xlabel('xAxis name') plt.ylabel('yAxis proper name') plt.show()        

Next, you'll run into how to apply the above template using a applied case.

Steps to Plot a Line Chart in Python using Matplotlib

Footstep 1: Install the Matplotlib package

If y'all haven't already done so, install the Matplotlib package in Python using this command (under Windows):

pip install matplotlib        

Y'all may cheque the following guide for the instructions to install a packet in Python using PIP.

Step 2: Gather the information for the Line chart

Next, gather the data for your Line chart.

For case, I gathered the following data about two variables for a given economy:

  • Year
  • Unemployment_Rate

Here is how the data looks like:

Twelvemonth Unemployment_Rate
1920 9.8
1930 12
1940 8
1950 seven.2
1960 6.9
1970 7
1980 vi.5
1990 vi.ii
2000 5.5
2010 half dozen.three

The ultimate goal is to depict the higher up data using a Line chart.

Step 3: Capture the data in Python

You can capture the to a higher place data in Python using the following two Lists:

Twelvemonth = [1920,1930,1940,1950,1960,1970,1980,1990,2000,2010] Unemployment_Rate = [nine.8,12,8,7.two,6.9,7,6.5,half-dozen.2,5.5,6.iii]        

Pace 4: Plot a Line chart in Python using Matplotlib

For the final footstep, yous may employ the template below in social club to plot the Line chart in Python:

import matplotlib.pyplot as plt  plt.plot(xAxis,yAxis) plt.title('title proper noun') plt.xlabel('xAxis name') plt.ylabel('yAxis name') plt.prove()        

Hither is how the code would look similar for our example:

import matplotlib.pyplot as plt     Twelvemonth = [1920,1930,1940,1950,1960,1970,1980,1990,2000,2010] Unemployment_Rate = [ix.8,12,8,7.two,six.9,7,6.5,half-dozen.2,v.5,6.3]    plt.plot(Year, Unemployment_Rate) plt.title('Unemployment Charge per unit Vs Year') plt.xlabel('Year') plt.ylabel('Unemployment Rate') plt.testify()        

Run the code in Python and you lot'll get the following Line chart:

How to Plot a Line Chart in Python using Matplotlib

Y'all could further style the Line nautical chart using this code:

import matplotlib.pyplot as plt     Year = [1920,1930,1940,1950,1960,1970,1980,1990,2000,2010] Unemployment_Rate = [9.8,12,8,7.2,6.9,seven,vi.v,6.2,5.5,6.three]    plt.plot(Year, Unemployment_Rate, color='cerise', mark='o') plt.championship('Unemployment Rate Vs Year', fontsize=xiv) plt.xlabel('Yr', fontsize=14) plt.ylabel('Unemployment Rate', fontsize=fourteen) plt.grid(True) plt.prove()        

You'll then get this styled Line nautical chart:

Plot a Line Chart in Python using Matplotlib

How to Create a Line Chart in Python with Pandas DataFrame

So far, you lot have seen how to create your Line chart using lists.

Alternatively, you could capture the dataset in Python using Pandas DataFrame, and and then plot your nautical chart.

In that instance, the complete code would look as follows:

import pandas equally pd import matplotlib.pyplot as plt     Data = {'Year': [1920,1930,1940,1950,1960,1970,1980,1990,2000,2010],         'Unemployment_Rate': [ix.8,12,eight,seven.two,six.9,7,half-dozen.5,6.2,5.5,6.3]        }    df = pd.DataFrame(Data,columns=['Year','Unemployment_Rate'])    plt.plot(df['Year'], df['Unemployment_Rate'], color='red', marking='o') plt.championship('Unemployment Rate Vs Yr', fontsize=fourteen) plt.xlabel('Year', fontsize=14) plt.ylabel('Unemployment Rate', fontsize=14) plt.grid(Truthful) plt.prove()        

You lot'll then become the verbal same Line nautical chart with Pandas DataFrame:

Line Chart in Python

Source: https://datatofish.com/line-chart-python-matplotlib/

Posted by: boothereastill.blogspot.com

0 Response to "how to draw a line in python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel