如何使用matplotlib.pyplot将y轴顶部的“低”设置为“高”以下

2024-04-18 02:41:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python编程新手,我正在尝试从逻辑门的输出创建一个时序图。我使用的是raspbian,数据保存在csv中。是否有代码翻转y轴,使底部为“0”,顶部为“1”?这是因为,如果我从“1”开始获取数据,则图形底部为“1”,顶部为“0”

这里我举一个例子,底部是“1”enter image description here

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

Tags: plotvaluestepsprecolorsetlimechoice
1条回答
网友
1楼 · 发布于 2024-04-18 02:41:13

您可以尝试使用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()

相关问题 更多 >