如何在 Matplotlib 中设置绘图背景颜色?
先决条件:
- Matplotlib
- 麻木
从下图可以推断出一个图由 X 轴、Y 轴、图标题和坐标轴组成。默认情况下,绘图的颜色为白色。如果我们必须设置绘图的背景颜色以使我们的绘图看起来很漂亮,我们必须在绘制图形后通过使用 axes() 属性制作轴对象。
方法:
- 导入模块
- 加载或创建数据
- 绘制正则图
- 创建轴对象
- 将属性 set_facecolor() 设置为所需的颜色。此属性接受颜色的名称或颜色代码
按照给定的示例进行操作以更好地理解。
示例:默认颜色图
Python
# importing library
import matplotlib.pyplot as plt
# giving values for x and y to plot
student_marks = [50, 60, 70, 80, 90]
student_grade = ['B', 'B', 'B+', 'B+', 'A']
plt.plot(student_marks, student_grade)
# Giving x label using xlabel() method
# with bold setting
plt.xlabel("student_marks", fontweight='bold')
# Giving y label using xlabel() method
# with bold setting
plt.ylabel("student_grade", fontweight='bold')
# Giving title to the plot
plt.title("Student Marks v/s Student Grade")
# Showing the plot using plt.show()
plt.show()
Python
# importing library
import matplotlib.pyplot as plt
# giving values for x and y to plot
student_marks = [50, 60, 70, 80, 90]
student_grade = ['B', 'B', 'B+', 'B+', 'A']
plt.plot(student_marks, student_grade)
# Giving x label using xlabel() method
# with bold setting
plt.xlabel("student_marks", fontweight='bold')
ax = plt.axes()
# Setting the background color of the plot
# using set_facecolor() method
ax.set_facecolor("yellow")
# Giving y label using xlabel() method
# with bold setting
plt.ylabel("student_grade", fontweight='bold')
# Giving title to the plot
plt.title("Student Marks v/s Student Grade")
# Showing the plot using plt.show()
plt.show()
Python
# importing libraries
import matplotlib.pyplot as plt
import numpy as np
# giving values for x and y to plot
x = np.arange(0, 10, .1)
y = np.sin(x)
plt.plot(x, y)
# Giving x label using xlabel() method
# with bold setting
plt.xlabel("X")
ax = plt.axes()
# Setting the background color of the
# plot using set_facecolor() method
ax.set_facecolor("violet")
# Giving y label using xlabel() method
# with bold setting
plt.ylabel('sin(x)')
# Showing the plot using plt.show()
plt.show()
Python
# importing libraries
import matplotlib.pyplot as plt
import numpy as np
# giving values for x and y to plot
x = np.arange(0, 10, .1)
y = np.sin(x)
# Set background color of the outer
# area of the plt
plt.figure(facecolor='yellow')
# Plotting the graph between x and y
plt.plot(x, y)
# Giving x label using xlabel() method
# with bold setting
plt.xlabel("X")
ax = plt.axes()
# Setting the background color of the plot
# using set_facecolor() method
ax.set_facecolor("violet")
# Giving y label using xlabel() method with
# bold setting
plt.ylabel('sin(x)')
# Showing the plot using plt.show()
plt.show()
Python
# importing libraries
import matplotlib.pyplot as plt
import numpy as np
# giving values for x and y to plot
x = np.arange(0, 10, .1)
y = np.sin(x)
# Set background color of the outer
# area of the plt
plt.figure(facecolor='#94F008')
# Plotting the graph between x and y
plt.plot(x, y)
# Giving x label using xlabel() method
# with bold setting
plt.xlabel("X")
ax = plt.axes()
# Setting the background color of the plot
# using set_facecolor() method
ax.set_facecolor("#1CC4AF")
# Giving y label using xlabel() method with
# bold setting
plt.ylabel('sin(x)')
# Showing the plot using plt.show()
plt.show()
输出:
示例 2:将背景颜色设置为黄色
Python
# importing library
import matplotlib.pyplot as plt
# giving values for x and y to plot
student_marks = [50, 60, 70, 80, 90]
student_grade = ['B', 'B', 'B+', 'B+', 'A']
plt.plot(student_marks, student_grade)
# Giving x label using xlabel() method
# with bold setting
plt.xlabel("student_marks", fontweight='bold')
ax = plt.axes()
# Setting the background color of the plot
# using set_facecolor() method
ax.set_facecolor("yellow")
# Giving y label using xlabel() method
# with bold setting
plt.ylabel("student_grade", fontweight='bold')
# Giving title to the plot
plt.title("Student Marks v/s Student Grade")
# Showing the plot using plt.show()
plt.show()
输出:
示例 3:将背景颜色设置为紫罗兰色
Python
# importing libraries
import matplotlib.pyplot as plt
import numpy as np
# giving values for x and y to plot
x = np.arange(0, 10, .1)
y = np.sin(x)
plt.plot(x, y)
# Giving x label using xlabel() method
# with bold setting
plt.xlabel("X")
ax = plt.axes()
# Setting the background color of the
# plot using set_facecolor() method
ax.set_facecolor("violet")
# Giving y label using xlabel() method
# with bold setting
plt.ylabel('sin(x)')
# Showing the plot using plt.show()
plt.show()
输出:
设置绘图的外部和内部颜色
我们还可以设置绘图外部的颜色。要设置绘图背景和绘图外部的颜色,我们必须在代码中做的唯一更改是我们必须在绘制图形之前添加plt.figure(faceccolor='color') 。
示例 1:
Python
# importing libraries
import matplotlib.pyplot as plt
import numpy as np
# giving values for x and y to plot
x = np.arange(0, 10, .1)
y = np.sin(x)
# Set background color of the outer
# area of the plt
plt.figure(facecolor='yellow')
# Plotting the graph between x and y
plt.plot(x, y)
# Giving x label using xlabel() method
# with bold setting
plt.xlabel("X")
ax = plt.axes()
# Setting the background color of the plot
# using set_facecolor() method
ax.set_facecolor("violet")
# Giving y label using xlabel() method with
# bold setting
plt.ylabel('sin(x)')
# Showing the plot using plt.show()
plt.show()
输出:
示例 2:使用 html 颜色代码设置绘图颜色
Python
# importing libraries
import matplotlib.pyplot as plt
import numpy as np
# giving values for x and y to plot
x = np.arange(0, 10, .1)
y = np.sin(x)
# Set background color of the outer
# area of the plt
plt.figure(facecolor='#94F008')
# Plotting the graph between x and y
plt.plot(x, y)
# Giving x label using xlabel() method
# with bold setting
plt.xlabel("X")
ax = plt.axes()
# Setting the background color of the plot
# using set_facecolor() method
ax.set_facecolor("#1CC4AF")
# Giving y label using xlabel() method with
# bold setting
plt.ylabel('sin(x)')
# Showing the plot using plt.show()
plt.show()
输出: