IKH

Line Graph and Histogram

In this segment, you will learn about two new visualisation charts, which are as follows:

  • Line graph
  • Histogram

A line graph is used to present continuous time-dependent data. It accurately depicts the trend of a variable over a specified time period. Let’s watch the next video to learn how to plot a line chart using the Matplotlib library.

You can use the following command to plot a line graph:plt.

.plot(x_axis, y_axis).

Remember to be careful while using the plt.plot function. This function also helps you create a scatter plot when you tweak the syntax and specify the markers. Try to run the following code to understand the difference between the outputs of the function.

(if you are getting an error, check the quotation marks.).

If you specify the colour and marker separately, then you will get a line plot with the points marked. Try to run the following the code for this.

A line graph can be helpful when you want to identify the trend of a particular variable. Some key industries and services that rely on line graphs include financial markets and weather forecast. Although you successfully created a line chart in the previous video, you can make certain visual improvements to create a chart that is more easily understandable. Let’s watch the next video to learn about these visual modifications. 

In the video above, you learnt how to rotate the tick labels on the axes using the following command.

After running the command, the chart will look like the one given below.

As you can see in the diagram, the x-ticks and y-ticks are much more readable. 

To further improve the readability of the chart, you can add markers to the data points. Let’s watch the next video to learn how to make modifications to add data labels.

In the video above, you learnt how to use the annotate method to add data labels to the plot. The code given below was used in the previous video.

After running this code, your plot will look like the one given below.

In the earlier segment on scatter plot, you used the annotate method to add data labels to a scatter plot. Similarly, the annotate method can be used to add data labels to graphs as well. Now that you know the basics of line graphs, attempt the following questions.

Histograms

histogram is a frequency chart that records the number of occurrences of an entry or an element in a data set. It can be useful when you want to understand the distribution of a given series. Let’s watch the next video to learn how to plot a histogram.

As shown in the video above, you can use the following command to plot a histogram.

After running this code, your histogram will look like the one given below. 

The x-ticks in the histogram above are not very informative. Let’s try to add more detailed x-ticks so that the data is more readable. In the next video, you will learn how to add more information to classes and x-ticks.  

In the video above, you learnt how to use the hist() function to add more information to classes and x-ticks. Now that we have covered the basics of histograms, attempt the following questions.  

In the next segment, you will learn about another plot, namely, Box Plot. It is used to visualise the spread of a numerical variable. 

Additional Resources

You can also use line graphs to plot and visualize time series data. You can learn more about plotting time series data here

Report an error