Show
Learn the methods in this post, and draw the perfect line plots.y Becca Tapert on UnsplashData 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:
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 MatplotlibFirst, let’s import Matplotlib, Numpy, Seaborn, and Pandas libraries.
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 objectFirst, 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 colorsYou 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 stylesYou 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 axesYou 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 plotTo 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. PracticeTo 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 plotThe 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 MatplotlibSo 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. ConclusionData 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.
|