如何在 Matplotlib 中更改繪圖大小

在數據可視化中,圖是可視化表示數據的最有效方式。如果沒有詳細的圖表,它會顯得很複雜。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-hant/n/127091.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
MYRTI的頭像MYRTI
上一篇 2024-10-03 23:13
下一篇 2024-10-03 23:13

相關推薦

  • 如何在PyCharm中安裝OpenCV?

    本文將從以下幾個方面詳細介紹如何在PyCharm中安裝OpenCV。 一、安裝Python 在安裝OpenCV之前,請確保已經安裝了Python。 如果您還沒有安裝Python,可…

    編程 2025-04-29
  • 如何在Python中實現平方運算?

    在Python中,平方運算是常見的數學運算之一。本文將從多個方面詳細闡述如何在Python中實現平方運算。 一、使用乘法運算實現平方 平方運算就是一個數乘以自己,因此可以使用乘法運…

    編程 2025-04-29
  • 如何在樹莓派上安裝Windows 7系統?

    隨着樹莓派的普及,許多用戶想在樹莓派上安裝Windows 7操作系統。 一、準備工作 在開始之前,需要準備以下材料: 1.樹莓派4B一台; 2.一張8GB以上的SD卡; 3.下載並…

    編程 2025-04-29
  • 如何在Python中找出所有的三位水仙花數

    本文將介紹如何使用Python語言編寫程序,找出所有的三位水仙花數。 一、什麼是水仙花數 水仙花數也稱為自戀數,是指一個n位數(n≥3),其各位數字的n次方和等於該數本身。例如,1…

    編程 2025-04-29
  • 如何在代碼中打出正確的橫杆

    在編程中,橫杆是一個很常見的符號,但是有些人可能會在打橫杆時出錯。本文將從多個方面詳細介紹如何在代碼中打出正確的橫杆。 一、正常使用橫杆 在代碼中,直接使用“-”即可打出橫杆。例如…

    編程 2025-04-29
  • Python最強大的製圖庫——Matplotlib

    Matplotlib是Python中最強大的數據可視化工具之一,它提供了海量的製圖、繪圖、繪製動畫的功能,通過它可以輕鬆地展示數據的分布、比較和趨勢。下面將從多個方面對Matplo…

    編程 2025-04-29
  • 如何在Spring Cloud中整合騰訊雲TSF

    本篇文章將介紹如何在Spring Cloud中整合騰訊雲TSF,並提供完整的代碼示例。 一、TSF簡介 TSF (Tencent Serverless Framework)是騰訊雲…

    編程 2025-04-29
  • Akka 設置郵箱大小的方法和注意事項

    為了保障系統的穩定性和可靠性,Akka 允許用戶設置郵箱大小。本文將介紹如何在 Akka 中設置郵箱大小,並且提供一些注意事項,以幫助讀者解決可能遇到的問題。 一、設置郵箱大小 A…

    編程 2025-04-28
  • 如何在服務器上運行網站

    想要在服務器上運行網站,需要按照以下步驟進行配置和部署。 一、選擇服務器和域名 想要在服務器上運行網站,首先需要選擇一台雲服務器或者自己搭建的服務器。雲服務器會提供更好的穩定性和可…

    編程 2025-04-28
  • 如何在谷歌中定位系統彈框元素

    本文將從以下幾個方面為大家介紹如何在谷歌中準確地定位系統彈框元素。 一、利用開發者工具 在使用谷歌瀏覽器時,我們可以通過它自帶的開發者工具來定位系統彈框元素。 首先,我們可以按下F…

    編程 2025-04-28

發表回復

登錄後才能評論