DEV Community

TechPlygrnd
TechPlygrnd

Posted on

Matplotlib Tutorial #7: Plot Color Customization

Hi everyone, Today I will show you how to color your plot.

Marker Edge Color

You can use the keyword argument markeredgecolor or the shorter mec to set the color of the edge of the markers

import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([0, 1, 2, 3, 5, 6])
ypoints = np.array([0, 1, 2, 3, 5, 6])

plt.plot(xpoints, ypoints, marker='o', markeredgecolor='r')
plt.show()
Enter fullscreen mode Exit fullscreen mode

That will generate the following output

Plot 1

For the value of color, you can use basic color code such as:

  • b: blue
  • g: green
  • r: red
  • c: cyan
  • m: magenta
  • y: yellow
  • k: black
  • w: white

Other than basic value, you can use hex code and color codes from the color names supported by all browsers https://www.w3schools.com/colors/colors_names.asp

Marker Face Color

You can use the keyword argument markerfacecolor or the shorter mfc to set the color inside the edge of the markers

import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([0, 1, 2, 3, 5, 6])
ypoints = np.array([0, 1, 2, 3, 5, 6])

plt.plot(xpoints, ypoints, marker='o', markerfacecolor='r')
plt.show()
Enter fullscreen mode Exit fullscreen mode

Which will produces the following output

Plot 2

For color value you can refer to the available color value for the Marker Edge Color above.

Line Color

You can use the keyword argument color or the shorter c to set the color of the line

import matplotlib.pyplot as plt
import numpy as np

ypoints = np.arange(10)

plt.plot(ypoints, color='r')
plt.show()
Enter fullscreen mode Exit fullscreen mode

Which will produce the following output

Plot 3


That is how you can customize the color of your plot. Thank you for reading and have a nice day!

Top comments (0)