Python matplotlib - 如何在x轴上绘制一条线?

时间:2018-01-01 08:13:51

标签: python matplotlib plot graph format

所以我正在开发一个新项目,我正在使用matplotlib来跟踪我的习惯。 y轴是我所有习惯的列表(底部是“保留者”,顶部是“UAS”(我的俱乐部会议))。 x轴是1 - 天的数字(例如2018年1月的1-31)。我试图在图表后面划一条线,如果没有完成任何习惯,它将隐藏在x轴下,并且会在任何特定的日子同样地达到多少习惯。我的问题是将行绘制在(0,x),或者使用正确的语法:plt.plot((0),(0)),实际上将其绘制在看似(1,1)或带有我的标签的位置( 1, “保持器”)。这会让人觉得至少有一种习惯已经完成。

在matplotlib的先前版本中,似乎没有与图形线直到轴的争吵,但现在应用了自动“缓冲”空间,因此您不能再这样做了。

我不想改变那个缓冲区空间,我只是想在没有习惯完成的情况下在x轴上绘制我的线条,这样就不会显示。

目前的习惯只是测试数据,我提到它,否则它看起来有点奇怪。

这是一张照片,然后是我的代码:

picture of plot, graph line is scale with completed habits (red), but is one element too high

和我的代码:

import matplotlib.pyplot as plt
import calendar
import datetime
import atexit

def main():
    global trackers
    activities = ["Retainer","Steps Goal","Drink Water","6+ Hours Sleep","<=2 Sodas","Study","Leave Campus","Clean","Mike","Paint","Phone Call","Saw Friends","UAS"]
    trackers = []
    completed = []
    days_in_month = calendar.monthrange(int(str(datetime.datetime.now())[0:4]),int(str(datetime.datetime.now())[5:7]))[1]
    current_day = int(str(datetime.datetime.now())[8:11])
    file = open("trackers.txt","r")
    data = file.readlines()
    for i in range(len(data)):
        data[i] = data[i].rstrip("\n").rstrip("").rstrip(",")
        trackers.append(data[i].split(","))
        for b in range(len(trackers[-1])):
            trackers[-1][b] = int(trackers[-1][b])
    print("Trackers Loaded")

    plt.switch_backend('TkAgg')

    for i in range(days_in_month):
        completed.append(0)
        for b in range(len(trackers)):
            completed[-1] += trackers[b][i]

    plt.plot(tuple([i for i in range(days_in_month)]), tuple(completed),c = "k")

    plt.axvline(x=current_day - 1, c="#ccf3ff", linewidth = 30, zorder = -1)
    plt.grid()

    plt.title("Habit Tracker")
    plt.yticks([i for i in range(len(trackers))],tuple(activities))
    plt.xticks([i for i in range(days_in_month)],[i for i in range(1,days_in_month + 1)])
    for i in range(len(trackers)):
        for b in range(days_in_month):
            if trackers[i][b] == 1:
                plt.plot(b,i,"rs", markersize = 8)
            else:
                plt.plot(b,i,"s", color = "#d2d2d2", markersize = 8)

    mng = plt.get_current_fig_manager()
    mng.window.state('zoomed')

    plt.show()

    return

@atexit.register
def exit():
    file = open("trackers.txt","w")
    for i in range(len(trackers)):
        for b in range(len(trackers[i])):
            file.write(str(trackers[i][b]) + ",")
        file.write("\n")
    print("Trackers Saved")

if __name__ == "__main__":
    main()

如果你想运行它,你需要附带的文件“trackers.txt”,它可以在程序运行之间保存所有习惯数据,这里是我文件的直接副本。只需复制并粘贴此内容即可避免读取错误。 请务必在此文件的末尾添加空行!

0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,

提前谢谢!

1 个答案:

答案 0 :(得分:0)

我认为x轴不是你想象的那样。在此行中,您可以设置y刻度:

plt.yticks([i for i in range(len(trackers))],tuple(activities))

range从0开始,因此您的第一个活动标签标记为y = 0。也就是说,&#34; retainer&#34; 的x轴。

如果您希望第一个标签位于高度1,那么您应该在刻度线上添加一个标签:

plt.yticks([i+1 for i in range(len(trackers))],tuple(activities))

或者更简单:

plt.yticks(range(1, len(trackers)+1), activities)

(顺便说一句,你的代码中存在许多低效率和笨拙的结构。你不需要在所有内容上调用tuple。你可能最好迭代la {{1}而不是for item in trackers。)