如何在 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/n/127091.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
MYRTIMYRTI
上一篇 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

发表回复

登录后才能评论