简体   繁体   English

在 matplotlib 中设置独立于 y 值的 y 轴值

[英]Setting y-axis values independent of y-values in matplotlib

I'm using a while loop to plot multiple outputs against a single input.我正在对 plot 对单个输入使用多个输出的 while 循环。 How can I set my y-axis ticks so that I get evenly spaced ticks with values from 0 to 10?如何设置我的 y 轴刻度,以便获得值从 0 到 10 的均匀间隔刻度? It seems like the ticks are taking in the outputs as the values in some weird order I don't understand.似乎滴答声以我不明白的一些奇怪的顺序将输出作为值。 When I pass in a list to y-ticks it just gives me a subset of the ticks it's currently showing rather than ticks based on the list I pass it.当我将一个列表传递给 y-ticks 时,它只会给我它当前显示的刻度的子集,而不是基于我传递它的列表的刻度。 Here's my code:这是我的代码:

output20 = [0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
outputs = [output1, output2, output3, output4, output5, output6, output7, output8, output9, output10, output11, output12, output13, output14, output15, output16, output17, output18, output19, output20]

import matplotlib.ticker as ticker

plt.figure(figsize = (16, 10))

i = 0
while i < 20:
    if i < 19:
        plt.plot(inputs, outputs[i], label = 'V_pgm = %s'%df.iloc[i+2]['Unnamed: 3'], marker = 'o', linestyle = '') #label = 'V_pgm = %s'%df.iloc[i+2]['Unnamed: 3']
    if i == 19:
        plt.plot(inputs, outputs[i], label = '__nolegend__', marker = '', linestyle = '')
    i+= 1


plt.xlabel("Input Voltage (V)")
plt.ylabel("Load Voltage (V)")

plt.title("Load Voltage (V)")
plt.axis([8, 24, -1, 100])
plt.yticks()
plt.legend()
plt.show()


Output Output

Here's what it looks like when I pass values into y-ticks:这是我将值传递给 y-ticks 时的样子:

output20 = [0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
outputs = [output1, output2, output3, output4, output5, output6, output7, output8, output9, output10, output11, output12, output13, output14, output15, output16, output17, output18, output19, output20]

import matplotlib.ticker as ticker

plt.figure(figsize = (16, 10))

i = 0
while i < 20:
    if i < 19:
        plt.plot(inputs, outputs[i], label = 'V_pgm = %s'%df.iloc[i+2]['Unnamed: 3'], marker = 'o', linestyle = '') #label = 'V_pgm = %s'%df.iloc[i+2]['Unnamed: 3']
    if i == 19:
        plt.plot(inputs, outputs[i], label = '__nolegend__', marker = '', linestyle = '')
    i+= 1


plt.xlabel("Input Voltage (V)")
plt.ylabel("Load Voltage (V)")

plt.title("Load Voltage (V)")
plt.axis([8, 24, -1, 100])
plt.yticks([2, 10])
plt.legend()
plt.show()

Output Output

I want the y-axis to be uncluttered and to be able to set the number of ticks and their values.我希望 y 轴整洁,并且能够设置刻度数及其值。

plt.yticks takes in certain arguments that allow you to space it according to how you want it to be spaced. plt.yticks 接受某些 arguments 允许您根据您希望的间隔方式对其进行间隔。 Since I don't have your output and input data, I made a simple example below and show how the ytick parameters corrispond to changes in the plots.由于我没有您的 output 和输入数据,我在下面做了一个简单的示例,并展示了 ytick 参数如何与图中的变化相对应。

import matplotlib.pyplot as plt
import numpy as np

indices = [1,2,3,4,5,6,7,8,9]
array = [35,27,38,49,95,65,67,32,45]

plt.figure(figsize = (5,5))

plt.plot(array,indices)

# None                                                                                                                                                   
plt.title("No yticks")

# plt.yticks()                                                                                                                                           
# plt.yticks(np.arange(0,5,step=0.2))                                                                                                                    
# plt.yticks(np.arange(0,len(indices),step=0.5))                                                                                                         
plt.show()

在此处输入图像描述

import matplotlib.pyplot as plt
import numpy as np

indices = [1,2,3,4,5,6,7,8,9]
array = [35,27,38,49,95,65,67,32,45]

plt.figure(figsize = (5,5))

plt.plot(array,indices)

# None                                                                                                                                                   
#plt.title("No yticks")                                                                                                                                  

plt.yticks()
plt.title("yticks, no params")

# plt.yticks(np.arange(0,5,step=0.2))                                                                                                                    
# plt.yticks(np.arange(0,len(indices),step=0.5))                                                                                                         
plt.show()

在此处输入图像描述

NOTICE THESE FIRST TWO ARE THE SAME注意前两个是相同的

import matplotlib.pyplot as plt
import numpy as np

indices = [1,2,3,4,5,6,7,8,9]
array = [35,27,38,49,95,65,67,32,45]

plt.figure(figsize = (5,5))

plt.plot(array,indices)

# None                                                                                                                                                   
#plt.title("No yticks")                                                                                                                                  

#plt.yticks()                                                                                                                                            
#plt.title("yticks, no params")                                                                                                                          

plt.yticks(np.arange(0,5,step=0.2))
plt.title("yticks params: less than indice length")

# plt.yticks(np.arange(0,len(indices),step=0.5))                                                                                                         
plt.show()

在此处输入图像描述

import matplotlib.pyplot as plt
import numpy as np

indices = [1,2,3,4,5,6,7,8,9]
array = [35,27,38,49,95,65,67,32,45]

plt.figure(figsize = (5,5))

plt.plot(array,indices)

# None                                                                                                                                                   
#plt.title("No yticks")                                                                                                                                  

#plt.yticks()                                                                                                                                            
#plt.title("yticks, no params")                                                                                                                          

#plt.yticks(np.arange(0,5,step=0.2))                                                                                                                     
#plt.title("yticks params: less than indice length")                                                                                                     

plt.yticks(np.arange(0,len(indices),step=0.5))
plt.title("yticks params: equal to indice length")

plt.show()

在此处输入图像描述

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

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