Как установить минимум на уровне ниже максимума на верхней оси y с помощью matplotlib.pyplot - PullRequest
0 голосов
/ 13 апреля 2020

Я новичок в программировании python и пытаюсь создать временную диаграмму из вывода логического элемента c. Я использую raspbian и данные сохраняются в CSV. Любой код, чтобы перевернуть ось Y, чтобы «0» снизу и «1» сверху? Это потому, что если я получу данные, начиная с «1», график будет «1» внизу и «0» сверху.

Здесь я привожу пример с цифрой «1» внизу. введите описание изображения здесь

enter image description here

## the # in 2 row have some code##

from guizero import App,PushButton
from gpiozero import LED,Button
from time import sleep
import csv
from matplotlib import pyplot as plt
a=LED(2)
b=LED(3)
c=LED(4)
d=LED(14)
E=Button(15,pull_up=False)
F=Button(17,pull_up=False)
G=Button(18,pull_up=False)
H=Button(27,pull_up=False)
I=Button(22,pull_up=False)
J=Button(23,pull_up=False)
K=Button(24,pull_up=False)
L=Button(10,pull_up=False)
M=Button(9,pull_up=False)
N=Button(25,pull_up=False)
O=Button(11,pull_up=False)
P=Button(8,pull_up=False)
Q=Button(7,pull_up=False)
R=Button(5,pull_up=False)
S=Button(6,pull_up=False)
T=Button(12,pull_up=False)
U=Button(13,pull_up=False)
V=Button(19,pull_up=False)
W=Button(16,pull_up=False)
X=Button(26,pull_up=False)
Y=Button(20,pull_up=False)
Z=Button(21,pull_up=False)
t=1/1000000

def nwln_csv():
    with open('hz.csv','w') as file:
        writer=csv.writer(file)
        writer.writerow(["D","C","B","A","choice","choice_output","Z","Y","X","W","V","U","T","S","R","Q","P","O","N","M","L","K","J","I","H","G","F","E"])    
def nxtln_csv():
    print(d.value,c.value,b.value,a.value,choice.value,choice_output.value,Z.value,Y.value,X.value,W.value,V.value,U.value,T.value,S.value,R.value,Q.value,P.value,O.value,N.value,M.value,L.value,K.value,J.value,I.value,H.value,G.value,F.value,E.value)
    with open('hz.csv','a') as file:
        writer=csv.writer(file)
        writer.writerow([d.value,c.value,b.value,a.value,choice.value,choice_output.value,Z.value,Y.value,X.value,W.value,V.value,U.value,T.value,S.value,R.value,Q.value,P.value,O.value,N.value,M.value,L.value,K.value,J.value,I.value,H.value,G.value,F.value,E.value])           
#
#
def input_one():
    nwln_csv()
    d.off()
    c.off()
    b.off()
    a.off()
    sleep(t)
    nxtln_csv()
    a.on()
    sleep(t)
    nxtln_csv()
    d.off()
    c.off()
    b.off()
    a.off()
def input_two():
    nwln_csv()
    d.off()
    c.off()
    b.off()
    a.off()
    sleep(t)
    nxtln_csv()
    a.on()
    sleep(t)
    nxtln_csv()
    b.on()
    a.off()
    sleep(t)
    nxtln_csv()
    a.on()
    sleep(t)
    nxtln_csv()
    d.off()
    c.off()
    b.off()
    a.off() 
def plot_graph():
    def input1_outputz():
        fig,(axa,axz)=plt.subplots(nrows=2,ncols=1,sharex=True)
        axa.set_title('TIMING DIAGRAM')
        axa.plot(a,drawstyle='steps-pre',color='lime')
        axa.set_ylabel('A',rotation=0)
        axz.plot(z,drawstyle='steps-pre',color='red')
        axz.set_ylabel('Z',rotation=0)
        axz.set_xlabel('x axis')
    def input2_outputz():
        fig,(axa,axb,axz)=plt.subplots(nrows=3,ncols=1,sharex=True)
        axa.set_title('TIMING DIAGRAM')
        axa.plot(a,drawstyle='steps-pre',color='lime')
        axa.set_ylabel('A',rotation=0)
        axb.plot(b,drawstyle='steps-pre',color='lime')
        axb.set_ylabel('B',rotation=0)
        axz.plot(z,drawstyle='steps-pre',color='red')
        axz.set_ylabel('Z',rotation=0)
        axz.set_xlabel('x axis')
    def input3_outputz():
        fig,(axa,axb,axc,axz)=plt.subplots(nrows=4,ncols=1,sharex=True)
        axa.set_title('TIMING DIAGRAM')
        axa.plot(a,drawstyle='steps-pre',color='lime')
        axa.set_ylabel('A',rotation=0)
        axb.plot(b,drawstyle='steps-pre',color='lime')
        axb.set_ylabel('B',rotation=0)
        axc.plot(c,drawstyle='steps-pre',color='lime')
        axc.set_ylabel('C',rotation=0)
        axz.plot(z,drawstyle='steps-pre',color='red')
        axz.set_ylabel('Z',rotation=0)
        axz.set_xlabel('x axis')
    def input4_outputz():
        fig,(axa,axb,axc,axd,axz)=plt.subplots(nrows=5,ncols=1,sharex=True)
        axa.set_title('TIMING DIAGRAM')
        axa.plot(a,drawstyle='steps-pre',color='lime')
        axa.set_ylabel('A',rotation=0)
        axb.plot(b,drawstyle='steps-pre',color='lime')
        axb.set_ylabel('B',rotation=0)
        axc.plot(c,drawstyle='steps-pre',color='lime')
        axc.set_ylabel('C',rotation=0)
        axd.plot(d,drawstyle='steps-pre',color='lime')
        axd.set_ylabel('D',rotation=0)
        axz.plot(z,drawstyle='steps-pre',color='red')
        axz.set_ylabel('Z',rotation=0)
        axz.set_xlabel('x axis')

    def input1_outputyz():
        fig,(axa,axy,axz)=plt.subplots(nrows=3,ncols=1,sharex=True)
        axa.set_title('TIMING DIAGRAM')
        axa.plot(a,drawstyle='steps-pre',color='lime')
        axa.set_ylabel('A',rotation=0)
        axy.plot(y,drawstyle='steps-pre',color='plum')
        axy.set_ylabel('Y',rotation=0)
        axz.plot(z,drawstyle='steps-pre',color='red')
        axz.set_ylabel('Z,rotation=0')
        axz.set_xlabel('x axis')
    def input2_outputyz():
        fig,(axa,axb,axy,axz)=plt.subplots(nrows=4,ncols=1,sharex=True)
        axa.set_title('TIMING DIAGRAM')
        axa.plot(a,drawstyle='steps-pre',color='lime')
        axa.set_ylabel('A',rotation=0)
        axb.plot(b,drawstyle='steps-pre',color='lime')
        axb.set_ylabel('B',rotation=0)
        axy.plot(y,drawstyle='steps-pre',color='plum')
        axy.set_ylabel('Y',rotation=0)
        axz.plot(z,drawstyle='steps-pre',color='red')
        axz.set_ylabel('Z',rotation=0)
        axz.set_xlabel('x axis')
    def input3_outputyz():
        fig,(axa,axb,axc,axy,axz)=plt.subplots(nrows=5,ncols=1,sharex=True)
        axa.set_title('TIMING DIAGRAM')
        axa.plot(a,drawstyle='steps-pre',color='lime')
        axa.set_ylabel('A',rotation=0)
        axb.plot(b,drawstyle='steps-pre',color='lime')
        axb.set_ylabel('B',rotation=0)
        axc.plot(c,drawstyle='steps-pre',color='lime')
        axc.set_ylabel('C',rotation=0)
        axy.plot(y,drawstyle='steps-pre',color='plum')
        axy.set_ylabel('Y',rotation=0)
        axz.plot(z,drawstyle='steps-pre',color='red')
        axz.set_ylabel('Z',rotation=0)
        axz.set_xlabel('x axis')
    def input4_outputyz():
        fig,(axa,axb,axc,axd,axy,axz)=plt.subplots(nrows=6,ncols=1,sharex=True)
        axa.set_title('TIMING DIAGRAM')
        axa.plot(a,drawstyle='steps-pre',color='lime')
        axa.set_ylabel('A',rotation=0)
        axb.plot(b,drawstyle='steps-pre',color='lime')
        axb.set_ylabel('B',rotation=0)
        axc.plot(c,drawstyle='steps-pre',color='lime')
        axc.set_ylabel('C',rotation=0)
        axd.plot(d,drawstyle='steps-pre',color='lime')
        axd.set_ylabel('D',rotation=0)
        axy.plot(y,drawstyle='steps-pre',color='plum')
        axy.set_ylabel('Y',rotation=0)
        axz.plot(z,drawstyle='steps-pre',color='red')
        axz.set_ylabel('Z',rotation=0)
        axz.set_xlabel('x axis')

    def input1_outputxz():
        fig,(axa,axx,axy,axz)=plt.subplots(nrows=4,ncols=1,sharex=True)
        axa.set_title('TIMING DIAGRAM')
        axa.plot(a,drawstyle='steps-pre',color='lime')
        axa.set_ylabel('A',rotation=0)
        axx.plot(x,drawstyle='steps-pre',color='plum')
        axx.set_ylabel('X',rotation=0)
        axy.plot(y,drawstyle='steps-pre',color='plum')
        axy.set_ylabel('Y',rotation=0)
        axz.plot(z,drawstyle='steps-pre',color='red')
        axz.set_ylabel('Z,rotation=0')
        axz.set_xlabel('x axis')
    def input2_outputxz():
        fig,(axa,axb,axx,axy,axz)=plt.subplots(nrows=5,ncols=1,sharex=True)
        axa.set_title('TIMING DIAGRAM')
        axa.plot(a,drawstyle='steps-pre',color='lime')
        axa.set_ylabel('A',rotation=0)
        axb.plot(b,drawstyle='steps-pre',color='lime')
        axb.set_ylabel('B',rotation=0)
        axx.plot(x,drawstyle='steps-pre',color='plum')
        axx.set_ylabel('X',rotation=0)
        axy.plot(y,drawstyle='steps-pre',color='plum')
        axy.set_ylabel('Y',rotation=0)
        axz.plot(z,drawstyle='steps-pre',color='red')
        axz.set_ylabel('Z',rotation=0)
        axz.set_xlabel('x axis')
    def input3_outputxz():
        fig,(axa,axb,axc,axx,axy,axz)=plt.subplots(nrows=6,ncols=1,sharex=True)
        axa.set_title('TIMING DIAGRAM')
        axa.plot(a,drawstyle='steps-pre',color='lime')
        axa.set_ylabel('A',rotation=0)
        axb.plot(b,drawstyle='steps-pre',color='lime')
        axb.set_ylabel('B',rotation=0)
        axc.plot(c,drawstyle='steps-pre',color='lime')
        axc.set_ylabel('C',rotation=0)
        axx.plot(x,drawstyle='steps-pre',color='plum')
        axx.set_ylabel('X',rotation=0)
        axy.plot(y,drawstyle='steps-pre',color='plum')
        axy.set_ylabel('Y',rotation=0)
        axz.plot(z,drawstyle='steps-pre',color='red')
        axz.set_ylabel('Z',rotation=0)
        axz.set_xlabel('x axis')
    def input4_outputxz():
        fig,(axa,axb,axc,axd,axx,axy,axz)=plt.subplots(nrows=7,ncols=1,sharex=True)
        axa.set_title('TIMING DIAGRAM')
        axa.plot(a,drawstyle='steps-pre',color='lime')
        axa.set_ylabel('A',rotation=0)
        axb.plot(b,drawstyle='steps-pre',color='lime')
        axb.set_ylabel('B',rotation=0)
        axc.plot(c,drawstyle='steps-pre',color='lime')
        axc.set_ylabel('C',rotation=0)
        axd.plot(d,drawstyle='steps-pre',color='lime')
        axd.set_ylabel('D',rotation=0)
        axx.plot(x,drawstyle='steps-pre',color='plum')
        axx.set_ylabel('X',rotation=0)
        axy.plot(y,drawstyle='steps-pre',color='plum')
        axy.set_ylabel('Y',rotation=0)
        axz.plot(z,drawstyle='steps-pre',color='red')
        axz.set_ylabel('Z',rotation=0)
        axz.set_xlabel('x axis')
#
#

    d=[]
#
#
    x=[]
    y=[]
    z=[]
    with open("hz.csv","r") as file:
        reader=csv.reader(file)
        next(file)
        for row in reader:
            d.append(row[0])
            c.append(row[1])
            b.append(row[2])
            a.append(row[3])
            z.append(row[6])
            y.append(row[7])
            x.append(row[8])
#
#
    if choice.value=='1' and choice_output.value=='Z':
        input1_outputz()
    if choice.value=='2' and choice_output.value=='Z':
        input2_outputz()
    if choice.value=='3' and choice_output.value=='Z':
        input3_outputz()
    if choice.value=='4' and choice_output.value=='Z':
        input4_outputz()
    if choice.value=='1' and choice_output.value=='Y-Z':
        input1_outputyz()
    if choice.value=='2' and choice_output.value=='Y-Z':
        input2_outputyz()
    if choice.value=='3' and choice_output.value=='Y-Z':
        input3_outputyz()
    if choice.value=='4' and choice_output.value=='Y-Z':
        input4_outputyz()
    if choice.value=='1' and choice_output.value=='X-Z':
        input1_outputxz()
    if choice.value=='2' and choice_output.value=='X-Z':
        input2_outputxz()
    if choice.value=='3' and choice_output.value=='X-Z':
        input3_outputxz()
    if choice.value=='4' and choice_output.value=='X-Z':
        input4_outputxz()
#
#
    plt.show()

app=App(title='LITAR LGOGIKA 2.0',width=800,height=480,layout="grid")
#
#
b_generate=PushButton (app,command=plot_graph,text="GENERATE GRAPH",grid=[2,5])
app.display()

1 Ответ

0 голосов
/ 13 апреля 2020

Вы можете попытаться использовать invert_yaxis(), чтобы перевернуть любой yaxis, как это:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [0, 1, 1, 0, 1]

plt.plot(x, y)
plt.gca().invert_yaxis()
plt.show()

Или вы можете попробовать вручную установить галочки y следующим образом:

plt.plot(x, y)
plt.yticks([0, 1])
plt.show()

Или Вы можете попытаться вручную установить пределы y следующим образом:

plt.plot(x, y)
plt.ylim(0, 1)
plt.show()

Если вы используете вспомогательные участки, любой из этих параметров можно использовать аналогичным образом. Возможно, вам не нужно использовать их все для одного сюжета, но вот как они могут быть применены:


x = [1, 2, 3, 4, 5]
y = [0, 1, 1, 0, 1]

fig, ax = plt.subplots()
ax.plot(x, y)

ax.invert_yaxis()
ax.set_yticks([0, 1])
ax.set_ylim(0, 1)

plt.show()
...