Home About Contact

 

Create charts and graphs using Python Matplotlib

3 min read

Python has an awesome library known as Matplotlib, which you can use to create all sort of graphs and charts by simply a few lines of code.

The matplotlib does not come by default with your Python installation. If you don’t have it, you’ll need to install it by the following command

python -m pip install matplotlib

Here are a few quick example of charts that it can create for you.

1. Line chart

import matplotlib.pyplot as plt
plt.plot([1, 3, 6], [5, 8, 6.5])
plt.show()

  • Line 1 – import the pyplot of matprotlib, which is what you need to generate charts and graphs
  • Line 2 – to create a simple line chart, we use the plot() method, which takes an array of X points (1,3,6) and an array of Y points (5,8,6.5)
  • Line 3 – Finally, call the show() method to display the chart, this will open a window and display the chart as shown in the image above
  • 2. Bar chart

    import matplotlib.pyplot as plt
    plt.bar(['Apple','Orange','Grape'], [3, 8, 10], color = '#FF9922')
    plt.show()
    

  • Line 1 – as usual import the pyplot of matprotlib
  • Line 2 – to create a bar chart, just use the bar() method, which you just need to supply at least two parameters, they are an array of values for the X-axis and an array of values for the Y-axis. And you can change the color of the bars by the parameter color, which can be in HTML HEX color code.
  • Line 3 – Finally, call the show() method to display the bar chart, as shown above
  • 3. Pie Chart

    import matplotlib.pyplot as plt
    plt.pie([3, 8, 10],  labels =  ['Apple','Orange','Grape'], colors = ["#334455", "#FFAA34", "#508932"])
    plt.show()
    

  • Line 1 – as usual import the pyplot
  • Line 2 – to create a pie chart, simply call the pie() method, which takes the first parameter as an array of values and you can add other parameters such as labels, which is an array of strings, colors – an array of strings of color values for each portion in the pie chart. Color values can be in HTML HEX color code.
  • Line 3 – Finally, call the show() method to display the bar chart, as shown above
  • 4. Histogram – the kind of graph that shows frequency distributions

    import matplotlib.pyplot as plt
    plt.hist([76, 77, 75, 76, 75, 74, 76, 76, 78, 74, 76, 76, 74, 76, 74,
      77, 76, 76, 74, 75, 77, 75, 75, 76, 75, 77, 76, 74, 75, 75,
      74, 76, 77, 75, 73, 75, 77, 75, 75, 76, 74, 76, 76, 75, 76,
      75, 75, 75, 76, 74, 88,89, 76, 76, 76, 78,77,76,76, 76])
    plt.show()
    

    Line 2 – to create a histogram, simply call the method hist() and supply it with a parameter of an array of numbers. E.g, the above shows the distribution of scores of math test in a class of 60 students.

    More info about Matplotlib can be found on here

    Spread the love
    Posted on December 3, 2020 By Christopher Chee

    Please leave us your comments below, if you find any errors or mistakes with this post. Or you have better idea to suggest for better result etc.


    Our FB Twitter Our IG Copyright © 2024