How do i make a line plot in python?

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

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.

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.

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

Creating a graphics object

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

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

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

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

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

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.

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

You can draw the plot with the grayness value.

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

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.

Let’s draw a dashed line plot.

Let’s use a dashdot style.

Let’s draw the plot with dots.

You can also use symbols instead of these string values.

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

Let’s draw the plot with dashed blue color.

Let me draw green dashed lines.

Let’s draw a plot with black dots.

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.

You can reverse one of the axes.

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

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

Adding a label to a plot

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

Let’s name the axes.

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.

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.

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

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

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

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.

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

Let’s draw the plot.

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.

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

You can also use the fivethirtyeight style.

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

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.