Hướng dẫn how do you plot two graphs in python? - làm thế nào để bạn vẽ hai đồ thị trong python?

Điều kiện tiên quyết: matplotlib: Matplotlib

Trong matplotlib, chúng ta có thể vẽ nhiều biểu đồ trong một lô theo hai cách. Một là bằng cách sử dụng hàm Subplot () và các hàm khác bằng cách chồng chất của biểu đồ thứ hai trên i.e đầu tiên, tất cả các biểu đồ sẽ xuất hiện trên cùng một lô. Chúng tôi sẽ xem xét cả hai cách từng người một.

Nhiều sơ đồ sử dụng hàm Subplot ()

Hàm Subplot () là hàm trình bao bọc cho phép lập trình viên vẽ nhiều hơn một biểu đồ trong một hình bằng cách gọi nó một lần.

Cú pháp: matplotlib.pyplot.subplots (nrows = 1, ncols = 1, sharex = false, sharey = false, squeeze = true, subplot_kw = none matplotlib.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)

Parameters:

  1. NROWS, NCOLS: & NBSP; Chúng cung cấp số lượng hàng và cột & nbsp; tương ứng. Ngoài ra, phải lưu ý rằng cả hai tham số này đều là tùy chọn và giá trị mặc định là 1. These gives the number of rows and columns  respectively. Also, it must be noted that both these parameters are optional and the default value is 1.
  2. chia sẻ, chia sẻ: & nbsp; Các tham số này chỉ định về các thuộc tính được chia sẻ giữa các giá trị A và Y.Possible cho chúng có thể là, hàng, col, không có hoặc giá trị mặc định là sai. These parameters specify about the properties that are shared among a and y axis.Possible values for them can be, row, col, none or default value which is False.
  3. Squeeze: & nbsp; Tham số này là một giá trị boolean được chỉ định, yêu cầu lập trình viên có nên vắt ra hay không, có nghĩa là loại bỏ kích thước phụ khỏi mảng. Nó có một giá trị mặc định sai.This parameter is a boolean value specified, which asks the programmer whether to squeeze out, meaning remove the extra dimension from the array. It has a default value False.
  4. subplot_kw: Các tham số này cho phép chúng tôi thêm các từ khóa vào mỗi subplot và giá trị mặc định của nó là không có.This parameters allow us to add keywords to each subplot and its default value is None.
  5. GridSpec_kw: Điều này cho phép chúng tôi thêm lưới trên mỗi subplot và có giá trị mặc định là không có.This allows us to add grids on each subplot and has a default value of None.
  6. ** FIG_KW: Điều này cho phép chúng tôi chuyển bất kỳ đối số từ khóa bổ sung nào khác cho cuộc gọi chức năng và có giá trị mặc định là không có.This allows us to pass any other additional keyword argument to the function call and has a default value of None.

Thí dụ :

Python3

import matplotlib.pyplot as plt

import numpy as np

import math

X =

matplotlib.pyplot as plt6= matplotlib.pyplot as plt8

matplotlib.pyplot as plt9= import1

import2= import4

import5= import7

import8= numpy as np0__12

numpy as np50matplotlib.pyplot as plt30numpy as np9

numpy as np50matplotlib.pyplot as plt30import4import5matplotlib.pyplot as plt5

numpy as np50matplotlib.pyplot as plt3math0math1

Các

numpy as np5math0matplotlib.pyplot as plt30X 3

numpy as np5math0matplotlib.pyplot as plt30import4X 9matplotlib.pyplot as plt5

numpy as np5math0matplotlib.pyplot as plt3math0=5

numpy as np5math0matplotlib.pyplot as plt3math0import4np.arange(1matplotlib.pyplot as plt5

np.arange(3

Đầu ra

Hướng dẫn how do you plot two graphs in python? - làm thế nào để bạn vẽ hai đồ thị trong python?

Nhiều sơ đồ sử dụng hàm Subplot ()

Trong matplotlib, có một hàm khác rất giống với subplot là subplot2grid (). Nó gần giống như hàm Subplot nhưng cung cấp sự linh hoạt hơn để sắp xếp các đối tượng cốt truyện theo nhu cầu của lập trình viên.

Hàm này được viết như sau:

Cú pháp: matplotlib.pyplot.subplot2grid (hình dạng, loc, rowspan = 1, colspan = 1, fig = none, ** kwargs)matplotlib.pyplot.subplot2grid(shape, loc, rowspan=1, colspan=1, fig=None, **kwargs)

Parameter:

  1. Tham số Shapethis là một chuỗi gồm hai giá trị số nguyên cho biết hình dạng của lưới mà chúng ta cần đặt các trục. Mục đầu tiên là cho hàng, trong khi mục thứ hai là dành cho cột.
    This parameter is a sequence of two integer values which tells the shape of the grid for which we need to place the axes. The first entry is for row, whereas the second entry is for column.
  2. Tham số hình dạng locus, thậm chí IOC là một chuỗi gồm 2 giá trị số nguyên, trong đó mục đầu tiên vẫn còn cho hàng và thứ hai là để cột đặt trục trong lưới.
    Like shape parameter, even Ioc is a sequence of 2 integer values, where first entry remains for the row and the second is for column to place axis within grid.
  3. Tham số Rowspanthis lấy giá trị nguyên và số cho biết số lượng hàng cho trục kéo dài hoặc tăng về phía bên phải.
    This parameter takes integer value and the number which indicates the number of rows for the axis to span to or increase towards right side.
  4. Tham số colspanthis lấy giá trị số nguyên và số cho biết số lượng cột cho trục kéo dài hoặc tăng độ dài xuống.
    This parameter takes integer value and the number which indicates the number of columns for the axis to span to or increase the length downwards.
  5. Figthis là một tham số tùy chọn và đưa hình để đặt trục vào. Nó mặc định là hình hiện tại.
    This is an optional parameter and takes Figure to place axis in. It defaults to current figure.
  6. ** kwargsthis cho phép chúng tôi chuyển bất kỳ đối số từ khóa bổ sung nào khác cho cuộc gọi hàm và có giá trị mặc định là không có.
    This allows us to pass any other additional keyword argument to the function call and has a default value of None.

Thí dụ :

Python3

import matplotlib.pyplot as plt

import numpy as np

import math

X =

matplotlib.pyplot as plt6= matplotlib.pyplot as plt8

matplotlib.pyplot as plt9= import1

import2= import4

matplotlib.pyplot as plt42matplotlib.pyplot as plt1matplotlib.pyplot as plt1matplotlib.pyplot as plt45matplotlib.pyplot as plt5

matplotlib.pyplot as plt47matplotlib.pyplot as plt48matplotlib.pyplot as plt5

matplotlib.pyplot as plt50

matplotlib.pyplot as plt51matplotlib.pyplot as plt52matplotlib.pyplot as plt5

matplotlib.pyplot as plt54matplotlib.pyplot as plt1matplotlib.pyplot as plt56

matplotlib.pyplot as plt57matplotlib.pyplot as plt58matplotlib.pyplot as plt5

matplotlib.pyplot as plt60

np.arange(3

Đầu ra

Hướng dẫn how do you plot two graphs in python? - làm thế nào để bạn vẽ hai đồ thị trong python?

Nhiều sơ đồ sử dụng hàm Subplot ()

Trong matplotlib, có một hàm khác rất giống với subplot là subplot2grid (). Nó gần giống như hàm Subplot nhưng cung cấp sự linh hoạt hơn để sắp xếp các đối tượng cốt truyện theo nhu cầu của lập trình viên.

Hàm này được viết như sau:

Thí dụ :

Python3

import matplotlib.pyplot as plt

import numpy as np

import math

X =

matplotlib.pyplot as plt78= matplotlib.pyplot as plt8

matplotlib.pyplot as plt81= import1

Các

matplotlib.pyplot as plt91=matplotlib.pyplot as plt93matplotlib.pyplot as plt87=matplotlib.pyplot as plt96matplotlib.pyplot as plt5

matplotlib.pyplot as plt98matplotlib.pyplot as plt99matplotlib.pyplot as plt5

import01import02matplotlib.pyplot as plt5

import04import05matplotlib.pyplot as plt5

import07

np.arange(3

Đầu ra

Hướng dẫn how do you plot two graphs in python? - làm thế nào để bạn vẽ hai đồ thị trong python?

đường cong chức năng sin và cosine trong một biểu đồ


Làm thế nào để bạn vẽ hai biểu đồ trên cùng một lô trong Python?

Trong matplotlib, chúng ta có thể vẽ nhiều biểu đồ trong một lô theo hai cách.Một là bằng cách sử dụng hàm Subplot () và các hàm khác bằng cách chồng chất của biểu đồ thứ hai trên i.e đầu tiên, tất cả các biểu đồ sẽ xuất hiện trên cùng một lô.using subplot() function and other by superimposition of second graph on the first i.e, all graphs will appear on the same plot.

Làm thế nào để bạn vẽ hai biểu đồ cạnh nhau?

Để tạo ra nhiều lô, chúng tôi sử dụng chức năng Subplot của mô -đun pyplot trong matplotlib.Tham số: NROWS dành cho số lượng hàng có nghĩa là nếu hàng là 1 thì các ô nằm ngang.NColumns là viết tắt của cột có nghĩa là nếu cột là 1 thì âm mưu nằm thẳng đứng.use the subplot function of pyplot module in Matplotlib. Parameters: nrows is for number of rows means if the row is 1 then the plots lie horizontally. ncolumns stands for column means if the column is 1 then the plot lie vertically.

Làm thế nào để bạn vẽ hai điều trong Python?

Bạn có thể vẽ nhiều dòng từ dữ liệu được cung cấp bởi DataFrame trong Python bằng matplotlib.Bạn có thể làm điều đó bằng cách chỉ định các cột khác nhau của DataFrame là tham số trục x và y trong hàm matplotlib.pyplot.plot ().specifying different columns of the dataframe as the x and y-axis parameters in the matplotlib. pyplot. plot() function.