通过 matplotlib 中的一个因子更改绘图比例 [英] Changing plot scale by a factor in matplotlib

查看:30
本文介绍了通过 matplotlib 中的一个因子更改绘图比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 python 创建一个情节.有没有办法按一个因子重新缩放轴?yscalexscale 命令只允许我关闭对数缩放.

I am creating a plot in python. Is there a way to re-scale the axis by a factor? The yscale and xscale commands only allow me to turn log scale off.


例如.如果我有一个 x 尺度从 1 nm 到 50 nm 的图,x 尺度的范围将从 1x10^(-9) 到 50x10^(-9),我希望它从1 到 50.因此,我希望绘图函数将放置在绘图上的 x 值除以 10^(-9)


For example. If I have a plot where the x scales goes from 1 nm to 50 nm, the x scale will range from 1x10^(-9) to 50x10^(-9) and I want it to change from 1 to 50. Thus, I want the plot function to divide the x values placed on the plot by 10^(-9)

推荐答案

与其改变刻度,为什么不改变单位呢?制作一个单独的数组 X 的 x 值,其单位为 nm.这样,当您绘制数据时,它就已经是正确的格式了!只需确保添加 xlabel 以指示单位(无论如何都应该始终完成).

Instead of changing the ticks, why not change the units instead? Make a separate array X of x-values whose units are in nm. This way, when you plot the data it is already in the correct format! Just make sure you add a xlabel to indicate the units (which should always be done anyways).

from pylab import *

# Generate random test data in your range
N = 200
epsilon = 10**(-9.0)
X = epsilon*(50*random(N) + 1)
Y = random(N)

# X2 now has the "units" of nanometers by scaling X
X2 = (1/epsilon) * X

subplot(121)
scatter(X,Y)
xlim(epsilon,50*epsilon)
xlabel("meters")

subplot(122)
scatter(X2,Y)
xlim(1, 50)
xlabel("nanometers")

show()

这篇关于通过 matplotlib 中的一个因子更改绘图比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆