Monday, 21 August 2023

How to Change the Line Color in a Pyplot Line Graph?

By setting the ‘color’ argument, we can specify the color of the lines. Following table summarizes the different formats that we can specify a value to the color argument.

 

Format

Description

Color names

Color names like 'red', 'green', 'blue', 'black', 'white', 'purple', 'orange' can be specified directly. Reference: https://matplotlib.org/2.0.2/api/colors_api.html

Color abbreviations

Single character abbreviations are supported. For example, 'r' for red, 'g' for green, 'b' for blue, 'k' for black, 'w' for white, etc.

Hexadecimal notation

Color can be specified using a six-digit hexadecimal representation of colors. For example, '#FF0000' represents red, '#00FF00' represents green, and '#0000FF' represents blue.

RGB notation

Color can be specified using RGB tuple. For example, (1, 0, 0) represents red, (0, 1, 0) represents green, and (0, 0, 1) represents blue.

RGBA notation

Similar to RGB, but it takes an addition value to represent opacity rangin from 0 to 1.

 

Examples

# Red color line
plt.plot(x1, y1, color='red', label='red-line')

# Green color line
plt.plot(x2, y2, color='g', label='green-line')

# Blue color line
plt.plot(x3, y3, color='#0000FF', label='blue-line')

# Aqua color line
plt.plot(x4, y4, color=(0, 1, 1), label='aqua-line')

# Red color line with opacity 0.5
plt.plot(x5, y5, color=(1, 0, 0, 0.5), label='red-line-opacity')

 Find the below working application.

line_color.py

import matplotlib.pyplot as plt
import numpy as np

# Sample data
x1 = [5, 10, 15, 20, 25]
y1 = [20, 40, 60, 80, 100]

x2 = [10, 20, 30, 40, 50]
y2 = [30, 60, 90, 120, 150]

x3 = [15, 30, 45, 60, 75]
y3 = [8, 16, 24, 32, 40]

x4 = [12, 24, 36, 48, 60]
y4 = [15, 30, 45, 60, 75]

x5 = [8, 16, 24, 32, 40]
y5 = [25, 50, 75, 100, 125]

# Red color line
plt.plot(x1, y1, color='red', label='red-line')

# Green color line
plt.plot(x2, y2, color='g', label='green-line')

# Blue color line
plt.plot(x3, y3, color='#0000FF', label='blue-line')

# Aqua color line
plt.plot(x4, y4, color=(0, 1, 1), label='aqua-line')

# Red color line with opacity 0.5
plt.plot(x5, y5, color=(1, 0, 0, 0.5), label='red-line-opacity')

# Ti display the legend on the plot
plt.legend()

plt.show()

 

Output

 


 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment