简体   繁体   English

在Python中更改图形的x和y轴上的值

[英]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. 这将显示一个图形,其中y轴显示负数。 I was wondering how you make the graph display it as a logarithm instead (like what the list y was initially defined to be). 我想知道如何使图形显示为对数(例如列表y最初定义为)。

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. 例如10 ^ x = 1/2,x 必须为负数才能得到分数。 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. 最简单的方法是每个y值取10^x ,其中x是十进制值。 Then you will have the y output as decimals, instead of negative numbers. 然后,您将把y输出显示为小数而不是负数。

Then you can set .ylabel() as "log scale". 然后,您可以将.ylabel()设置为“对数刻度”。

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. 您应该能够使用yticks代替xticks在回答中。

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

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