如何使用Python在 Matplotlib 中更改图形图的透明度?
Matplotlib是Python中的一个库,它是数值型的——NumPy库的数学扩展。 Pyplot是atplotlib模块的一个基于状态的接口,它提供了一个类似 MATLAB 的接口。可以在Pyplot中使用的图有线图、轮廓图、直方图、散点图、3D 图等。
为了改变matplotlib中图形的透明度,我们将使用matplotlib.pyplot.plot()函数。情节() matplotlib库的pyplot模块中的函数用于制作 2D 插图。
Syntax: matplotlib.pyplot.plot(\*args, scalex=True, scaley=True, data=None, \*\*kwargs)
Parameters: This method accept the following parameters that are described below:
- x, y: These parameter are the horizontal and vertical coordinates of the data points. x values are optional.
- fmt: This parameter is an optional parameter and it contains the string value.
- data: This parameter is an optional parameter and it is an object with labelled data.
Returns: This returns the following:
- lines : This returns the list of Line2D objects representing the plotted data.
我们将要使用的另一个参数是alpha参数,该参数负责使用 matplotlib 库描绘的任何插图的透明度。它的值范围从 0 到 1,默认情况下它的值为 1 表示插图的不透明。
下面是一些示例,它们描述了如何使用matplotlib库更改图形图的透明度
示例 1:
Python3
# importing module
import matplotlib.pyplot as plt
# assigning x and y coordinates
y = [0, 1, 2, 3, 4, 5]
x = [0, 5, 10, 15, 20, 25]
# depicting the visualization
plt.plot(x, y, color='green', alpha=0.25)
plt.xlabel('x')
plt.ylabel('y')
# displaying the title
plt.title("Linear graph")
plt.show()
Python3
# importing module
import matplotlib.pyplot as plt
# assigning x and y coordinates
x = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
y = []
for i in range(len(x)):
y.append(max(0, x[i]))
# depicting the visualization
plt.plot(x, y, color='green', alpha=0.75)
plt.xlabel('x')
plt.ylabel('y')
# displaying the title
plt.title(label="ReLU function graph",
fontsize=40,
color="green")
Python3
# importing modules
from matplotlib import pyplot
import numpy
# assigning time values of the signal
# initial time period, final time period
# and phase angle
signalTime = numpy.arange(0, 100, 0.5)
# getting the amplitude of the signal
signalAmplitude = numpy.sin(signalTime)
# depicting the visualization
pyplot.plot(signalTime, signalAmplitude,
color='green', alpha=0.1)
pyplot.xlabel('Time')
pyplot.ylabel('Amplitude')
# displaying the title
pyplot.title("Signal",
loc='right',
rotation=45)
Python3
# importing module
import matplotlib.pyplot as plt
# assigning x and y coordinates
z = [i for i in range(0, 6)]
for i in range(0, 11, 2):
# depicting the visualization
plt.plot(z, z, color='green', alpha=i/10)
plt.xlabel('x')
plt.ylabel('y')
# displaying the title
print('\nIllustration with alpha =', i/10)
plt.show()
输出:
在上面的程序中,线性图是用透明度表示的,即alpha=0.25。
示例 2:
蟒蛇3
# importing module
import matplotlib.pyplot as plt
# assigning x and y coordinates
x = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
y = []
for i in range(len(x)):
y.append(max(0, x[i]))
# depicting the visualization
plt.plot(x, y, color='green', alpha=0.75)
plt.xlabel('x')
plt.ylabel('y')
# displaying the title
plt.title(label="ReLU function graph",
fontsize=40,
color="green")
输出:
在这里,该图非常不透明,因为alpha值接近 1(不透明)。
示例 3:
蟒蛇3
# importing modules
from matplotlib import pyplot
import numpy
# assigning time values of the signal
# initial time period, final time period
# and phase angle
signalTime = numpy.arange(0, 100, 0.5)
# getting the amplitude of the signal
signalAmplitude = numpy.sin(signalTime)
# depicting the visualization
pyplot.plot(signalTime, signalAmplitude,
color='green', alpha=0.1)
pyplot.xlabel('Time')
pyplot.ylabel('Amplitude')
# displaying the title
pyplot.title("Signal",
loc='right',
rotation=45)
输出:
上面的例子描述了一个alpha值为 0.1 的信号。
示例 4:
蟒蛇3
# importing module
import matplotlib.pyplot as plt
# assigning x and y coordinates
z = [i for i in range(0, 6)]
for i in range(0, 11, 2):
# depicting the visualization
plt.plot(z, z, color='green', alpha=i/10)
plt.xlabel('x')
plt.ylabel('y')
# displaying the title
print('\nIllustration with alpha =', i/10)
plt.show()
输出:
上面的程序用可变的 alpha 值描述了相同的插图。