Sê -ri này sẽ giới thiệu cho bạn về biểu đồ trong Python với matplotlib, được cho là thư viện trực quan hóa và đồ thị phổ biến nhất cho python.installation Cách dễ nhất để cài đặt matplotlib là sử dụng PIP. Loại lệnh sau trong thiết bị đầu cuối: & nbsp; & nbsp;
Installation
The easiest way to install matplotlib is to use pip. Type following command in terminal:
pip install matplotlib
Hoặc, bạn có thể tải xuống từ đây và cài đặt nó theo cách thủ công. & Nbsp; & nbsp;
Bắt đầu [âm mưu một dòng]
Python
import
matplotlib.pyplot as plt
x
=
[
1
,
2
__7import
0____11
import
2=
[
2
,
import
7____________1
import
1
matplotlib.pyplot as plt
1
matplotlib.pyplot as plt
2matplotlib.pyplot as plt
3matplotlib.pyplot as plt
4
matplotlib.pyplot as plt
5matplotlib.pyplot as plt
6matplotlib.pyplot as plt
4
matplotlib.pyplot as plt
8matplotlib.pyplot as plt
9matplotlib.pyplot as plt
4
x
1
Output:
Mã có vẻ tự giải thích. Các bước sau đã được tuân thủ: & nbsp;
- Xác định trục x và các giá trị trục y tương ứng là danh sách.
- Vẽ chúng trên canvas bằng hàm .plot []..plot[] function.
- Đặt tên cho trục x và trục y bằng các hàm .xlabel [] và .ylabel []..xlabel[] and .ylabel[] functions.
- Đưa ra một tiêu đề cho cốt truyện của bạn bằng hàm .title []..title[] function.
- Cuối cùng, để xem cốt truyện của bạn, chúng tôi sử dụng hàm .show []..show[] function.
Âm mưu hai hoặc nhiều dòng trên cùng một lô
Python
import
matplotlib.pyplot as plt
x
=
[
1
,
2
__7import
0____11
import
2=
[
2
,
import
7____________1
import
1
matplotlib.pyplot as plt
5matplotlib.pyplot as plt
6matplotlib.pyplot as plt
4
Mã có vẻ tự giải thích. Các bước sau đã được tuân thủ: & nbsp;
Xác định trục x và các giá trị trục y tương ứng là danh sách.
Vẽ chúng trên canvas bằng hàm .plot [].
matplotlib.pyplot as plt
2matplotlib.pyplot as plt
3matplotlib.pyplot as plt
4
matplotlib.pyplot as plt
5matplotlib.pyplot as plt
6matplotlib.pyplot as plt
4
matplotlib.pyplot as plt
82
5matplotlib.pyplot as plt
4
2
7
x
1
Output:
- Đặt tên cho trục x và trục y bằng các hàm .xlabel [] và .ylabel [].label] which is passed as an argument of the .plot[] function.
- Đưa ra một tiêu đề cho cốt truyện của bạn bằng hàm .title []..legend[] function.
Cuối cùng, để xem cốt truyện của bạn, chúng tôi sử dụng hàm .show [].
Âm mưu hai hoặc nhiều dòng trên cùng một lô
Python
import
matplotlib.pyplot as plt
x
=
[
1
,
2
__7import
0____11
import
2=
[
2
,
import
7____________1
import
1
matplotlib.pyplot as plt
5matplotlib.pyplot as plt
6matplotlib.pyplot as plt
4
Mã có vẻ tự giải thích. Các bước sau đã được tuân thủ: & nbsp;
import
421
,
import
45matplotlib.pyplot as plt
4
import
471
,
import
45matplotlib.pyplot as plt
4
matplotlib.pyplot as plt
2matplotlib.pyplot as plt
3matplotlib.pyplot as plt
4
matplotlib.pyplot as plt
5matplotlib.pyplot as plt
6matplotlib.pyplot as plt
4
matplotlib.pyplot as plt
8import
59matplotlib.pyplot as plt
4
x
1
Output:
Xác định trục x và các giá trị trục y tương ứng là danh sách.
- Vẽ chúng trên canvas bằng hàm .plot [].
- Đặt tên cho trục x và trục y bằng các hàm .xlabel [] và .ylabel [].
- Đưa ra một tiêu đề cho cốt truyện của bạn bằng hàm .title [].
Cuối cùng, để xem cốt truyện của bạn, chúng tôi sử dụng hàm .show [].
Python
import
matplotlib.pyplot as plt
x
=
[
1
,
2
__7import
0____11
import
2=
[
2
,
import
7____________1
import
1
matplotlib.pyplot as plt
5matplotlib.pyplot as plt
6matplotlib.pyplot as plt
4
Mã có vẻ tự giải thích. Các bước sau đã được tuân thủ: & nbsp;
Xác định trục x và các giá trị trục y tương ứng là danh sách.
matplotlib.pyplot as plt
2matplotlib.pyplot as plt
3matplotlib.pyplot as plt
4
matplotlib.pyplot as plt
5matplotlib.pyplot as plt
6matplotlib.pyplot as plt
4
matplotlib.pyplot as plt
8matplotlib.pyplot as plt
24matplotlib.pyplot as plt
4
x
1
Output :
- Mã có vẻ tự giải thích. Các bước sau đã được tuân thủ: & nbsp;plt.bar[] function to plot a bar chart.
- Xác định trục x và các giá trị trục y tương ứng là danh sách.
- Vẽ chúng trên canvas bằng hàm .plot [].tick_labels
Đặt tên cho trục x và trục y bằng các hàm .xlabel [] và .ylabel [].
Python
import
matplotlib.pyplot as plt
Đưa ra một tiêu đề cho cốt truyện của bạn bằng hàm .title [].
matplotlib.pyplot as plt
06matplotlib.pyplot as plt
55,
matplotlib.pyplot as plt
57,
matplotlib.pyplot as plt
59,
matplotlib.pyplot as plt
61,
matplotlib.pyplot as plt
63,
matplotlib.pyplot as plt
65,
matplotlib.pyplot as plt
67,
matplotlib.pyplot as plt
69,
matplotlib.pyplot as plt
71,
matplotlib.pyplot as plt
73,
import
86import
1
matplotlib.pyplot as plt
77 =
matplotlib.pyplot as plt
79matplotlib.pyplot as plt
80import
68matplotlib.pyplot as plt
82matplotlib.pyplot as plt
4
matplotlib.pyplot as plt
84=
________ 180 & nbsp; & nbsp;
matplotlib.pyplot as plt
87matplotlib.pyplot as plt
777____210=
import
23,
‘
matplotlib.pyplot as plt
2x
02matplotlib.pyplot as plt
4
matplotlib.pyplot as plt
5x
05matplotlib.pyplot as plt
4
matplotlib.pyplot as plt
8x
08matplotlib.pyplot as plt
4
x
1
Output:
- Ở đây, chúng tôi sử dụng hàm plt.hist [] để vẽ biểu đồ.plt.hist[] function to plot a histogram.
- Tần số được truyền như danh sách lứa tuổi.ages list.
- Phạm vi có thể được đặt bằng cách xác định một tuple chứa các giá trị tối đa và tối đa.
- Bước tiếp theo là đối với các bin bin, phạm vi của các giá trị, đó là chia toàn bộ phạm vi giá trị thành một loạt các khoảng thời gian và sau đó đếm số lượng giá trị rơi vào mỗi khoảng thời gian. Ở đây chúng tôi đã xác định các thùng = 10. Vì vậy, có tổng cộng 100/10 = 10 khoảng.bin” the range of values—that is, divide the entire range of values into a series of intervals—and then count how many values fall into each interval. Here we have defined bins = 10. So, there are a total of 100/10 = 10 intervals.
Cốt truyện phân tán
Python
import
matplotlib.pyplot as plt
Các
Các
x
59=
x
61x
62=
x
644____365
x
66import
32=
x
69x
70=
__240matplotlib.pyplot as plt
4
matplotlib.pyplot as plt
2matplotlib.pyplot as plt
3matplotlib.pyplot as plt
4
matplotlib.pyplot as plt
5matplotlib.pyplot as plt
6matplotlib.pyplot as plt
4
matplotlib.pyplot as plt
8x
81matplotlib.pyplot as plt
4
2
7
x
1
Output:
- Ở đây, chúng tôi sử dụng hàm plt.scatter [] để vẽ biểu đồ phân tán.plt.scatter[] function to plot a scatter plot.
- Là một dòng, chúng tôi định nghĩa X và các giá trị trục y tương ứng ở đây.
- Đối số đánh dấu được sử dụng để đặt ký tự để sử dụng làm điểm đánh dấu. Kích thước của nó có thể được xác định bằng tham số S. argument is used to set the character to use as a marker. Its size can be defined using the s parameter.
Pie-chart
Python
import
matplotlib.pyplot as plt
Các
Các
x
59=
x
61x
62=
x
644____365
x
66import
32=
x
69x
70=
__240matplotlib.pyplot as plt
4
matplotlib.pyplot as plt
8x
81matplotlib.pyplot as plt
4
Ở đây, chúng tôi sử dụng hàm plt.scatter [] để vẽ biểu đồ phân tán.
2
7
x
1
Là một dòng, chúng tôi định nghĩa X và các giá trị trục y tương ứng ở đây.
- Đối số đánh dấu được sử dụng để đặt ký tự để sử dụng làm điểm đánh dấu. Kích thước của nó có thể được xác định bằng tham số S.plt.pie[] method.
x
87=
[
x
90import
68______392labels using a list called activities.x
98=
[
import
0import
68matplotlib.pyplot as plt
57import
68__slices.=
09=
[
=
12import
68=
14import
68=
16import
68=
18import
1colors.- ‘ will show a shadow beneath each label in pie chart.
- Các rotates the start of the pie chart by given degrees counterclockwise from the x-axis.
- ‘ is used to set the fraction of radius with which we offset each wedge.
- Đầu ra của chương trình trên trông như thế này: & nbsp; is used to format the value of each label. Here, we have set it to show the percentage value only upto 1 decimal place.
Ở đây, chúng tôi vẽ một biểu đồ hình tròn bằng cách sử dụng phương thức plt.pie [].
Python
import
matplotlib.pyplot as plt
Các
Các
x
59=
x
61x
62=
x
644____365
matplotlib.pyplot as plt
1
x
1
x
66import
32=
x
69x
70=
__240matplotlib.pyplot as plt
4
matplotlib.pyplot as plt
8x
81matplotlib.pyplot as plt
4
Ở đây, chúng tôi sử dụng hàm plt.scatter [] để vẽ biểu đồ phân tán.NumPy which is a general-purpose array-processing package in python.
- Là một dòng, chúng tôi định nghĩa X và các giá trị trục y tương ứng ở đây.the np.arange[] method in which the first two arguments are for range and the third one for step-wise increment. The result is a NumPy array.
- Đối số đánh dấu được sử dụng để đặt ký tự để sử dụng làm điểm đánh dấu. Kích thước của nó có thể được xác định bằng tham số S.np.sin[] method on the NumPy array.
x
87=
[
x
90import
68______392plt.plot[] function.
x
98=
[
import
0import
68matplotlib.pyplot as plt
57import
68__
=
09=
[
=
12import
68=
14import
68=
16import
68=
18import
1- ‘
CácNikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your
article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.