Open In App

How to Change Line Color in Matplotlib?

Last Updated : 23 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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
  
# 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
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
  
# 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
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
  
# 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
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
  
# 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:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads