What is line graph python?

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

What is line graph 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.

What is line graph 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.

What is line graph python?

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

What is line graph python?

Creating a graphics object

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

What is line graph python?

What is line graph python?

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

What is line graph python?

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

What is line graph python?

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

What is line graph python?

What is line graph python?

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

What is line graph python?

What is line graph 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.

What is line graph python?

What is line graph python?

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

What is line graph python?

What is line graph python?

You can draw the plot with the grayness value.

What is line graph python?

What is line graph python?

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

What is line graph python?

What is line graph 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.

What is line graph python?

What is line graph python?

Let’s draw a dashed line plot.

What is line graph python?

What is line graph python?

Let’s use a dashdot style.

What is line graph python?

What is line graph python?

Let’s draw the plot with dots.

What is line graph python?

What is line graph python?

You can also use symbols instead of these string values.

What is line graph python?

What is line graph python?

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

What is line graph python?

What is line graph python?

Let’s draw the plot with dashed blue color.

What is line graph python?

What is line graph python?

Let me draw green dashed lines.

What is line graph python?

What is line graph python?

Let’s draw a plot with black dots.

What is line graph python?

What is line graph 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.

What is line graph python?

What is line graph python?

You can reverse one of the axes.

What is line graph python?

What is line graph python?

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

What is line graph python?

What is line graph python?

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

What is line graph python?

What is line graph python?

Adding a label to a plot

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

What is line graph python?

What is line graph python?

Let’s name the axes.

What is line graph python?

What is line graph 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.

What is line graph python?

What is line graph 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.

What is line graph python?

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

What is line graph python?

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

What is line graph python?

What is line graph python?

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

What is line graph python?

What is line graph 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.

What is line graph python?

What is line graph python?

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

What is line graph python?

Let’s draw the plot.

What is line graph python?

What is line graph 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.

What is line graph python?

What is line graph python?

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

What is line graph python?

What is line graph python?

You can also use the fivethirtyeight style.

What is line graph python?

What is line graph python?

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

What is line graph python?

What is line graph 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 👇

What is the line graph?

A line graph is a type of chart used to show information that changes over time. We plot line graphs using several points connected by straight lines. We also call it a line chart. The line graph comprises of two axes known as 'x' axis and 'y' axis. The horizontal axis is known as the x-axis.

What is a line graph use for?

Generally, line graphs or line charts are used to track variations over time, which may be long-term or short-term. We can also use line graphs to compare changes over the same period for more than one group.

What is line graph in Matplotlib?

Line charts work out of the box with matplotlib. You can have multiple lines in a line chart, change color, change type of line and much more. Matplotlib is a Python module for plotting. Line charts are one of the many chart types it can create.