How to use gradient color in matplotlib in Python
Hello friends, you can plot various graphs on Python using matplotlib, you can also give them different colors and different linestyles. In this tutorial, I will tell you how to use gradient color in matplotlib in Python.
How to use gradient color in matplotlib in Python
Today I will cover the following :
- How to use gradient color in scatter graphs,
- How to use gradient color in bar plots.
- How to use gradient color in a line plot.
Before starting with any specific topic, I would recommend you add these dependencies to your code :
import matplotlib.pyplot as plt import matplotlib.cm as cm import numpy as np
You already know about matplotlib.pyplot and numpy. If you don’t, please check k our posts on aggregate and statistical functions in Numpy and Python Matplotlib Library. If you see I have also included one more library mathplotlib.cm. This provides me access to built-in colormaps which is of utmost importance while adding gradients in matplotlib.
How to use gradient color in scatter graphs
In this example, I have taken two lists, which will give me pair values of x and y. Now I have also taken a temporary variable, value. This variable stores a 2-D array of values effected by the cos()
function which takes the x_axis values as input. This value
variable will provide me with different colors in either RGB or RGBA while plotting the pair values. While plotting I used the scatter()
function and passed four attributes as arguments. The first two are the two lists x_axis
and y_axis
holding the x values and y values respectively. c
attribute is assigned the variable, value. cmap attribute is assigned the name of the colormap you desire to use.
Code :
num = 500 plt.figure(figsize = (10, 5)) x_axis = np.linspace(0, 100, num) y_axis = np.random.rand(num) value = np.cos(x_axis) plt.scatter(x_axis, y_axis, c = value, cmap = "Blues")
Output :
How to use gradient color in bar graphs
For this example, I have used the same values for x, y, and num. Here I have used the bar()
function to plot a bar plot. I have passed the two lists x_axis and y_axis for values of x and y respectively. To add color I have introduced the color attribute which takes the ‘Greens’ colormap as the color and the quotient returned by dividing x_axis
value by the num
variable, I get the different colors in RGB or RGBA. I have also defined the edgecolor
attribute as grey to differentiate between different bars in the plot.
Code :
plt.bar(x_axis, y_axis, width = 2, color=cm.Greens(x_axis/num), edgecolor = 'grey')
Output :
How to use gradient color in line graphs
Here I have redefined the various variables’ values like x_axis
, y_axis
, and value
. To plot a line plot, I used the scatter()
function and passed the exact same attributes as the one while plotting a scatter plot. The only difference here is that I have added one more attribute called, marker
and gave it a string value of 'o'
.
Code :
x_axis = np.linspace(-1, 1, 1000) y_axis = np.sin(x_axis) value = np.tan(x_axis) plt.scatter(x_axis, y_axis, c = value, cmap = "Blues", marker = 'o')
Output :
Thus you know how to use gradient color in matplotlib in Python.
Leave a Reply