简体   繁体   English

matplotlib format_major_ticks不检索轴刻度标签

[英]matplotlib format_major_ticks not retrieving axes tick labels

Using Python, I am trying to get the tick labels of an axis that spans many orders of magnitude to be displayed in scientific notation, which should be relatively straight forward with: ax1.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.2E')) (full code below) 使用Python,我试图得到一个跨越多个数量级的轴的刻度标签,以科学记数法显示,这应该是相对直接的: ax1.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.2E')) (完整代码如下)

but this seems to ignore the tick labels that were previously set, and instead formats range(len(axis_vector)) in scientific notation as shown in Figure 1. The correct tick labels but not displayed in scientific notation (by not using ax1.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.2E')) ) are shown in Figure 2. 但这似乎忽略了先前设置的刻度标签,而是以科学计数法格式化range(len(axis_vector)) ,如图1所示。正确的刻度标签但不以科学记数法显示(不使用ax1.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.2E')) )如图2所示。

Figure 1: 图1:

图1

Figure 2: 图2:

图2

How can I get it to display the actual tick labels in scientific notation? 如何让它以科学计数法显示实际的刻度标签?

Here is the code with randomly generated sample data: 以下是随机生成的示例数据的代码:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import matplotlib.ticker as mtick

y = np.array([1E0,5E0,1E1,5E1,1E2,5E2,1E3,5E3,1E4,5e4,1E5,5E5,1E6,5E5,1E7,5E7])
x = np.arange(0.2,3.2,0.2) 

### generate sample data with random powers of 10 
data = np.zeros(240)

for i in range(len(data)):

    power = np.round(np.random.rand()*10)

    data[i] = 10**power    

image = data.reshape((16,15))

### Plotting 
fig = plt.figure(figsize=(6,6))
ax1 = fig.add_subplot(111)

im = ax1.imshow(image, interpolation='none',norm=LogNorm(vmin=(np.min(image)),vmax=(np.max(image))))

ax1.set_xticks(np.arange(len(x)), minor=False)
ax1.set_xticklabels(x, minor=False)
ax1.set_xlabel('x-axis')

ax1.set_yticks(np.arange(len(y)), minor=False)
ax1.set_yticklabels(y, minor=False)
ax1.set_ylabel('y-axis')

ax1.tick_params(labelbottom='on',labeltop='off', labelleft='on',labelright='off', 
    top='off', right='off')

ax1.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.2E'))  

axcolor = fig.add_axes([0.9, 0.12, 0.03, 0.79])
t = np.logspace(1,11,num=12)
fig.colorbar(im, cax=axcolor, ticks=t, format='%.3E')

The issue is because the tick formatter operates directly on the tick values not the previous labels. 问题是因为刻度格式化程序直接操作刻度而不是之前的标签。 In your case the y tick values are at np.arange(len(x)) 在您的情况下,y刻度值位于np.arange(len(x))

In your code, where you passed an array of numbers to the set_yticklabels method which internally just got the string representation of each value in the array and used that 在你的代码中,你将一个数字数组传递给set_yticklabels方法,该方法在内部只获得了数组中每个值的字符串表示并使用了

labels = [str(x) for x in y]

What you'll want to do is to instead create your custom (formatted) ytick labels and assign those using set_yticklabels 您要做的是改为创建自定义(格式化)的ytick标签,并使用set_yticklabels分配这些标签

labels = ['%.2E' % x for x in y]
ax1.set_yticklabels(labels)

If you wanted to be fancy, you could use your FormatStrFormatter object to do this as well 如果你想要花哨,你也可以使用你的FormatStrFormatter对象来做这件事

formatter = mtick.FormatStrFormatter('%.2E')
labels = [formatter(x) for x in y]
ax1.set_yticklabels(labels)

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM