【发布时间】:2022-01-28 06:07:33
【问题描述】:
我的代码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
fig, ax = plt.subplots()
contour_levels = [10,100,500,1000,10000]
h = np.histogram2d(np.random.normal(0,2,10000),np.random.normal(0,2,10000),bins=10)
c = ax.contourf(h[0].T,cmap='magma',norm=LogNorm(),levels=contour_levels,extend='both')
cbar = plt.colorbar(c)
ticks = cbar.ax.yaxis.get_ticklabels()
cbar.ax.tick_params(which='minor',size=8,width=1,colors='white')
cbar.ax.tick_params(which='major',size=15,width=1,colors='white')
[t.set_color('black') for t in ticks]
ax.set_aspect('equal')
plt.show()
结果:
>print(ticks)
[Text(1, 10.0, '$\\mathdefault{10^{1}}$'),
Text(1, 56.234132519034915, '$\\mathdefault{10^{2}}$'),
Text(1, 316.2277660168379, ''),
Text(1, 1778.2794100389228, '$\\mathdefault{10^{3}}$'),
Text(1, 10000.0, '$\\mathdefault{10^{4}}$')]
问题:
令人震惊的是,颜色条刻度标签仅匹配第一个和最后一个刻度上的相应刻度值!次要刻度的奇怪位置暗示主要刻度标签放置错误(导致 10^4 的次要刻度除外)。
知道发生了什么吗?提前谢谢!
【问题讨论】:
标签: python matplotlib