How to Change Line Color in Matplotlib?
Last Updated :
23 Jan, 2023
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
Changing line color using plot
The default color used for a line in Matplotlib is blue. The plot function used to plot data takes in an optional argument for color, i.e., If the argument is provided, the color associated with the argument would be used to plot the line. The color value could be provided to the function in various ways, such as:
Example 1: Using initials of color names (“g” for red)
Firstly the x-axis and y-axis labels are defined, along with the title for the plot (for demonstration). Then a line is plotted on the 2d plot using the function plot, and the string orange is passed as the second argument to the function. This argument determines the color of the plot line. Therefore the plotted line in the output is of orange color rather than the default blue color.
Python3
import matplotlib.pyplot as plt
plt.ylabel( 'Year' )
plt.xlabel( 'Population' )
plt.title( "Shwarma Population" )
plt.plot([ 100 , 200 , 300 , 400 , 500 , 600 ], [
1950 , 1960 , 1970 , 1980 , 1990 , 2000 ], 'orange' )
plt.show()
|
Output:
Example 2: Using color names (“red” for red)
Then a line is plotted on the 2d plot using the function plot, and the string green is passed as the second argument to the function.
Python3
import matplotlib.pyplot as plt
plt.ylabel( 'Year' )
plt.xlabel( 'Population' )
plt.title( "Shwarma Population" )
plt.plot([ 100 , 200 , 300 , 400 , 500 , 600 ], [
1950 , 1960 , 1970 , 1980 , 1990 , 2000 ], 'g' )
plt.show()
|
Output:
Example 3: Using color codes (“FF0000” for red)
Then a line is plotted on the 2d plot using the function plot, and the string red is passed as the second argument to the function.
Python3
import matplotlib.pyplot as plt
plt.ylabel( 'Year' )
plt.xlabel( 'Population' )
plt.title( "Shwarma Population" )
plt.plot([ 100 , 200 , 300 , 400 , 500 , 600 ], [
1950 , 1960 , 1970 , 1980 , 1990 , 2000 ], 'g' )
plt.show()
|
Output:
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
plt.ylabel( 'Year' )
plt.xlabel( 'Population' )
plt.title( "Shwarma Population" )
line, = plt.plot([ 100 , 200 , 300 , 400 , 500 , 600 ], [
1950 , 1960 , 1970 , 1980 , 1990 , 2000 ])
line.set_color( "yellow" )
plt.show()
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...