Hướng dẫn how do you label a single point in a matplotlib graph in python? - làm cách nào để bạn gắn nhãn một điểm duy nhất trong biểu đồ matplotlib trong python?

Cập nhật lần cuối: 10 tháng 10 năm 2020

Mục lục

  • Thêm văn bản vào cốt truyện
  • Thêm nhãn vào các lô hàng
  • Thêm nhãn vào các lô thanh
  • Thêm nhãn vào các điểm trong các sơ đồ phân tán
  • Thêm văn bản vào trục

Phiên bản Matplotlib đã sử dụng 3.x. Xem tất cả mã trên sổ ghi chép này3.x. View all code on this notebook

Thêm văn bản vào cốt truyện

Thêm nhãn vào các lô hàng

Thêm nhãn vào các lô thanh

import matplotlib.pyplot as plt
import numpy as np

plt.clf()

# using some dummy data for this example
xs = np.arange(0,10,1)
ys = np.random.normal(loc=2.0, scale=0.8, size=10)

plt.plot(xs,ys)

# text is left-aligned
plt.text(2,4,'This text starts at point (2,4)')

# text is right-aligned
plt.text(8,3,'This text ends at point (8,3)',horizontalalignment='right')

plt.show()

Hướng dẫn how do you label a single point in a matplotlib graph in python? - làm cách nào để bạn gắn nhãn một điểm duy nhất trong biểu đồ matplotlib trong python?
Thêm nhãn vào các điểm trong các sơ đồ phân tán

Thêm nhãn vào các lô hàng

Thêm nhãn vào các lô thanh

Thêm nhãn vào các điểm trong các sơ đồ phân tán

Hướng dẫn how do you label a single point in a matplotlib graph in python? - làm cách nào để bạn gắn nhãn một điểm duy nhất trong biểu đồ matplotlib trong python?
Thêm văn bản vào trục

Thêm nhãn vào các lô thanh

Thêm nhãn vào các điểm trong các sơ đồ phân tán

import matplotlib.pyplot as plt
import numpy as np

plt.clf()

# using some dummy data for this example
xs = np.arange(0,10,1)
ys = np.random.normal(loc=3, scale=0.4, size=10)

plt.bar(xs,ys)

# zip joins x and y coordinates in pairs
for x,y in zip(xs,ys):

    label = "{:.2f}".format(y)

    plt.annotate(label, # this is the text
                 (x,y), # these are the coordinates to position the label
                 textcoords="offset points", # how to position the text
                 xytext=(0,10), # distance from text to points (x,y)
                 ha='center') # horizontal alignment can be left, right or center

plt.show()

Hướng dẫn how do you label a single point in a matplotlib graph in python? - làm cách nào để bạn gắn nhãn một điểm duy nhất trong biểu đồ matplotlib trong python?
Thêm văn bản vào trục

Thêm nhãn vào các điểm trong các sơ đồ phân tán

Thêm văn bản vào trục

import matplotlib.pyplot as plt
import numpy as np

# using some dummy data for this example
xs = np.random.randint( 0, 10, size=10)
ys = np.random.randint(-5, 5,  size=10)

# plot the points
plt.scatter(xs,ys)

# zip joins x and y coordinates in pairs
for x,y in zip(xs,ys):

    label = f"({x},{y})"

    plt.annotate(label, # this is the text
                 (x,y), # these are the coordinates to position the label
                 textcoords="offset points", # how to position the text
                 xytext=(0,10), # distance from text to points (x,y)
                 ha='center') # horizontal alignment can be left, right or center

Hướng dẫn how do you label a single point in a matplotlib graph in python? - làm cách nào để bạn gắn nhãn một điểm duy nhất trong biểu đồ matplotlib trong python?
Phiên bản Matplotlib đã sử dụng 3.x. Xem tất cả mã trên sổ ghi chép này

Thêm văn bản vào trục

Phiên bản Matplotlib đã sử dụng 3.x. Xem tất cả mã trên sổ ghi chép này

Xem tất cả các tùy chọn bạn có thể chuyển đến plt.text tại đây: Từ khóa hợp lệ args cho plt.txtindividual Axes object just call

import matplotlib.pyplot as plt
import numpy as np

plt.clf()

# using some dummy data for this example
xs = np.arange(0,10,1)
ys = np.random.normal(loc=3, scale=0.4, size=10)

# 'bo-' means blue color, round points, solid lines
plt.plot(xs,ys,'bo-')

# zip joins x and y coordinates in pairs
for x,y in zip(xs,ys):

    label = "{:.2f}".format(y)

    plt.annotate(label, # this is the text
                 (x,y), # these are the coordinates to position the label
                 textcoords="offset points", # how to position the text
                 xytext=(0,10), # distance from text to points (x,y)
                 ha='center') # horizontal alignment can be left, right or center

plt.show()
0:

import matplotlib.pyplot as plt
import numpy as np

# generate sample data to plot
x = np.linspace(0.0,100,50)
y = np.random.uniform(low=0,high=10,size=50)

# get references to the 2 axes with plt.subplots()
fig, (ax1, ax2) = plt.subplots(1,2)

ax1.bar(x,y)

# add text to the axes on the left only
ax1.annotate('Some text in axes ax1', 
             (40, 8), # these are the coordinates to position the label
             color='red') # you can pass any extra params too

ax2.bar(x,y)
# add text to the other axes (on the right)
ax2.annotate('More text in axes ax2', 
             (40, 8), # these are the coordinates to position the label
             color='black',
             size=16)

Hướng dẫn how do you label a single point in a matplotlib graph in python? - làm cách nào để bạn gắn nhãn một điểm duy nhất trong biểu đồ matplotlib trong python?
Sử dụng plt.text(, , ):
(generated with
import matplotlib.pyplot as plt
import numpy as np

plt.clf()

# using some dummy data for this example
xs = np.arange(0,10,1)
ys = np.random.normal(loc=3, scale=0.4, size=10)

# 'bo-' means blue color, round points, solid lines
plt.plot(xs,ys,'bo-')

# zip joins x and y coordinates in pairs
for x,y in zip(xs,ys):

    label = "{:.2f}".format(y)

    plt.annotate(label, # this is the text
                 (x,y), # these are the coordinates to position the label
                 textcoords="offset points", # how to position the text
                 xytext=(0,10), # distance from text to points (x,y)
                 ha='center') # horizontal alignment can be left, right or center

plt.show()
2)

Văn bản có thể được liên kết bên trái hoặc phải

Một lần nữa, zip cùng nhau dữ liệu (x và y) và lặp qua nó, hãy gọi plt.annotate(

import matplotlib.pyplot as plt
import numpy as np

# generate sample data for this example
xs = [1,2,3,4,5,6,7,8,9,10,11,12]
ys = np.random.normal(loc=3.0,size=12)
labels = ['jan','feb','mar','apr','may','jun','jul','aug','sept','oct','nov','dec']

# plot
plt.bar(xs,ys)

# apply custom tick labels
plt.xticks(xs,labels)

# this is the x-axis label you want to write the text for
string_label = "feb"

# figure out which x-axis tick corresponds to that
label_index = labels.index(string_label)
xs_index    = xs[label_index]

# use xs_index to position the text
plt.annotate("foo-bar",
             (xs_index,4)) # these are the coordinates to position the label

Hướng dẫn how do you label a single point in a matplotlib graph in python? - làm cách nào để bạn gắn nhãn một điểm duy nhất trong biểu đồ matplotlib trong python?
import matplotlib.pyplot as plt
import numpy as np

plt.clf()

# using some dummy data for this example
xs = np.arange(0,10,1)
ys = np.random.normal(loc=3, scale=0.4, size=10)

# 'bo-' means blue color, round points, solid lines
plt.plot(xs,ys,'bo-')

# zip joins x and y coordinates in pairs
for x,y in zip(xs,ys):

    label = "{:.2f}".format(y)

    plt.annotate(label, # this is the text
                 (x,y), # these are the coordinates to position the label
                 textcoords="offset points", # how to position the text
                 xytext=(0,10), # distance from text to points (x,y)
                 ha='center') # horizontal alignment can be left, right or center

plt.show()

Nhãn chứa giá trị của y, với 2 chữ số thập phân

Hướng dẫn how do you label a single point in a matplotlib graph in python? - làm cách nào để bạn gắn nhãn một điểm duy nhất trong biểu đồ matplotlib trong python?
Vòng lặp qua các mảng (XS và YS) và gọi plt.annotate(:
exactly at what would be
import matplotlib.pyplot as plt
import numpy as np

plt.clf()

# using some dummy data for this example
xs = np.arange(0,10,1)
ys = np.random.normal(loc=3, scale=0.4, size=10)

# 'bo-' means blue color, round points, solid lines
plt.plot(xs,ys,'bo-')

# zip joins x and y coordinates in pairs
for x,y in zip(xs,ys):

    label = "{:.2f}".format(y)

    plt.annotate(label, # this is the text
                 (x,y), # these are the coordinates to position the label
                 textcoords="offset points", # how to position the text
                 xytext=(0,10), # distance from text to points (x,y)
                 ha='center') # horizontal alignment can be left, right or center

plt.show()
5 coordinates

Làm cách nào để thêm một điểm duy nhất trong matplotlib?

Để vẽ một điểm duy nhất trong matplotlib, hãy sử dụng hàm plt.scatter (~) như vậy:.
Theo mặc định, các điểm mà bạn vẽ thông qua phân tán (~) xuất hiện trong nền:.
Để làm cho các điểm xuất hiện ở phía trước, hãy đặt giá trị zorder dương:.

Làm thế nào để bạn đặt tên cho một điểm trong Python?

hàm () hàm để thêm nhãn văn bản vào các điểm phân tán ...
Thêm nhãn để phân tán các điểm cốt truyện bằng cách sử dụng matplotlib.pyplot.chức năng chú thích () ..
Thêm nhãn để phân tán các điểm cốt truyện bằng cách sử dụng matplotlib.pyplot.văn bản () chức năng ..
Bài viết liên quan - Biểu đồ phân tán Matplotlib ..

Làm cách nào để thêm nhãn vào biểu đồ matplotlib?

Nhãn Matplotlib và tiêu đề..
Thêm nhãn vào trục x và y: nhập Numpy dưới dạng NP.Nhập matplotlib.pyplot như plt.....
Thêm một tiêu đề cốt truyện và nhãn cho trục x và y: nhập Numpy dưới dạng NP.....
Đặt thuộc tính phông chữ cho tiêu đề và nhãn: Nhập Numpy dưới dạng NP.....
Định vị tiêu đề ở bên trái: Nhập Numpy là NP ..

Làm cách nào để chú thích một điểm trong matplotlib?

Chú thích của matplotlib có nghĩa là chúng tôi muốn đặt một đoạn văn bản bên cạnh sự phân tán ...
Nhập thư viện ..
Tạo dữ liệu ..
Lưu trữ tất cả các chú thích trong một danh sách theo thứ tự với chuỗi các điểm sẽ được hiển thị ..
Vẽ lô phân tán ..
Sử dụng một vòng lặp chú thích mỗi điểm ..