在數據可視化中,圖是可視化表示數據的最有效方式。如果沒有詳細的圖表,它會顯得很複雜。Python 有 Matplotlib ,用於以繪圖形式表示數據。
用戶應該在創建圖時優化圖的大小。在本教程中,我們將討論根據用戶所需尺寸更改默認繪圖大小或調整給定繪圖大小的各種方法。
用戶可以使用 set_figheight() 更改高度,使用 set_figwidth() 更改地塊寬度。
示例:
# first, import the matplotlib library
import matplotlib.pyplot as plot
# The values on x-axis
x_axis = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
# The values on y-axis
y_axis = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
# Now, name the x and y axis
plot.xlabel('X - AXIS')
plot.ylabel('Y - AXIS')
#Then, plot the line plot with its default size
print ("The plot is plotted in its default size: ")
plot.plot(x_axis, y_axis)
plot.show()
# Now, plot the line plot after changing the size of its width and height
K = plot.figure()
K.set_figwidth(5)
K.set_figheight(2)
print ("The plot is plotted after changing its size: ")
plot.plot(x_axis, y_axis)
plot.show()
輸出:
The plot is plotted in its default size:
The plot is plotted after changing its size:
figsize() 函數取兩個參數,即以英寸為單位的寬度和高度。默認情況下,寬度= 6.4 英寸,高度= 4.8 英寸英寸。
語法:
Plot.figure(figsize = (x_axis, y_axis)
其中 x 軸為寬度, y 軸為高度,單位為英寸。
示例:
# First, import the matplotlib library
import matplotlib.pyplot as plot
# The values on x-axis
x_axis = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
# The values on y-axis
y_axis = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
#Then, plot the line plot with its default size
print ("The plot is plotted in its default size: ")
plot.plot(x_axis, y_axis)
plot.show()
# Now, plot the line plot after changing the size of figure to 3 X 3
plot.figure(figsize = (3, 3))
print ("The plot is plotted after changing its size: ")
plot.plot(x_axis, y_axis)
plot.show()
輸出:
The plot is plotted in its default size:
The plot is plotted after changing its size:
用戶可以根據自己的需要,通過設置圖形、圖形尺寸、來永久改變圖形的默認尺寸
示例:
# First, import the matplotlib library
import matplotlib.pyplot as plot
# The values on x-axis
x_axis = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
# The values on y-axis
y_axis = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
# now, name the x axis
plot.xlabel('X - AXIS')
# name the y axis
plot.ylabel('Y - AXIS')
#Then, plot the line plot with its default size
print ("The plot is plotted in its default size: ")
plot.plot(x_axis, y_axis)
plot.show()
# Now, change the rc parameters and plot the line plot after changing the size.
plot.rcParams['figure.figsize'] = [3, 3]
print ("The plot is plotted after changing its size: ")
plot.plot(x_axis, y_axis)
plot.show()
plot.scatter(x_axis, y_axis)
plot.show()
輸出:
The plot is plotted in its default size:
The plot is plotted after changing its size:
原創文章,作者:MYRTI,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/127091.html