How do i make a line plot in python?

Learn the methods in this post, and draw the perfect line plots.

How do i make a line plot in python?

y Becca Tapert on Unsplash

Data visualization is one of the important steps of data analysis. By visualizing the data, you can explore the information in the data. One of the simplest plots is the line plot.

In this post, I’ll cover the following topics:

  • What are the line plots?
  • How to draw a simple line plot?
  • How to specify the color and style of the lines?
  • How to fill the area under the line plot?
  • Practicing with a real-world dataset.

Let’s dive

What is line plot?

A line plot is often used to display a trend in data. It is a basic type of plot common in many fields. Let’s say you have daily stock market closing data of a company. If you want to see the performance of this company in the daily stock market for a year, you can use the line plot.

Let’s draw a simple line plot.

Drawing a simple line plot with Matplotlib

First, let’s import Matplotlib, Numpy, Seaborn, and Pandas libraries.

How do i make a line plot in python?

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. Matplotlib makes easy things easy and hard things possible.

You can find this notebook here. Let’s use the % matplotlib inline magic command to see the plots between the lines.

How do i make a line plot in python?

You can adjust the styles of the plots with the set method in Seaborn.

How do i make a line plot in python?

Creating a graphics object

First, let’s create a graphic object and graphic area.

How do i make a line plot in python?

How do i make a line plot in python?

Now let’s generate 100 values for the x-axis as follows:

How do i make a line plot in python?

Let me create values for the y-axis by calculating the sines of these values.

How do i make a line plot in python?

Now let’s draw a line plot with these variables.

How do i make a line plot in python?

How do i make a line plot in python?

To see more than one line plot in a chart, you can use the commands multiple times.

How do i make a line plot in python?

How do i make a line plot in python?

Specifying line colors

You can also specify the line colors and styles. To determine the color of the line, you can use the color parameter. Let’s do this.

How do i make a line plot in python?

How do i make a line plot in python?

Let’s take a look at another example with shortening the color names.

How do i make a line plot in python?

How do i make a line plot in python?

You can draw the plot with the grayness value.

How do i make a line plot in python?

How do i make a line plot in python?

You can also use hex code to specify the color of the line.

How do i make a line plot in python?

How do i make a line plot in python?

If you don’t determine a color, Matplotlib automatically assigns the colors.

Specifying the line styles

You can use the linestyle parameter for the line style. Let’s draw the plot with a straight line.

How do i make a line plot in python?

How do i make a line plot in python?

Let’s draw a dashed line plot.

How do i make a line plot in python?

How do i make a line plot in python?

Let’s use a dashdot style.

How do i make a line plot in python?

How do i make a line plot in python?

Let’s draw the plot with dots.

How do i make a line plot in python?

How do i make a line plot in python?

You can also use symbols instead of these string values.

How do i make a line plot in python?

How do i make a line plot in python?

You can also set both color and line styles. For example, let’s draw a red straight line.

How do i make a line plot in python?

How do i make a line plot in python?

Let’s draw the plot with dashed blue color.

How do i make a line plot in python?

How do i make a line plot in python?

Let me draw green dashed lines.

How do i make a line plot in python?

How do i make a line plot in python?

Let’s draw a plot with black dots.

How do i make a line plot in python?

How do i make a line plot in python?

Specifying the intervals of the axes

You can specify the intervals of the axes with the xlim and ylim methods. Let me show you this.

How do i make a line plot in python?

How do i make a line plot in python?

You can reverse one of the axes.

How do i make a line plot in python?

How do i make a line plot in python?

You can specify boundaries for the x and y axes with the axis method. Let me show you this.

How do i make a line plot in python?

How do i make a line plot in python?

The axis method has parameters that allow automatic adjustments. Let’s use the tight option.

How do i make a line plot in python?

How do i make a line plot in python?

Adding a label to a plot

To give a title to the plot, you can use the title() method.

How do i make a line plot in python?

How do i make a line plot in python?

Let’s name the axes.

How do i make a line plot in python?

How do i make a line plot in python?

If there is more than one line plot in a chart, you can use the legend method to show the names of the lines. Let me show you this.

How do i make a line plot in python?

How do i make a line plot in python?

Practice

To show the line plots, let’s perform an application. The data I’m going to use shows the age and median salaries of developers in America. First, let’s create age and salary variables.

How do i make a line plot in python?

Let’s create a variable that shows the median salary of Python developers.

How do i make a line plot in python?

Now let’s draw the line plot that shows the age of all developers and Python developers.

How do i make a line plot in python?

How do i make a line plot in python?

As you can see, Python developers’ salaries are higher than all developers. Let’s add a legend.

How do i make a line plot in python?

How do i make a line plot in python?

Filling the area under the line plot

The fill_between() method is used to fill the area under the line plot. Let me show you this using the age and py_salary variables.

How do i make a line plot in python?

How do i make a line plot in python?

You can specify a value from the y-axis for the area. Let’s use the median value of all salaries.

How do i make a line plot in python?

Let’s draw the plot.

How do i make a line plot in python?

How do i make a line plot in python?

Graphic styles in Matplotlib

So far we have used the Seaborn style as the graphic style. You can change this style. Let’s see the styles that can be used in Matplotlib.

How do i make a line plot in python?

How do i make a line plot in python?

For example, let’s see the ggplot style used for R programming.

How do i make a line plot in python?

How do i make a line plot in python?

You can also use the fivethirtyeight style.

How do i make a line plot in python?

How do i make a line plot in python?

You can also use the xkcd method as a graphic style.

How do i make a line plot in python?

How do i make a line plot in python?

Conclusion

Data visualization is one of the most important states of data analysis. In this post, I talked about how to draw the line plots. That’s it. I hope you enjoy it. Thank you for reading. You can find this notebook here.

Don’t forget to follow us on YouTube | GitHub | Twitter | Kaggle | LinkedIn

If this post was helpful, please click the clap 👏 button below a few times to show me your support 👇

Which method is used to generate line chart in Python?

You can use the plot(x,y) method to create a line chart.

How do I draw a line in Matplotlib?

You can also plot many lines by adding the points for the x- and y-axis for each line in the same plt. plot() function. (In the examples above we only specified the points on the y-axis, meaning that the points on the x-axis got the the default values (0, 1, 2, 3).)

How do you plot a line between two points in Python?

Use matplotlib..
point1 = [1, 2].
point2 = [3, 4].
x_values = [point1[0], point2[0]] gather x-values..
y_values = [point1[1], point2[1]] gather y-values..
plt. plot(x_values, y_values).

How do you construct a line plot?

To make a line plot, organize your gathered data in numerical order from smallest to largest, or vice versa. Then, draw a number line that includes all of the numbers in your data, moving from left to right. Mark an "X" above the number for each time that specific number occurs in your data set.