Python matplotlib - 如何在x轴上绘制一条线?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python matplotlib - 如何在x轴上绘制一条线?相关的知识,希望对你有一定的参考价值。
所以我正在开发一个新项目,我正在使用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("
").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("
")
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,
先感谢您!
我认为x轴不是你想象的那样。在此行中,您可以设置y刻度:
plt.yticks([i for i in range(len(trackers))],tuple(activities))
range
从0开始,因此您的第一个活动标签标记为y = 0。也就是说,表示“保持器”的线是x轴。
如果您希望您的第一个标签位于高度1,那么您应该在您的标记中添加一个标签:
plt.yticks([i+1 for i in range(len(trackers))],tuple(activities))
或者,更简单:
plt.yticks(range(1, len(trackers)+1), activities)
(顺便说一句,你的代码中有许多低效率和笨拙的结构。你不需要在所有内容上调用tuple
。你可能最好迭代la for item in trackers
而不是for i in range(len(trackers))
。)
以上是关于Python matplotlib - 如何在x轴上绘制一条线?的主要内容,如果未能解决你的问题,请参考以下文章
在python上使用matplotlib挤压x轴图形比例[重复]
python使用matplotlib可视化为X轴设置轴标签位置及其对应的标签文本(set label locations and corresponding labels in matplotlib
python使用matplotlib可视化为可视化图像的X轴和Y轴设置自定义的轴标签(axis labels of matplotlib plot)