简体   繁体   中英

Changing the values on the x and y axis of a graph in Python

if I had something like;

import numpy as np, math as m, matplotlib.pyplot as plt
def test():
    x = [1,2,3]
    y = [m.log(0.1),m.log(0.2),m.log(0.3)]

    fig1 = plt.figure()
    plt.plot(x,y)
    plt.show()

This will display a graph with the y axis showing negative numbers. I was wondering how you make the graph display it as a logarithm instead (like what the list y was initially defined to be).

Thanks!

Well the log of a fraction is negative, by the definition of a log. For example 10^x=1/2, x must be negative to get a fractional value. That is the idea behind a log. So your best bet is to display with a log scale so that it displays positive fractions.

The easiest way to do this would be to take 10^x where x is the decimal value, of every y value. Then you will have the y output as decimals, instead of negative numbers.

Then you can set .ylabel() as "log scale".

Edit: I think what you are looking for is answered on this stackoverflow question about making custom ticks. You should be able to use yticks instead of xticks used in the answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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