Mục lụ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 nhãn vào các lô hàng Thêm nhãn vào các lô thanhThêm văn bản vào cốt truyệ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=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[]
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
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[]
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
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]
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
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ânVò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