Ví dụ: trong mã sau:
import numpy as np
import matplotlib.pyplot as plt
N = 10
x = [1,2,3,4,5,6,7,8,9,10]
y = np.random.rand[N]
plt.scatter[x, y]
plt.show[]
Tôi nhận được cốt truyện sau
Như bạn có thể thấy, trong trục X chỉ xuất hiện các giá trị chẵn. Làm thế nào để buộc matplotlib hiển thị tất cả các giá trị, đó là 1 2 3 4 5 6 7 8 9 10?
Để hiển thị tất cả các giá trị nhãn, chúng ta có thể sử dụng set_xticklabels [] & nbsp; và set_yticklabels [] & nbsp; phương thức.set_xticklabels[] and set_yticklabels[] methods.
Các bước
Tạo một danh sách các số [x] có thể được sử dụng để đánh dấu các trục.
Nhận trục bằng cách sử dụng Subplot [] giúp thêm một subplot vào hình hiện tại.
Đặt các ve trên các trục x và y bằng các phương thức set_xticks và set_yticks tương ứng và liệt kê x [từ bước 1].
Đặt nhãn đánh dấu với danh sách nhãn [["một", "hai", "ba", "bốn"]] và xoay 45 bằng set_xticklabels [] và set_yticklabels [].["one", "two", "three", "four"]] and rotation of 45 using set_xticklabels[] and set_yticklabels[].
Để thêm không gian giữa các trục và nhãn đánh dấu, chúng ta có thể sử dụng phương thức tick_params [] với pad & nbsp; đối số giúp thêm không gian. Hướng đối số [IN] giúp đặt ve bên trong các trục và trục [cả hai], các tham số được áp dụng trên cả hai trục.tick_params[] method with pad argument that helps to add space. Argument direction [in] helps to put ticks inside the axes, and axis [both], parameters to be applied on both the axes.
Để hiển thị hình, sử dụng & nbsp; plt.show [] phương thức. plt.show[] method.
Thí dụ
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4] ax1 = plt.subplot[] ax1.set_xticks[x] ax1.set_yticks[x] ax1.set_xticklabels[["one", "two", "three", "four"], rotation=45] ax1.set_yticklabels[["one", "two", "three", "four"], rotation=45] ax1.tick_params[axis="both", direction="in", pad=15] plt.show[]
Đầu ra
Cập nhật vào ngày 11-tháng 5 năm 2021 12:37:08
- Câu hỏi và câu trả lời liên quan
- Làm thế nào để dán nhãn một bản vá trong matplotlib?
- Làm thế nào để dán nhãn một dòng trong matplotlib [python]?
- Làm thế nào để truy cập đối tượng nhãn trục trong matplotlib?
- Làm thế nào để hiển thị phạm vi trục y matplotlib bằng các giá trị tuyệt đối thay vì các giá trị bù?
- Làm thế nào để hiển thị nhiều dòng văn bản trong nhãn Tkinter?
- Nhãn hàng đầu cho matplotlib colorbars
- Làm thế nào để kết hợp một từ điển Python để hiển thị tất cả các giá trị với nhau?
- Làm thế nào để cải thiện vị trí nhãn cho biểu đồ phân tán matplotlib?
- Làm thế nào để đặt nhãn cho một dòng đã được vẽ trong matplotlib?
- Nền Matplotlib nền và vị trí nhãn
- Xoay Axes Nhãn văn bản trong 3D matplotlib
- Làm thế nào để căn chỉnh nhãn trục ở bên phải hoặc trên cùng trong matplotlib?
- Nhãn đánh dấu trục y chồng chéo và nhãn đánh dấu trục x trong matplotlib
- Truy vấn MongoDB để hiển thị tất cả các giá trị không bao gồm ID?
- Làm thế nào để sử dụng nhiều kích thước phông chữ trong một nhãn trong python matplotlib?
Làm cách nào để hiển thị tất cả các nhãn trong matplotlib?
Để hiển thị tất cả các giá trị nhãn, chúng ta có thể sử dụng các phương thức set_xticklabels [] và set_yticklabels [].
Làm thế nào để bạn chỉ định các giá trị cho x
Làm cách nào để hiển thị tất cả các nhãn trong matplotlib?
Để hiển thị tất cả các giá trị nhãn, chúng ta có thể sử dụng các phương thức set_xticklabels [] và set_yticklabels [].
Làm thế nào để bạn chỉ định các giá trị cho x
Hàm xticks [] trong mô-đun pyplot của thư viện matplotlib được sử dụng để đặt các giá trị trục x.
Chỉ cần gọi hàm lô [] và cung cấp các giá trị x và y của bạn. Gọi hàm show [] đầu ra biểu đồ trực quan.
Syntax:
matplotlib.pyplot.xticks[ticks=None, labels=None, **kwargs]
Làm cách nào để hiển thị tất cả các nhãn trong matplotlib?
Hàm xticks [] trong mô-đun pyplot của thư viện matplotlib được sử dụng để đặt các giá trị trục x. |
|
Đọc |
|
Lưu bài viết | Đọc |
Bàn luậnxticks[] function returns following values:
- Trong bài viết này, chúng tôi sẽ xem xét cách tiếp cận để đặt các giá trị trục X trong matplotlib trong ngôn ngữ lập trình python.
- hàm xticks [] chấp nhận các tham số sau:
Thông số
Sự mô tả
Python3
import
matplotlib.pyplot as plt
x
=
[
1
,
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4] ax1 = plt.subplot[] ax1.set_xticks[x] ax1.set_yticks[x] ax1.set_xticklabels[["one", "two", "three", "four"], rotation=45] ax1.set_yticklabels[["one", "two", "three", "four"], rotation=45] ax1.tick_params[axis="both", direction="in", pad=15] plt.show[]0 ____9
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4] ax1 = plt.subplot[] ax1.set_xticks[x] ax1.set_yticks[x] ax1.set_xticklabels[["one", "two", "three", "four"], rotation=45] ax1.set_yticklabels[["one", "two", "three", "four"], rotation=45] ax1.tick_params[axis="both", direction="in", pad=15] plt.show[]2
matplotlib.pyplot.xticks[ticks=None, labels=None, **kwargs]0____6
[
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4] ax1 = plt.subplot[] ax1.set_xticks[x] ax1.set_yticks[x] ax1.set_xticklabels[["one", "two", "three", "four"], rotation=45] ax1.set_yticklabels[["one", "two", "three", "four"], rotation=45] ax1.tick_params[axis="both", direction="in", pad=15] plt.show[]2
,
1
____9____import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4] ax1 = plt.subplot[] ax1.set_xticks[x] ax1.set_yticks[x] ax1.set_xticklabels[["one", "two", "three", "four"], rotation=45] ax1.set_yticklabels[["one", "two", "three", "four"], rotation=45] ax1.tick_params[axis="both", direction="in", pad=15] plt.show[]4
,
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4] ax1 = plt.subplot[] ax1.set_xticks[x] ax1.set_yticks[x] ax1.set_xticklabels[["one", "two", "three", "four"], rotation=45] ax1.set_yticklabels[["one", "two", "three", "four"], rotation=45] ax1.tick_params[axis="both", direction="in", pad=15] plt.show[]6
,
__12import
5=
[
import
8,
matplotlib.pyplot as plt
0,
matplotlib.pyplot as plt
2,
matplotlib.pyplot as plt
44____9____matplotlib.pyplot as plt
6,
matplotlib.pyplot as plt
8
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4] ax1 = plt.subplot[] ax1.set_xticks[x] ax1.set_yticks[x] ax1.set_xticklabels[["one", "two", "three", "four"], rotation=45] ax1.set_yticklabels[["one", "two", "three", "four"], rotation=45] ax1.tick_params[axis="both", direction="in", pad=15] plt.show[]9
x
0
x
1x
2x
3
x
4x
5x
3
x
7x
8x
3
=
0
=
1
Output:
Ví dụ #2:
Trong ví dụ này, chúng tôi sẽ sử dụng đối số xoay để chấp nhận giá trị xoay theo mức độ và nó sẽ xoay các nhãn theo hướng theo chiều kim đồng hồ theo mức độ được chỉ định.
Python3
import
matplotlib.pyplot as plt
x
=
[
1
,
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4] ax1 = plt.subplot[] ax1.set_xticks[x] ax1.set_yticks[x] ax1.set_xticklabels[["one", "two", "three", "four"], rotation=45] ax1.set_yticklabels[["one", "two", "three", "four"], rotation=45] ax1.tick_params[axis="both", direction="in", pad=15] plt.show[]0 ____9
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4] ax1 = plt.subplot[] ax1.set_xticks[x] ax1.set_yticks[x] ax1.set_xticklabels[["one", "two", "three", "four"], rotation=45] ax1.set_yticklabels[["one", "two", "three", "four"], rotation=45] ax1.tick_params[axis="both", direction="in", pad=15] plt.show[]2
matplotlib.pyplot.xticks[ticks=None, labels=None, **kwargs]0____6
[
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4] ax1 = plt.subplot[] ax1.set_xticks[x] ax1.set_yticks[x] ax1.set_xticklabels[["one", "two", "three", "four"], rotation=45] ax1.set_yticklabels[["one", "two", "three", "four"], rotation=45] ax1.tick_params[axis="both", direction="in", pad=15] plt.show[]2
,
1
____9____import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4] ax1 = plt.subplot[] ax1.set_xticks[x] ax1.set_yticks[x] ax1.set_xticklabels[["one", "two", "three", "four"], rotation=45] ax1.set_yticklabels[["one", "two", "three", "four"], rotation=45] ax1.tick_params[axis="both", direction="in", pad=15] plt.show[]4
,
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4] ax1 = plt.subplot[] ax1.set_xticks[x] ax1.set_yticks[x] ax1.set_xticklabels[["one", "two", "three", "four"], rotation=45] ax1.set_yticklabels[["one", "two", "three", "four"], rotation=45] ax1.tick_params[axis="both", direction="in", pad=15] plt.show[]6
,
__12import
5=
[
import
8,
matplotlib.pyplot as plt
0,
matplotlib.pyplot as plt
2,
matplotlib.pyplot as plt
44____9____matplotlib.pyplot as plt
6,
matplotlib.pyplot as plt
8
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4] ax1 = plt.subplot[] ax1.set_xticks[x] ax1.set_yticks[x] ax1.set_xticklabels[["one", "two", "three", "four"], rotation=45] ax1.set_yticklabels[["one", "two", "three", "four"], rotation=45] ax1.tick_params[axis="both", direction="in", pad=15] plt.show[]9
x
0
x
1x
2x
3
x
4x
5x
3
=
0
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4] ax1 = plt.subplot[] ax1.set_xticks[x] ax1.set_yticks[x] ax1.set_xticklabels[["one", "two", "three", "four"], rotation=45] ax1.set_yticklabels[["one", "two", "three", "four"], rotation=45] ax1.tick_params[axis="both", direction="in", pad=15] plt.show[]19
=
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4] ax1 = plt.subplot[] ax1.set_xticks[x] ax1.set_yticks[x] ax1.set_xticklabels[["one", "two", "three", "four"], rotation=45] ax1.set_yticklabels[["one", "two", "three", "four"], rotation=45] ax1.tick_params[axis="both", direction="in", pad=15] plt.show[]21
x
3=
1
Output: