简体   繁体   English

如何使用 matplotlib.pyplot 在顶部 y 轴的高点以下设置低点

[英]How to set low at below high at top y axis with matplotlib.pyplot

I'm new to the python programming and I'm trying to create a timing diagram from the output of a logic gate.我是 python 编程的新手,我正在尝试从逻辑门的 output 创建时序图。 I'm using raspbian and the data is saved in csv.我正在使用 raspbian,数据保存在 csv 中。 Any code to flip the y axis to make '0' on bottom and '1' on top?有什么代码可以翻转 y 轴以使底部为“0”而顶部为“1”? It's because if I get the data starting from '1', the graph will be '1' at bottom and '0' at top.这是因为如果我从“1”开始获取数据,则图表将在底部为“1”,在顶部为“0”。

Here I give the example that '1' at the bottom.这里我举个例子,底部是“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()

You can try to use invert_yaxis() to flip any yaxis like this:您可以尝试使用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()

Or you can try to manually set the y ticks like this:或者您可以尝试手动设置 y 刻度,如下所示:

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

Or you can try to manually set the y limits like this:或者您可以尝试手动设置 y 限制,如下所示:

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

If you are using subplots, any of these options can be used in a similar way.如果您使用的是子图,则可以以类似的方式使用这些选项中的任何一个。 You probably don't need to use them all for the same plot, but this is how they could be applied:您可能不需要将它们全部用于相同的 plot,但这就是它们的应用方式:


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

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

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