简体   繁体   English

以 2 为底的对数刻度

[英]Logarithmic scale with base 2

I have the following pairs of points that I want to plot using a logarithmic scale.我有以下几对点,我想要 plot 使用对数刻度。

import matplotlib.pyplot as plt
f, ax = plt.subplots(1)
xdata = [256, 512, 1024, 2048]
ydata = [1, 2, 30, 150]
ax.scatter(xdata, ydata)
ax.set_ylim(ymin=0)
ax.set_xlim(xmin=0)
plt.show(f)

在此处输入图像描述

The points render but I don't know how to make the scale logarithmic to the base of 2 on both axes.这些点呈现,但我不知道如何使比例在两个轴上都以 2 为底进行对数。 I want every increment to double the value eg x increments should be 0, 256, 512, 1024, 2048 (bytes) and y increments should be minutes where the data now is in seconds: 0, 1, 2, 4, 8, 16, 32.我希望每个增量都将值加倍,例如 x 增量应该是 0、256、512、1024、2048(字节),y 增量应该是分钟,现在数据以秒为单位:0、1、2、4、8、16 , 32.

Can it be done?可以吗?

What you want is called a logarithmic scale (rather than exponential). 你想要的是一个对数尺度(而不是指数)。 I guess this is why you failed to find the answer yourself, otherwise you could easily arrive at the corresponding matplotlib example . 我想这就是你自己找不到答案的原因,否则你很容易就会得到相应的matplotlib例子

The following should work (the only discrepancy from your requirements is that the axes labels are in the form of 2 n rather than plain numbers): 以下应该有效(与您的要求唯一不同的是轴标签的形式为2 n而不是普通数字):

import matplotlib.pyplot as plt
f, ax = plt.subplots(1)
xdata = [256, 512, 1024, 2048]
ydata = [1, 2, 30, 150]
plt.xscale('log', basex=2)
plt.yscale('log', basey=2)
ax.scatter(xdata, ydata)
ax.set_ylim(ymin=0)
plt.show(f)

If someone is looking for the new way to do it, since如果有人正在寻找新的方法来做到这一点,因为

plt.xscale('log', basex=2)

is not working anymore, use不再工作,使用

ax.set_xscale('log', base=2)

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

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