Hướng dẫn python colorbar tick location - vị trí đánh dấu thanh màu python

Ghi chú

Nhấn vào đây để tải xuống mã ví dụ đầy đủhere to download the full example code

Sản xuất ghi nhãn tùy chỉnh cho một colorbar.

Được đóng góp bởi Scott Sinclair

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from numpy.random import randn


# Fixing random state for reproducibility
np.random.seed(19680801)

Tạo cốt truyện với thanh màu thẳng đứng (mặc định)

fig, ax = plt.subplots()

data = np.clip(randn(250, 250), -1, 1)

cax = ax.imshow(data, cmap=cm.coolwarm)
ax.set_title('Gaussian noise with vertical colorbar')

# Add colorbar, make sure to specify tick locations to match desired ticklabels
cbar = fig.colorbar(cax, ticks=[-1, 0, 1])
cbar.ax.set_yticklabels(['< -1', '0', '> 1'])  # vertically oriented colorbar

Hướng dẫn python colorbar tick location - vị trí đánh dấu thanh màu python

Out:

[Text(1, -1, '< -1'), Text(1, 0, '0'), Text(1, 1, '> 1')]

Tạo cốt truyện với thanh màu ngang

fig, ax = plt.subplots()

data = np.clip(randn(250, 250), -1, 1)

cax = ax.imshow(data, cmap=cm.afmhot)
ax.set_title('Gaussian noise with horizontal colorbar')

cbar = fig.colorbar(cax, ticks=[-1, 0, 1], orientation='horizontal')
cbar.ax.set_xticklabels(['Low', 'Medium', 'High'])  # horizontal colorbar

plt.show()

Hướng dẫn python colorbar tick location - vị trí đánh dấu thanh màu python

Từ khóa: Ví dụ về mã Matplotlib, Codex, Python Plot, Pyplot Gallery được tạo bởi Sphinx-Gallery

Người ta có thể tạo ra một thanh màu thẳng đứng như vậy (đơn giản hóa):

import matplotlib.pyplot as plt
import matplotlib as mpl

plt.figure()
c_ax=plt.subplot(111)
cb = mpl.colorbar.ColorbarBase(c_ax,orientation='vertical')
plt.savefig('my_colorbar.png')

Dẫn đến một cái gì đó như thế này (xoay vòng vì lý do không gian):

Hướng dẫn python colorbar tick location - vị trí đánh dấu thanh màu python

Có thể nhận được các nhãn Ticks ở phía đối diện?

Hướng dẫn python colorbar tick location - vị trí đánh dấu thanh màu python

hỏi ngày 29 tháng 4 năm 2016 lúc 13:01Apr 29, 2016 at 13:01

Bạn có thể chuyển vị trí của ve bằng cách sử dụng

fig, ax = plt.subplots()

data = np.clip(randn(250, 250), -1, 1)

cax = ax.imshow(data, cmap=cm.coolwarm)
ax.set_title('Gaussian noise with vertical colorbar')

# Add colorbar, make sure to specify tick locations to match desired ticklabels
cbar = fig.colorbar(cax, ticks=[-1, 0, 1])
cbar.ax.set_yticklabels(['< -1', '0', '> 1'])  # vertically oriented colorbar
5

Vì vậy, ví dụ của bạn:

import matplotlib.pyplot as plt
import matplotlib as mpl

plt.viridis()

fig=plt.figure()
c_ax=plt.subplot(199)

cb = mpl.colorbar.ColorbarBase(c_ax,orientation='vertical')

c_ax.yaxis.set_ticks_position('left')

plt.savefig('my_colorbar.png')

Hướng dẫn python colorbar tick location - vị trí đánh dấu thanh màu python

Lưu ý Bạn cũng có thể di chuyển nhãn colorbar theo cách tương tự:

c_ax.yaxis.set_label_position('left')

Và cuối cùng, trong trường hợp bạn có một thanh màu ngang và muốn di chuyển các ve và nhãn lên trên cùng, bạn có thể sử dụng chức năng tương đương trên

fig, ax = plt.subplots()

data = np.clip(randn(250, 250), -1, 1)

cax = ax.imshow(data, cmap=cm.coolwarm)
ax.set_title('Gaussian noise with vertical colorbar')

# Add colorbar, make sure to specify tick locations to match desired ticklabels
cbar = fig.colorbar(cax, ticks=[-1, 0, 1])
cbar.ax.set_yticklabels(['< -1', '0', '> 1'])  # vertically oriented colorbar
6:

c_ax.xaxis.set_label_position('top')
c_ax.xaxis.set_ticks_position('top')

Đã trả lời ngày 29 tháng 4 năm 2016 lúc 13:24Apr 29, 2016 at 13:24

tmdavisontmdavisontmdavison

60.7K12 Huy hiệu vàng171 Huy hiệu bạc154 Huy hiệu đồng12 gold badges171 silver badges154 bronze badges

0

Vì một số lý do áp dụng các thay đổi cho các trục đầu vào colorbar (tham số

fig, ax = plt.subplots()

data = np.clip(randn(250, 250), -1, 1)

cax = ax.imshow(data, cmap=cm.coolwarm)
ax.set_title('Gaussian noise with vertical colorbar')

# Add colorbar, make sure to specify tick locations to match desired ticklabels
cbar = fig.colorbar(cax, ticks=[-1, 0, 1])
cbar.ax.set_yticklabels(['< -1', '0', '> 1'])  # vertically oriented colorbar
7) không hoạt động với tôi.

Tuy nhiên, lấy các trục từ đối tượng colorbar và sau đó áp dụng các thay đổi hoạt động độc đáo:

cb.ax.xaxis.set_ticks_position("top")

Đã trả lời ngày 26 tháng 11 năm 2020 lúc 0:08Nov 26, 2020 at 0:08

XrioxrioXrio

1252 Huy hiệu bạc9 Huy hiệu Đồng2 silver badges9 bronze badges

Tôi đã có cùng một vấn đề. Tình huống này có thể phát sinh khi bạn đang thiết lập rõ ràng các trục của thanh màu. Trong trường hợp đó, bạn có thể dễ dàng chỉ định ở phía nào của các trục này, bạn nhận được các con bọ và nhãn. Ví dụ: nếu bạn đã tạo một hình ảnh bằng cách sử dụng imshow

fig = plt.figure()
ax = plt.subplot(1, 1, 1)
im = ax.imshow(data)

Bạn có thể xác định trục cho thanh màu, trong ví dụ này bằng cách sử dụng inset_axes:

fig, ax = plt.subplots()

data = np.clip(randn(250, 250), -1, 1)

cax = ax.imshow(data, cmap=cm.coolwarm)
ax.set_title('Gaussian noise with vertical colorbar')

# Add colorbar, make sure to specify tick locations to match desired ticklabels
cbar = fig.colorbar(cax, ticks=[-1, 0, 1])
cbar.ax.set_yticklabels(['< -1', '0', '> 1'])  # vertically oriented colorbar
0

Sau đó, thêm thanh màu của bạn

fig, ax = plt.subplots()

data = np.clip(randn(250, 250), -1, 1)

cax = ax.imshow(data, cmap=cm.coolwarm)
ax.set_title('Gaussian noise with vertical colorbar')

# Add colorbar, make sure to specify tick locations to match desired ticklabels
cbar = fig.colorbar(cax, ticks=[-1, 0, 1])
cbar.ax.set_yticklabels(['< -1', '0', '> 1'])  # vertically oriented colorbar
1

và sau đó kiểm soát vị trí của ve, v.v.

fig, ax = plt.subplots()

data = np.clip(randn(250, 250), -1, 1)

cax = ax.imshow(data, cmap=cm.coolwarm)
ax.set_title('Gaussian noise with vertical colorbar')

# Add colorbar, make sure to specify tick locations to match desired ticklabels
cbar = fig.colorbar(cax, ticks=[-1, 0, 1])
cbar.ax.set_yticklabels(['< -1', '0', '> 1'])  # vertically oriented colorbar
2

Đã trả lời ngày 26 tháng 11 năm 2019 lúc 20:25Nov 26, 2019 at 20:25

Hướng dẫn python colorbar tick location - vị trí đánh dấu thanh màu python

Tôi không biết tại sao, nhưng không có lệnh nào trong số này làm việc cho tôi. Nếu đó cũng là trường hợp của bạn, tôi đã giải quyết vấn đề bằng cách chỉ định thuộc tính ColorBar

fig, ax = plt.subplots()

data = np.clip(randn(250, 250), -1, 1)

cax = ax.imshow(data, cmap=cm.coolwarm)
ax.set_title('Gaussian noise with vertical colorbar')

# Add colorbar, make sure to specify tick locations to match desired ticklabels
cbar = fig.colorbar(cax, ticks=[-1, 0, 1])
cbar.ax.set_yticklabels(['< -1', '0', '> 1'])  # vertically oriented colorbar
3

trong đó chăm sóc cả ve và nhãn. Nếu bạn vẫn muốn thanh màu ở bên phải của cốt truyện, chỉ cần xác định một trục mới giới hạn ở vị trí bạn muốn và liên kết bạn colorbar với nó.

fig, ax = plt.subplots()

data = np.clip(randn(250, 250), -1, 1)

cax = ax.imshow(data, cmap=cm.coolwarm)
ax.set_title('Gaussian noise with vertical colorbar')

# Add colorbar, make sure to specify tick locations to match desired ticklabels
cbar = fig.colorbar(cax, ticks=[-1, 0, 1])
cbar.ax.set_yticklabels(['< -1', '0', '> 1'])  # vertically oriented colorbar
4

Đã trả lời ngày 25 tháng 5 năm 2020 lúc 14:49May 25, 2020 at 14:49