banner



How To Add A Background Image To A Plot Python

How to Set Plot Background Color in Matplotlib?

Amend Article

Save Article

Like Article

Prerequisites:

  • Matplotlib
  • Numpy

From the below figure one can infer that a plot consists of X-centrality, Y-axis, plot championship and the axes. By default, the colour of the plot is white. If we take to set the background colour of the plot then that our plot looks beautiful, nosotros take to make the axes object, by using axes() aspect afterwards plotting the graph.

Approach:

  • Import module
  • Load or create data
  • Plot a regular graph
  • Create axes object
  • Set attribute set_facecolor() to the required color. This aspect accepts both name or colour code of the color

Follow the given examples to understand better.

Example: Default color plot

Python

import matplotlib.pyplot equally plt

student_marks = [ 50 , sixty , 70 , 80 , xc ]

student_grade = [ 'B' , 'B' , 'B+' , 'B+' , 'A' ]

plt.plot(student_marks, student_grade)

plt.xlabel( "student_marks" , fontweight = 'assuming' )

plt.ylabel( "student_grade" , fontweight = 'assuming' )

plt.title( "Student Marks five/southward Educatee Course" )

plt.bear witness()

Output:

Example 2 : Setting groundwork color to yellow

Python

import matplotlib.pyplot equally plt

student_marks = [ fifty , 60 , 70 , fourscore , xc ]

student_grade = [ 'B' , 'B' , 'B+' , 'B+' , 'A' ]

plt.plot(student_marks, student_grade)

plt.xlabel( "student_marks" , fontweight = 'bold' )

ax = plt.axes()

ax.set_facecolor( "yellow" )

plt.ylabel( "student_grade" , fontweight = 'bold' )

plt.championship( "Student Marks five/southward Student Grade" )

plt.show()

Output:

Example 3: Setting background colour to violet

Python

import matplotlib.pyplot as plt

import numpy as np

x = np.arange( 0 , x , . 1 )

y = np.sin(x)

plt.plot(ten, y)

plt.xlabel( "X" )

ax = plt.axes()

ax.set_facecolor( "violet" )

plt.ylabel( 'sin(10)' )

plt.show()

Output:

Setting Outer and Inner color of plot

We tin can too set the color of the outer portion of the plot. To set both the colour for plot background and for outer portion of the plot the only alter nosotros have to exercise in our lawmaking is that we have to add plt.effigy(faceccolor='color') before plotting the graph.

Instance one:

Python

import matplotlib.pyplot equally plt

import numpy as np

x = np.arange( 0 , x , . 1 )

y = np.sin(x)

plt.figure(facecolor = 'yellow' )

plt.plot(x, y)

plt.xlabel( "Ten" )

ax = plt.axes()

ax.set_facecolor( "violet" )

plt.ylabel( 'sin(x)' )

plt.prove()

Output:

Instance two: Setting plot color using html color codes

Python

import matplotlib.pyplot as plt

import numpy as np

x = np.arange( 0 , x , . 1 )

y = np.sin(x)

plt.effigy(facecolor = '#94F008' )

plt.plot(x, y)

plt.xlabel( "Ten" )

ax = plt.axes()

ax.set_facecolor( "#1CC4AF" )

plt.ylabel( 'sin(x)' )

plt.testify()

Output:


How To Add A Background Image To A Plot Python,

Source: https://www.geeksforgeeks.org/how-to-set-plot-background-color-in-matplotlib/

Posted by: wallaceuple1986.blogspot.com

0 Response to "How To Add A Background Image To A Plot Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel