Changing line color using set_color
This time the plot function is called to plot the line without any color argument. The plot function returns at first a Line2D instance. This plotted line is saved in a variable named line. Later the line.set_color method is called on this variable to change the line color to yellow (passed as an argument to the function). The plot is displayed below.
Python3
import matplotlib.pyplot as plt # Adding X and Y axis label plt.ylabel( 'Year' ) plt.xlabel( 'Population' ) # Adding title to the plot plt.title( "Shwarma Population" ) # Plotting x and y axes and line color to orange line, = plt.plot([ 100 , 200 , 300 , 400 , 500 , 600 ], [ 1950 , 1960 , 1970 , 1980 , 1990 , 2000 ]) # The previously defined line had the default color of blue # Now that color is changed to pink line.set_color( "yellow" ) plt.show() |
Output:
How to Change Line Color in Matplotlib?
Matlab’s plotting functions are included in Python by the Inclusion of the library Matplotlib. The library allows the plotting of the data of various dimensions without ambiguity in a plot. The library is widely used in Data Science and Data visualization. In this article, we will discuss how to change line color in Matplotlib. Since Matplotlib is an external library it can be installed on the machine by running the following command:
pip install matplotlib