Changing plot scale by a factor in matplotlib

后端 未结 3 1294
半阙折子戏
半阙折子戏 2020-11-27 15:44

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

3条回答
  •  情话喂你
    2020-11-27 16:04

    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()
    

    enter image description here

提交回复
热议问题