Làm thế nào để bạn vẽ một giá trị lớn trong python?

Nếu bạn đang sử dụng Jupyter Notebook, bạn có thể gửi lệnh này

barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
1 một lần để tự động hiển thị hoặc hiển thị các ô mà không cần nhập
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
2 sau khi tạo từng ô

Các chức năng được sử dụng cho các loại biểu đồ khác nhau

Các bảng sau đây giải thích các biểu đồ khác nhau cùng với các hàm được xác định cho các biểu đồ này trong thư viện matplotlib. Loại lô đất Lô chức năng [Mặc định]plt. biểu đồ[  ]biểu đồ thanh dọc biểu đồplt. thanh[  ]biểu đồ thanh ngangplt. barh[  ]histogramplt. lịch sử[  ]boxplotplt. hộp[  ]ô khu vựcplt. diện tích[  ]ô phân tánplt. phân tán[  ]biểu đồ hình trònplt. pie[  ]ô lục giác bin plott. hexbin[  ]

1. Thanh biểu đồ

Biểu đồ thanh được sử dụng để so sánh giữa các danh mục hoặc nhóm khác nhau. Giả sử bạn muốn hiển thị so sánh giữa các thành phố về thu nhập trung bình hàng năm. Hãy thử với biểu đồ thanh cơ bản.
plt.title["Simple Bar graph"] # Name title of the graph
plt.xlabel['Students'] # Assign the name of the x axis
plt.ylabel["Math Score"] # Assign the name of the y axis
plt.bar[x, y, color='red'] # Change bar color
plt.show[]

Bạn có thể tạo kiểu cho biểu đồ của mình bằng các chức năng sau -
  • barplot = plt.bar[x, y]
    for bar in barplot:
        yval = bar.get_height[]
        plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
        
    plt.title["Simple Bar graph"]
    plt.xlabel['Students']
    plt.ylabel["Math Score"]
    
    3 để chỉ định tiêu đề cốt truyện của bạn
  • barplot = plt.bar[x, y]
    for bar in barplot:
        yval = bar.get_height[]
        plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
        
    plt.title["Simple Bar graph"]
    plt.xlabel['Students']
    plt.ylabel["Math Score"]
    
    4 để ghi nhãn trục x
  • barplot = plt.bar[x, y]
    for bar in barplot:
        yval = bar.get_height[]
        plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
        
    plt.title["Simple Bar graph"]
    plt.xlabel['Students']
    plt.ylabel["Math Score"]
    
    5 để dán nhãn trục y
  • tùy chọn
    barplot = plt.bar[x, y]
    for bar in barplot:
        yval = bar.get_height[]
        plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
        
    plt.title["Simple Bar graph"]
    plt.xlabel['Students']
    plt.ylabel["Math Score"]
    
    6 trong plt. bar[ ] để xác định màu của các thanh

Cách hiển thị giá trị hoặc nhãn trên đầu thanh

Không dễ dàng và đơn giản để hiển thị các giá trị trên biểu đồ thanh vì không có hàm dựng sẵn cho tác vụ này trong thư viện matplotlib. Do đó, chúng tôi phải viết mã của mình để hoàn thành nhiệm vụ này.
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]

Làm thế nào mã này hoạt động?

Chúng ta cần hiểu logic để hiển thị nhãn trên đầu mỗi thanh. Đầu tiên chúng ta cần tìm ra vị trí mà chúng ta cần hiển thị các nhãn.
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
7 trả về chiều cao của hình chữ nhật của mỗi thanh, về cơ bản là giá trị của trục y. Để tìm giá trị của trục x, chúng ta có thể sử dụng hàm
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
8 và
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
9.
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
10 được sử dụng để đặt văn bản trên biểu đồ. Cú pháp của plt. text[ ]
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
1Chúng tôi đã sử dụng int[yval] thay vì yval vì theo mặc định, nó hiển thị các giá trị ở dạng thập phân

Cách ẩn trục

Nhiều lần chúng tôi ẩn trục y để tạo nét thẩm mỹ cho biểu đồ thanh của chúng tôi. Để làm điều này trong matplotlib, chúng ta có thể tận dụng chức năng
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
11.
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
12 có nghĩa là danh sách trống.
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
5

Cách hiển thị giá trị chuỗi trong Biểu đồ thanh

Thật đơn giản nếu bạn đang sử dụng matplotlib phiên bản 2. 1 trở đi. Để kiểm tra phiên bản của gói python, bạn có thể sử dụng
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
13 trong sổ ghi chép Jupyter. Để nâng cấp nó, bạn có thể gửi mã
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
14
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
8

Nếu bạn đang sử dụng phiên bản trước matplotlib 2. 1, matplotlib không nhận các giá trị chuỗi trong trục x trong biểu đồ thanh, vì vậy chúng tôi cần tìm giải pháp thay thế để giải quyết vấn đề này. Giải pháp là hiển thị các giá trị chuỗi dưới dạng nhãn và phạm vi [len[x]] sẽ hiển thị các giá trị từ 1 đến 5 theo trục x. plt. xticks có thể được sử dụng cho nhiệm vụ này. Nó tuân theo cú pháp

barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
15____20

Biểu đồ thanh ngang

Ngày nay, các nhà phân tích thích hiển thị biểu đồ thanh ngang thay vì biểu đồ thanh cột để so sánh vì nó trông chuyên nghiệp và thanh lịch hơn về mặt giao diện. Cả hai loại biểu đồ đều phục vụ cùng một mục đích.
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
16 được sử dụng để tạo biểu đồ thanh ngang.
plt.title["Simple Bar graph"] # Name title of the graph
plt.xlabel['Students'] # Assign the name of the x axis
plt.ylabel["Math Score"] # Assign the name of the y axis
plt.bar[x, y, color='red'] # Change bar color
plt.show[]
2

Cách sắp xếp hoặc sắp xếp các thanh

Để sắp xếp các thanh theo thứ tự dựa trên các giá trị không theo thứ tự bảng chữ cái, chúng ta cần kết hợp cả hai danh sách và sau đó sắp xếp chúng dựa trên giá trị của danh sách y. Hàm
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
17 được sử dụng để kết hợp các mục của danh sách x và y.
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
18sắp xếp chúng dựa trên y. Sau đó, chúng tôi chia nhỏ và lưu trữ nó trong danh sách x và y.
plt.title["Simple Bar graph"] # Name title of the graph
plt.xlabel['Students'] # Assign the name of the x axis
plt.ylabel["Math Score"] # Assign the name of the y axis
plt.bar[x, y, color='red'] # Change bar color
plt.show[]
50

Thứ tự đảo ngược của các thanh

barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
19 được sử dụng để tìm ra các trục hiện tại và sau đó chức năng đảo ngược hỗ trợ đảo ngược thứ tự.
plt.title["Simple Bar graph"] # Name title of the graph
plt.xlabel['Students'] # Assign the name of the x axis
plt.ylabel["Math Score"] # Assign the name of the y axis
plt.bar[x, y, color='red'] # Change bar color
plt.show[]
51

Sử dụng Chủ đề / Kiểu chuyên nghiệp cho Đồ thị

Có nhiều chủ đề có sẵn trong mô-đun pyplot. Xem danh sách các chủ đề tích hợp mà bạn có thể tận dụng để làm cho biểu đồ của mình đẹp hơn.
plt.title["Simple Bar graph"] # Name title of the graph
plt.xlabel['Students'] # Assign the name of the x axis
plt.ylabel["Math Score"] # Assign the name of the y axis
plt.bar[x, y, color='red'] # Change bar color
plt.show[]
52
plt.title["Simple Bar graph"] # Name title of the graph
plt.xlabel['Students'] # Assign the name of the x axis
plt.ylabel["Math Score"] # Assign the name of the y axis
plt.bar[x, y, color='red'] # Change bar color
plt.show[]
53Tôi sẽ sử dụng chủ đề
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
50.
plt.title["Simple Bar graph"] # Name title of the graph
plt.xlabel['Students'] # Assign the name of the x axis
plt.ylabel["Math Score"] # Assign the name of the y axis
plt.bar[x, y, color='red'] # Change bar color
plt.show[]
54

đồ thị đường

Biểu đồ đường được sử dụng để hiển thị giá trị của một số mặt hàng theo thời gian. Giả sử bạn cần hiển thị tỷ lệ đỗ của học sinh các trường công lập trong 5 năm qua. Một ví dụ khác - doanh số bán hàng đã thay đổi như thế nào trong 5 năm qua? . Hàm
plt.title["Simple Bar graph"] # Name title of the graph
plt.xlabel['Students'] # Assign the name of the x axis
plt.ylabel["Math Score"] # Assign the name of the y axis
plt.bar[x, y, color='red'] # Change bar color
plt.show[]
55
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
51 được sử dụng cho biểu đồ đường. Đây là loại biểu đồ mặc định.
plt.title["Simple Bar graph"] # Name title of the graph
plt.xlabel['Students'] # Assign the name of the x axis
plt.ylabel["Math Score"] # Assign the name of the y axis
plt.bar[x, y, color='red'] # Change bar color
plt.show[]
56

Pandas có thể tạo biểu đồ bằng cách gọi cốt truyện trực tiếp từ khung dữ liệu. Cốt truyện có thể được gọi bằng cách xác định loại cốt truyện trong tùy chọn

barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
52. Cú pháp của Plot trong Pandas____257
plt.title["Simple Bar graph"] # Name title of the graph
plt.xlabel['Students'] # Assign the name of the x axis
plt.ylabel["Math Score"] # Assign the name of the y axis
plt.bar[x, y, color='red'] # Change bar color
plt.show[]
58

Thêm điểm đánh dấu trong sơ đồ dòng

Bằng cách sử dụng tùy chọn style=, bạn có thể bao gồm điểm đánh dấu với tùy chỉnh về màu sắc và kiểu dáng.
plt.title["Simple Bar graph"] # Name title of the graph
plt.xlabel['Students'] # Assign the name of the x axis
plt.ylabel["Math Score"] # Assign the name of the y axis
plt.bar[x, y, color='red'] # Change bar color
plt.show[]
59

Bạn cũng có thể sử dụng các kiểu này
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
53. Chúng đề cập đến đường tròn, đường gạch ngang, đường chấm gạch ngang. Bạn có thể đề cập đến bất kỳ màu nào ['g' cho màu xanh lá cây, 'b' cho màu xanh dương, 'k' cho màu đen, v.v. ]

Phương pháp

barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
54 được sử dụng để thêm nhãn trục x và y, giới hạn và đánh dấu. Nó cũng có thể được viết như đoạn mã dưới đây -

barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
10Cú pháp của rìu. thiết lập các hàm gia đình tương đương với plt. chức năng tạo kiểu. Xem so sánh của một số trong số họ -
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
11
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
55 bảo gấu trúc tắt huyền thoại

Thêm nhiều dòng trong biểu đồ đường Pandas Way

Trong mã bên dưới, chúng tôi đang tạo một Khung dữ liệu gấu trúc bao gồm doanh số của hai sản phẩm A và B cùng với khoảng thời gian [Năm]. Ý tưởng là so sánh doanh số bán sản phẩm và cách chúng hoạt động trong 5 năm qua.
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
12

Chúng tôi đã bao gồm dòng thứ hai bằng cách thêm nó vào tùy chọn
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
56 là đối tượng trục. Nó giống như một hình nhưng nhiều trục

Làm cách nào để thay đổi khoảng thời gian trên trục x?

Giả sử bạn muốn hiển thị các năm từ 2014 đến 2018 với số gia tăng một năm trên trục x. Nếu các giá trị được tăng thêm 0. 5 thay vì 1 nên đó là vấn đề bạn nên khắc phục. Xem hình ảnh bên dưới -

barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
13

barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
57 có thể được sử dụng để thay đổi tỷ lệ khoảng của trục x. Nó sẽ hiển thị dấu tích bạn muốn hiển thị gì trên trục x

Âm mưu phân tán

Một biểu đồ phân tán chủ yếu được sử dụng để hiển thị mối quan hệ giữa hai biến liên tục. Ví dụ bạn muốn đo lường mối quan hệ giữa chiều cao và cân nặng. Giống như biểu đồ đường, nó cũng có thể được sử dụng để hiển thị xu hướng theo thời gian. Chúng tôi thường vẽ một tập hợp các điểm trên trục x và y

barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
58 được sử dụng để tạo biểu đồ phân tán

barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
14

Biểu đồ tròn

Biểu đồ hình tròn là biểu đồ hình tròn chia dữ liệu thành các phần để hiển thị tỷ lệ số của từng danh mục. Nếu bạn đang hiển thị tỷ lệ phần trăm, thì tất cả chúng sẽ được thêm vào 100%

barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
15

Tùy chỉnh biểu đồ hình tròn

Startangle mặc định là 0. Bằng cách đặt startangle = 90 , mọi thứ sẽ được xoay ngược chiều kim đồng hồ 90 độ. Bằng cách sử dụng tùy chọn phát nổ =, bạn có thể phát nổ các danh mục cụ thể. Trong chương trình sau đây, chúng tôi đang khám phá ba danh mục đầu tiên.
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
16

Biểu đồ

Biểu đồ được sử dụng để hiển thị phân phối tần số của một biến liên tục. Giả sử bạn muốn xem phân phối điểm của sinh viên

barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
17bins đại diện cho số lượng khoảng thời gian bạn muốn hiển thị trên trục x. rwidth hiển thị chiều rộng tương đối của các thanh dưới dạng một phần của chiều rộng thùng

Cách thêm nhiều ô phụ

Với việc sử dụng thư viện matplotlib, chúng ta có thể tạo nhiều biểu đồ con trong cùng một biểu đồ hoặc hình. Matplotlib cung cấp hai giao diện để thực hiện tác vụ này -
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
59 và
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
80. Logic tương tự theo cả hai cách - chúng ta sẽ có một hình và chúng ta sẽ thêm nhiều trục [ô phụ] vào từng hình một

Tôi đã tạo một DataFrame giả để minh họa. Trong ví dụ này, chúng tôi có dữ liệu cho các thành phố có điểm chi phí sinh hoạt [dữ liệu giả mạo. ] của năm 2017 và 2018

barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
18

barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
81 tuân theo quy tắc cú pháp -
ô con [số hàng, số cột, số ô]
.add_subplot[121] means 1 row, 2 columns and 1st plot
. add_subplot[122] có nghĩa là 1 hàng, 2 cột và ô thứ 2

Tương tự,. ô con[1, 2] có nghĩa là 1 hàng và 2 cột

Cả hai phương thức sau đều trả về cùng một kết quả. Nó tạo ra hai ô con và đặt chúng cạnh nhau [theo chiều ngang]

Phương pháp I

barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
19

Phương pháp II

plt.title["Simple Bar graph"] # Name title of the graph
plt.xlabel['Students'] # Assign the name of the x axis
plt.ylabel["Math Score"] # Assign the name of the y axis
plt.bar[x, y, color='red'] # Change bar color
plt.show[]
0

Giải thích chi tiết

3 dòng mã này trả về ô trống [trống] 2 ô con. 121 đề cập đến ô bên trái và 122 đề cập đến ô bên phải. Với việc sử dụng đối tượng ax=, chúng ta sử dụng các trục và điều khiển vị trí của các ô con.
plt.title["Simple Bar graph"] # Name title of the graph
plt.xlabel['Students'] # Assign the name of the x axis
plt.ylabel["Math Score"] # Assign the name of the y axis
plt.bar[x, y, color='red'] # Change bar color
plt.show[]
1

Cách hiển thị các ô phụ theo chiều dọc

Trong phần này, chúng tôi sẽ trình bày cách đặt một ô phụ lên trên ô thứ hai. Ở đây chúng ta đang sử dụng 211 và 222 trong hàm subplot[]. 211 có nghĩa là 2 hàng, 1 cột và ô thứ nhất.
plt.title["Simple Bar graph"] # Name title of the graph
plt.xlabel['Students'] # Assign the name of the x axis
plt.ylabel["Math Score"] # Assign the name of the y axis
plt.bar[x, y, color='red'] # Change bar color
plt.show[]
2

Lời khuyên hữu ích

Cách lưu cốt truyện

plt.title["Simple Bar graph"] # Name title of the graph
plt.xlabel['Students'] # Assign the name of the x axis
plt.ylabel["Math Score"] # Assign the name of the y axis
plt.bar[x, y, color='red'] # Change bar color
plt.show[]
3

Cách đặt các giới hạn khác nhau trong trục

Giả sử bạn có biểu đồ thanh và bạn muốn đặt giới hạn theo cách thủ công cho trục x và y.
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
19 trả về trục. Trong
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
83, bạn có thể gán giới hạn là giá trị nhỏ nhất và lớn nhất của trục y. Tương tự, bạn có thể đặt giới hạn theo trục x.
plt.title["Simple Bar graph"] # Name title of the graph
plt.xlabel['Students'] # Assign the name of the x axis
plt.ylabel["Math Score"] # Assign the name of the y axis
plt.bar[x, y, color='red'] # Change bar color
plt.show[]
4

Làm thế nào để hiển thị nhiều mục trong truyền thuyết

plt.title["Simple Bar graph"] # Name title of the graph
plt.xlabel['Students'] # Assign the name of the x axis
plt.ylabel["Math Score"] # Assign the name of the y axis
plt.bar[x, y, color='red'] # Change bar color
plt.show[]
5

Cách thay đổi kích thước phông chữ, trọng lượng và màu sắc trong biểu đồ

plt.title["Simple Bar graph"] # Name title of the graph
plt.xlabel['Students'] # Assign the name of the x axis
plt.ylabel["Math Score"] # Assign the name of the y axis
plt.bar[x, y, color='red'] # Change bar color
plt.show[]
6

Tập thể dục

Làm bẩn tay với bài tập này. Chuẩn bị biểu đồ hiển thị bên dưới bằng khung dữ liệu
barplot = plt.bar[x, y]
for bar in barplot:
    yval = bar.get_height[]
    plt.text[bar.get_x[] + bar.get_width[]/2.0, yval, int[yval], va='bottom'] #va: vertical alignment y positional argument
    
plt.title["Simple Bar graph"]
plt.xlabel['Students']
plt.ylabel["Math Score"]
84. Chú ý đến từng phần của biểu đồ và cố gắng sao chép nó. Đăng giải pháp của bạn trong hộp bình luận bên dưới

Ghi chú cuối

Tôi hy vọng bài viết này đã cung cấp cho bạn một ý tưởng hợp lý về thư viện matplotlib và cách tạo các ô có tùy chỉnh trong python. Bước tiếp theo của bạn là thực hành, thực hành và thực hành với một số bộ dữ liệu mẫu hoặc có sẵn công khai [có sẵn trên Kho lưu trữ học máy UCI và các trang web kaggle]

Học Python. 50 hướng dẫn Python hàng đầu

Truyền bá.
Chia sẻ Chia sẻ Tweet Đăng ký

bài viết liên quan

Giới thiệu tác giả.

Deepanshu thành lập ListenData với mục tiêu đơn giản - Làm cho phân tích trở nên dễ hiểu và dễ theo dõi. Ông có hơn 10 năm kinh nghiệm trong lĩnh vực khoa học dữ liệu. Trong nhiệm kỳ của mình, ông đã làm việc với các khách hàng toàn cầu trong nhiều lĩnh vực khác nhau như Ngân hàng, Bảo hiểm, Cổ phần tư nhân, Viễn thông và Nhân sự.

Chủ Đề