Matplotlib Why Is Plot Background Blue

In this Python tutorial, we will discuss theMatplotlib change background color in python. Here we learn how to change the background color of the plot, and we will also cover the following topics:

  • Matplotlib change background color
  • Matplotlib change background color of plot
  • Matplotlib change background color example
  • Matplotlib change background color default
  • Matplotlib change background color change default color
  • Matplotlib change background color inner and outer color
  • Matplotlib change background color to image
  • Matplotlib change background color transparent
  • Matplotlib change background color legend
  • Matplotlib change background color subplot
  • Matplotlib change background color based on value

Matplotlib background color

Matplotlib is the most widely used data visualization library in Python. By using this library, we can customize the background color of the plot. It provides the functionality to change the background of axes region and figure area also

The following steps are used to change the color of the background of the plot which is outlined below:

  • Defining Libraries: Import the important libraries which are required to change the background color of the plot. ( For visualization: pyplot from matplotlib, For data creation and manipulation: numpy and pandas).
  • Plot the graph: Define the axis and plot the graph.
  • Change background color: By using the set_facecolor() method you can change the background color.
  • Display: At last by using the show() method display the plot.

Read: How to install matplotlib python

Matplotlib change background color of plot

set_facecolor() method in the axes module is used to change or set the background color of the plot.

It is used to set the face color of the figure or we can say the axes color of the plot.

Pass the argument as a color name that you want to set.

The syntax to change the background color of the plot is as below:

          matplotlib.pyplot.axes.set_facecolor(color=None)        

The above-used parameter is outlined as below:

  • color: specify the name of the color you want to set.

Matplotlib change background color example

In the above sections, we discussed what a background color exactly means. And we have also discussed what are the various steps used to change the background color of the plot. Now, let's see how to change the background color.

Let's understand the concept with the help of an example as below:

                      # Import library                        import matplotlib.pyplot as plt            # Define Data            x = [5, 6, 3.5, 9.3, 6.8, 9] y = [2, 3, 6, 8, 15, 6.5]            # Plot Graph            plt.plot(x,y) ax=plt.axes()            # Set color            ax.set_facecolor('pink')            # Display Graph                        plt.show()        
  • In the above example, we import the library matplotlib.
  • After this we define the x-axis and y-axis of the plot.
  • plt.plot() method is used to plot the data
  • plt.axes() method is assigned to variable ax.
  • set_facecolor() method is used to change the background color of the plot. Here we cahnge background to "pink".
  • Finally, the show() method is used to display the plotted graph.
Matplotlib change background color example
set_facecolor()

Read: Matplotlib plot a line

Matplotlib change background color default

Let's see what was the default background color of the plot.

By default the background color of the plot is "White".

Let's do an example for understanding the concept:

                      # Import library                        import matplotlib.pyplot as plt            # Define Data            x = [5, 6, 3.5, 9.3, 6.8, 9] y = [2, 3, 6, 8, 15, 6.5]            # Plot Graph            plt.plot(x,y)            # Display Graph            plt.show()        
  • In the above, example we import thematplotlib.pyplot library.
  • Then we define theX-axis and Y-axispoints.
  • plt.plot() method is used to plot data points.
  • Then we finally use the methodplt.show() to display the plotted graph.
Matplotlib change background color default
plt.plot() "default background color"

Read: Python plot multiple lines using Matplotlib

Matplotlib change background color change default color

As we learn above the default background color of the plot is "white".

We can also update the default color by updating the configurations.

The syntax to reset the default color is given below:

                      # To reset the plot configurations            matplotlib.pyplot.plt.rcdefaults()            # To set the new deault color for all plots            matplotlib.pyplot.plt.rcParams.update()        

Let's understand the concept of changing the default background color with the help of an example:

                      # Import library                        import matplotlib.pyplot as plt            # reset the plot configurations to default            plt.rcdefaults()            # set the axes color glbally for all plots            plt.rcParams.update({'axes.facecolor':'lightgreen'})            # Define Data            x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10]            # Plot the Graph            plt.plot(x,y)            # Display the plot            plt.show()        
  • rcdefaults() method is used to reset the matplotlib configurations.
  • plt.rcParams.update() to update default axes face color. Here we set it to "lightgreen".
  • After this we define the x-axis and y-axis on the plot.
  • plt.plot() method to plot the graph.
  • plt.show() to display the graph.
Matplotlib change background color change default color
plt.rcdefaults()

Now let's see what happens if we create a new plot without setting the color.

                      # Import library            import matplotlib.pyplot as plt            # Define Data            x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10]            # Plot the Graph            plt.scatter(x,y)            # Display the plot            plt.show()        
Matplotlib change background color to change default color

From the above, we concluded that after changing the default color. All the plots will have the same color which we set. It changes the default from "white" to "light green".

Read: What is matplotlib inline

Matplotlib change background color inner and outer color

We can set the Inner and Outer colors of the plot.

set_facecolor() method is used to change the inner background color of the plot.

figure(facecolor='color') method is used to change the outer background color of the plot.

Let's understand the concept with the help of an example:

                      # Import Libraries                        import matplotlib.pyplot as plt            # Define Data            x = [2,4,5,6,8,12]  y = [3,4,9,18,13,16]            # Set Outer Color            plt.figure(facecolor='red')            # Plot the graph            plt.plot(x,y)  plt.xlabel("X", fontweight='bold') plt.ylabel("Y", fontweight='bold')  ax = plt.axes()            # Set Inner Color            ax.set_facecolor('yellow')            # Display the graph            plt.show()        
  • In the above example, we have change both an inner and outer color of background of plot.
  • "facecolor" attribute is used in figure() method to change outer area color.
  • set_facecolor() method of the axes() object to change the inner area color of the plot.
Matplotlib change background color inner and outer color
Inner and Outer area background color change

Read: Matplotlib plot bar chart

Matplotlib change background color to image

We would change the background of the plot and set an image as a background.

In Python, we have two functions imread() and imshow() to set an image as a background.

The syntax to set an image as background :

                      # Set image            plt.imread("path of image")            # Show image            ax.imshow(image)        

Let's understand the concept of setting image as a background with the help of an example:

                      # Import Library            import matplotlib.pyplot as plt            # Define Data                        x = [10, 20, 30, 40, 50, 6]  y = [3,4,5, 9, 15, 16]            # Set Image            img = plt.imread("download.jpg")            # Plot the graph            fig, ax = plt.subplots() ax.scatter(x,y, s= 250 , color="yellow")            # Show Image            ax.imshow(img, extent=[-5, 80, -5, 30])            # Display graph            plt.show()        
  • In the above example, we use the imread() method to set an image as the background color. We pass the "path of the image" as an argument.
  • imshow() function is used to specify the region of the image. We pass an argument "extent" with information as horizontal_min, horizontal_max, vertical_min, vertical_max.
Matplotlib change background color to image
plt.imread()

Read: Matplotlib subplot tutorial

Matplotlib change background color transparent

If we want to set the background color of the figure and set axes to be transparent or need to set the figure area to transparent we need the set_alpha() method.

The default value of alpha is 1.0, which means fully opaque.

We can change this value by decreasing it.

The syntax for set_alpha() function is as below:

          matplotlib.patches.Patch.set_alpha(alpha)        

The above-used parameter is outlined as below:

  • alpha: float value to set transparency.

Here patch is an artist with face color and edge color.

Let's understand the concept with the help of an example:

                      # Import Library            import matplotlib.pyplot as plt            # Define Data            x = [10, 20, 30, 40, 50, 6]  y = [3,4,5, 9, 15, 16]            # Plot Graph            fig = plt.figure()            # Set background color of Figure                        fig.patch.set_facecolor('blue')            # Set transparency of figure            fig.patch.set_alpha(1)            # Plot graph            ax = fig.add_subplot(111)            # Background color of axes                        ax.patch.set_facecolor('orange')            # Set transaprency of axes            ax.patch.set_alpha(1)            # Display Graph            plt.scatter(x,y) plt.show()        
  • In the above example, we use the set_alpha() method to set the transparency of background color.
  • Here we set the value of alpha to be 1, for both figure and axes area.
  • It means both the area are fully opaque.
Matplotlib change background color transparent
set_alpha()

Let's check what happens if we change the set_alpha of axes area to 0.

                      # Import Library            import matplotlib.pyplot as plt            # Define Data            x = [10, 20, 30, 40, 50, 6]  y = [3,4,5, 9, 15, 16]            # Plot Graph            fig = plt.figure()            # Set background color of Figure                        fig.patch.set_facecolor('blue')            # Set transparency of figure            fig.patch.set_alpha(1)            # Plot graph            ax = fig.add_subplot(111)            # Background color of axes                        ax.patch.set_facecolor('orange')            # Set transaprency of axes            ax.patch.set_alpha(1)            # Display Graph            plt.scatter(x,y) plt.show()        
Matplotlib change background color to transparent
set_alpha() "set to be 0"

Conclusion! Now see, the axes are getting 'blue' in color.

Read: Matplotlib best fit line

Matplotlib change background color legend

Matplotlib provides functionality to change the background color of the legend.

plt.legend() method is used to change the background color of the legend.

The syntax to change the color of legend is as below:

          matplotlib.pyplot.plt.legend(facecolor=None)        

The above-used parameter is outlined as below:

  • facecolor: to change the background color of the legend.

Let's understand the concept with the help of an example to change the background color of the legend:

                      # Import Libraries            import matplotlib.pyplot as plt            #Define Data            plt.plot([0, 2], [0, 3.6], label='Line-1') plt.plot([1, 4], [0, 3.7], label='Line-2')            #Background color of legend            plt.legend(facecolor="yellow")            # Display Graph            plt.show()        
  • In the above example, we use plt.legend() method to change the background color of the legend.
  • Here we pass "facecolor" as an argument and set its value to "Yellow".
Matplotlib change background color legend
plt.legend(facecolor='yellow')

Read: Matplotlib subplots_adjust

Matplotlib change background color subplot

Here we will discuss how we can change the background color of the specific subplot if we draw multiple plots in a figure area.

We use the set_facecolor() method to change the background color of a specific subplot.

Let's understand the concept with the help of an example:

                      # Importing Libraries            import numpy as np import matplotlib.pyplot as plt            # Define Data                        x1= [0.2, 0.4, 0.6, 0.8, 1] y1= [0.3, 0.6, 0.8, 0.9, 1.5]  x2= [2, 6, 7, 9, 10] y2= [3, 4, 6, 9, 12]  x3= [5, 8, 12] y3= [3, 6, 9]  x4= [7, 8, 15] y4= [6, 12, 18]  fig, ax = plt.subplots(2, 2)            # Set background color of specfic plot            ax[0, 0].set_facecolor('cyan')            # Plot graph            ax[0, 0].plot(x1, y1) ax[0, 1].plot(x2, y2) ax[1, 0].plot(x3, y3) ax[1, 1].plot(x4,y4)            # Display Graph            fig.tight_layout() plt.show()                  
  • In the above example, we plot multiple plots in a figure area. And we want to change the background color of the specific plot.
  • Here we use the set_facecolor() method to change the background of the plot.
  • We use the set_facecolor() method with the first subplot and set its background to "Cyan".
Matplotlib change background color subplot
Set the background color of a specific subplot

Read: Matplotlib log log plot

Matplotlib change background color based on value

If we want to set the background color on a specific area of the plot.

We have to pass the "facecolor" argument to the axhspam() method and axvspan() method.

Here axhspam and axvspan is horizontal and vertical rectangular area respectively.

The syntax for this is given below:

          matplotlib.axes.Axes.axhspan(facecolor=None)  matplotlib.axes.Axes.axvspan(facecolor=None)        

The above-used parameter is outlined as below:

  • facecolor: to set specfic color

Let's understand the concept with the help of an example:

                      # Import Library            import matplotlib.pyplot as plt            # Plot figure            plt.figure() plt.xlim(0, 10) plt.ylim(0, 10)            # Set color            for i in range(0, 6):     plt.axhspan(i, i+.3, facecolor='r')     plt.axvspan(i, i+.15, facecolor='b')            # Display graph            plt.show()        

In the above example, we use the axhspan() and axvspan() method to set background color by values. Here we pass a "facecolor" parameter to set the color.

Matplotlib change background color based on value
Change background color by value

You may also like to read the following articles.

  • Matplotlib plot_date
  • Matplotlib save as png
  • Matplotlib plot error bars
  • Matplotlib dashed line
  • Matplotlib scatter marker
  • Matplotlib bar chart labels
  • Matplotlib invert y axis

In this Python tutorial, we have discussed the "Matplotlib change background color" and we have also covered some examples related to it. There are the following topics that we have discussed in this tutorial.

  • Matplotlib change background color
  • Matplotlib change background color of plot
  • Matplotlib change background color example
  • Matplotlib change background color default
  • Matplotlib change background color change default color
  • Matplotlib change background color inner and outer color
  • Matplotlib change background color to image
  • Matplotlib change background color transparent
  • Matplotlib change background color legend
  • Matplotlib change background color subplot
  • Matplotlib change background color based on value

Matplotlib Why Is Plot Background Blue

Source: https://pythonguides.com/matplotlib-change-background-color/

0 Response to "Matplotlib Why Is Plot Background Blue"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel