matplotlib.rcParams[‘lines.color’] allows us to change the color of the line in matplotlib. However, it has no affect on plot() function. In this tutorial, we will introduce how to fix this problem.
To understand how to use matplotlib.rcParams, you can read this tutorial:
Understand Matplotlib.rcParams: A Beginner Guide – Matplotlib Tutorial
Look at code below:
import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl # create data x = np.linspace(0, 4*np.pi) y = np.sin(x) mpl.rcParams['lines.linewidth'] = 3 mpl.rcParams['lines.color'] = 'ff0000' mpl.rcParams['lines.linestyle'] = '-.' # draw sine function plt.title('sin function') plt.plot(x, y, label='$sin(x)$') plt.show()
We will draw a sin(x) function using matplotlib. We plan to draw a red line using mpl.rcParams[‘lines.color’] = ‘ff0000’.
However, run this code, you will get the image below:
We can find mpl.rcParams[‘lines.color’] = ‘ff0000’ does not work.
How to set the color of line?
We can use mpl.rcParams[‘axes.prop_cycle’] to implement.
As example above, in order to set the color of line to red, we can do as this:
from cycler import cycler mpl.rcParams['axes.prop_cycle'] = cycler(color =['#ff0000']) mpl.rcParams['lines.linestyle'] = '-.' # draw sine function plt.title('sin function') plt.plot(x, y, label='$sin(x)$') plt.show()
Run this code, you will get a red image.