How to Change Color of a Graph Plot in Matplotlib With Python?


Matplotlib is a popular data visualization library used in python. It has wide range of tools and techniques for creating different types of plots. When we work in data visualization, color plays an important role in conveying information and making the work more appealing and easy to understand. We will start with the basics of Matplotlib, pyplot, creating graphs and then different methods to change the color of a graph plot with various examples.

What is Matplotlib?

Matplotlib is a simple interface for making different plots, where you have a variety of options to make changes to the plots. It is built on top of Numpy, python library for scientific computing. It has module name “pyplot” which makes things easy for plotting by providing features to control line-styles, font properties, formatting axes, etc.

How to Install Matplotlib?

We can install matplotlib by using a command “pip install matplotlib” in the command prompt.

What is pyplot?

Pyplot is an Application Programming Interference (API) in python matplotlib that provides a collection functions for creating and customizing different types of plots. It is a submodule of the Matplotlib library for python. This provides a “MATLAB” like interface used to create a figure, create a plot line area, decorates the plot with labels, etc. It used to create many varieties of plot like line plot, histograms, scatter, bars, 3D plots, images, contours, and polar. Matplotlib is an open–source drawing library that supports various drawing types.

How to Create Graph ?

Matplotlib is used to draw a graph in python by using different methods.

  • To create a line graph we need to use the ‘plt.plot()’ method.

  • To create a bar graph we need to use the ‘plt.bar()’ method.

  • To create a scatter graph we need to use the ‘plt.scatter()’ method.

Example

Here, we have plotted a line graph using plot function in matplotlib. First we have created two list “names” and “CGPA” and then created a line graph using plt.plot(). The -axis plots the names of the students and y–axis holds the CGPA of the students.

import matplotlib.pyplot as plt
name=["kunnal","lokie","anika","linda"]
cgpa=[9.8,7.6,5.8,9.0]
plt.plot(name, cgpa)
plt.title("student progress report")
plt.xlabel("names")
plt.ylabel("CGPPA")
plt.show()

Output

There are three ways to change color of graph plot let us see them one by one –

Using the plot() method

As discussed the plot() method is used to create a line graph and this accepts two lists representing the values along the x-axis and y-axis respectively.

By default, the color of the line plot is blue and we can change the color of a line plot by passing value to the color parameter to the plot() method.

Example

In the following example we have created a line graph using plt.plot() method. The x-axis holds the names of the fruits and the y–axis holds the sales values. Here we have changed the color of lines from the default value (blue) to red.

import matplotlib.pyplot as plt
fruits =["apple","mango","cherry","pear"]
sales =[45000,78900,65439,49000]
plt.plot(fruits,sales,color='red')
plt.title("SALES RECORD")
plt.xlabel("FRUITS")
plt.ylabel("SALES")
plt.show()

Output

Using the pie() method

Similarly, we can change the color of a pie chart using the color parameter of the pie() method. We need to pass a list of color values to the pie() method and the number of values should be equal to the number of sections in the pie chart, one color for each section.

Example

import matplotlib.pyplot as plt
sizes = [30, 40, 20, 10]
colors = ['pink', 'blue', 'green', 'yellow']
plt.pie( sizes, colors=colors)
plt.title('Input 3')
plt.show()

Output

Single characters instead of color names

We can also use single characters instead of complete color names along with the plot() method, and change the color of the graphs. Following are the characters representing different colors –

  • b: blue

  • g: green

  • r: red

  • c: cyan

  • m: magenta

  • y: yellow

  • k: black

  • w: white

Example

In the following example we have created a line plot using the plot() function and we have changed the color of the plot using single characters instead of complete color names.

import matplotlib.pyplot as plt
fruits=["apple","mango","cherry","pear"]
sales=[90000,67900,65439,89000]
plt.plot(fruits,sales, color='y')
plt.title("SALES RECORD")
plt.xlabel("FRUITS")
plt.ylabel("SALES")
plt.show() 

Output

Using the HEX-STRING (#) Function

Typically, to represents the colors in a webpage Hexadecimal values are used. Using these the colors are defined by its mix of Red, Green and Blue, each value will be in the rage of 00 to FF (in hexadecimal). A hex string is the hexa-decimal representation of the color and we can change the color of plot by passing hexa-decimal values representing the desired color to the plot() function.

Example

In the following program we are changing the color of the plot to black.

import matplotlib.pyplot as plt
player=["rahul","kunal","knuj","pearl"]
goals=[4,7,6,1]
plt.plot(player, goals,'#003000')
plt.title("FOOTBALL MATCH")
plt.xlabel("PLAYERS")
plt.ylabel("GOALS")
plt.show()

Output

Conclusion

In this article, we have briefly discussed about the different methods used to change the color of graph plot in matplotlib by using different methods. There are three method to change color of graph plot are described clearly and briefly

Updated on: 11-Oct-2023

264 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements