在matplotlib

时间:2019-10-28 01:54:34

标签: python python-3.x matplotlib

我正在使用while循环针对单个输入绘制多个输出。如何设置y轴刻度线,以使刻度线间隔从0到10?似乎滴答声正在接受输出,因为我不理解某些奇怪的值。当我将列表传递给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()
plt.legend()
plt.show()


Output

当我将值传递到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

我希望y轴整洁,并能够设置刻度数量及其值。

1 个答案:

答案 0 :(得分:0)

plt.yticks带有某些参数,这些参数使您可以根据想要的间距对其进行间距。由于我没有您的输出和输入数据,因此我在下面做了一个简单的示例,并说明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()

enter image description here

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

enter image description here

注意这两个相同的东西

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

enter image description here

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

enter image description here