Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过matplotlib中的因子更改打印比例_Python_Plot_Matplotlib - Fatal编程技术网

Python 通过matplotlib中的因子更改打印比例

Python 通过matplotlib中的因子更改打印比例,python,plot,matplotlib,Python,Plot,Matplotlib,我正在用python创建一个情节。有没有办法通过因子重新缩放轴?yscale和xscale命令仅允许我关闭日志刻度 编辑: 例如如果我有一个x比例从1 nm到50 nm的图,那么x比例将从1x10^(-9)到50x10^(-9),我希望它从1变为50。因此,我希望plot函数将放置在plot上的x值除以10^(-9)要设置x轴的范围,可以使用set_xlim(左、右) 更新: 看起来您想要一个相同的绘图,但只更改“刻度值”,您可以通过获取刻度值,然后将其更改为您想要的任何值来实现。因此,根据您的

我正在用python创建一个情节。有没有办法通过因子重新缩放轴?
yscale
xscale
命令仅允许我关闭日志刻度

编辑:

例如如果我有一个
x
比例从1 nm到50 nm的图,那么x比例将从1x10^(-9)到50x10^(-9),我希望它从1变为50。因此,我希望plot函数将放置在plot上的x值除以10^(-9)

要设置x轴的范围,可以使用
set_xlim(左、右)

更新:

看起来您想要一个相同的绘图,但只更改“刻度值”,您可以通过获取刻度值,然后将其更改为您想要的任何值来实现。因此,根据您的需要,它将是这样的:

ticks = your_plot.get_xticks()*10**9
your_plot.set_xticklabels(ticks)

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


正如您所注意到的,
xscale
yscale
不支持简单的线性重新缩放(不幸的是)。作为Hooked答案的替代方案,您可以像这样欺骗标签,而不是弄乱数据:

ticks = ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x*scale))
ax.xaxis.set_major_formatter(ticks)
显示x和y缩放的完整示例:

import numpy as np
import pylab as plt
import matplotlib.ticker as ticker

# Generate data
x = np.linspace(0, 1e-9)
y = 1e3*np.sin(2*np.pi*x/1e-9) # one period, 1k amplitude

# setup figures
fig = plt.figure()
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
# plot two identical plots
ax1.plot(x, y)
ax2.plot(x, y)

# Change only ax2
scale_x = 1e-9
scale_y = 1e3
ticks_x = ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x/scale_x))
ax2.xaxis.set_major_formatter(ticks_x)

ticks_y = ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x/scale_y))
ax2.yaxis.set_major_formatter(ticks_y)

ax1.set_xlabel("meters")
ax1.set_ylabel('volt')
ax2.set_xlabel("nanometers")
ax2.set_ylabel('kilovolt')

plt.show() 
最后我得到了一张照片的学分:


请注意,如果您像我一样拥有
text.usetex:true
,您可能希望将标签包含在
$
中,就像这样:
'${0:g}$'

据我所知,xlim会更改绘图范围,而不会缩放它。请看我的例子。@Yotam-所以您希望绘图是相同的,但是x比例标签上的值会改变?非常简单的解决方案。然而,我发现这会导致标签带有小数,即使使用只应显示整数的股票代码(例如MultipleLocator或MaxNLocator)。我发现这可以通过使用
ticks.astype(int)
而不是
ticks
来解决。不幸的是,
get_xticks()
在绘图之后才起作用。约塔姆,你似乎有很多悬而未决的问题已经得到了回答,但你还没有接受答案。请接受旧问题的答案或添加其他信息,以便我们可以帮助您!这就是我现在想要做的。我认为有更优雅的方式。这可能是更正确的答案。谢谢@Oysteinth这是我们需要的黑客在左边你可以看到右下角的“X10^-9”。使用
FuncFormatter
此功能似乎已禁用。不幸的是,我需要这个功能。
import numpy as np
import pylab as plt
import matplotlib.ticker as ticker

# Generate data
x = np.linspace(0, 1e-9)
y = 1e3*np.sin(2*np.pi*x/1e-9) # one period, 1k amplitude

# setup figures
fig = plt.figure()
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
# plot two identical plots
ax1.plot(x, y)
ax2.plot(x, y)

# Change only ax2
scale_x = 1e-9
scale_y = 1e3
ticks_x = ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x/scale_x))
ax2.xaxis.set_major_formatter(ticks_x)

ticks_y = ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x/scale_y))
ax2.yaxis.set_major_formatter(ticks_y)

ax1.set_xlabel("meters")
ax1.set_ylabel('volt')
ax2.set_xlabel("nanometers")
ax2.set_ylabel('kilovolt')

plt.show()