图表布局问题Matplolib python 3
基本上,我是基于存储在txt文件中的时间hh:MM:SS、x轴和浮点值y轴列表绘制一个图形,如下所示:图表布局问题Matplolib python 3,python,python-3.x,matplotlib,tkinter,Python,Python 3.x,Matplotlib,Tkinter,基本上,我是基于存储在txt文件中的时间hh:MM:SS、x轴和浮点值y轴列表绘制一个图形,如下所示: 15 52 27 0.00 15 52 37 0.2 15 52 50 0.00 15 53 12 2.55 15 54 21 10.00 15 55 15 13.55 我想将最后一个浮点值绘制为注释文本标签,与最后一个可用时间相对应。使用上面的txt,我想绘制13.55ml,对应于点[15,55,13.55] 下面是绘制我的图表的代码: datefunc = lambda x: mdate
15 52 27 0.00
15 52 37 0.2
15 52 50 0.00
15 53 12 2.55
15 54 21 10.00
15 55 15 13.55
我想将最后一个浮点值绘制为注释文本标签,与最后一个可用时间相对应。使用上面的txt,我想绘制13.55ml,对应于点[15,55,13.55]
下面是绘制我的图表的代码:
datefunc = lambda x: mdates.date2num(datetime.strptime(x.decode("utf-8"), '%H %M %S'))
dates, levels = np.genfromtxt('sensor1Text.txt', # Data to be read
delimiter=8, # First column is 8 characters wide
converters={0: datefunc}, # Formatting of column 0
dtype=float, # All values are floats
unpack=True) # Unpack to several variables
# Configure x-ticks
plot_fs1.set_xticks(dates) # Tickmark + label at every plotted point
plot_fs1.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
plot_fs1.set_ylabel('Fluid (mL)')
plot_fs1.grid(True)
# Format the x-axis for dates (label formatting, rotation)
fs1.autofmt_xdate(rotation= 45)
plot_fs1.plot_date(dates, levels, color='orange', ls='-', marker='o')
以下是我尝试在上次打印的值上打印注释标签的步骤:
lastxValue= len(dates)-1
lastyValue= len(levels)-1
lastValue = levels[lastyValue]
lastDate = dates[lastxValue]
plot_fs1.annotate(lastValue, (lastDate,
lastValue),xytext=(15, 15),textcoords='offset points')
fs1.tight_layout()
这就是我得到的:
注释未完全显示在打印窗口中,并且x轴值往往彼此重叠
有什么想法吗?一个选择是使用与之类似的方法,使用独立的、线性间隔的XTICK,并使用日期作为刻度的名称
这个问题使用条形图,但你可以使用秒数的差值,看看你经过了多少总时间,用记号间隔来覆盖整个时间,但步骤相同。你的时间差,有不同的间距,你只需要在你的点的绘图。通过适当的间距,XTICK变得更好。它还有助于为文本添加所需的额外空间。为了避免打印的每个点都有x轴条目,您可以使用定位器来标记图形上的每一分钟 其次,避免使用紧凑的布局,而是使用子地块调整,在需要的地方添加额外的间距。例如:
import numpy as np
import matplotlib
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
datefunc = lambda x: mdates.date2num(datetime.strptime(x.decode("utf-8"), '%H %M %S'))
dates, levels = np.genfromtxt('sensor1Text.txt', # Data to be read
delimiter=8, # First column is 8 characters wide
converters={0: datefunc}, # Formatting of column 0
dtype=float, # All values are floats
unpack=True) # Unpack to several variables
plot_fs1 = plt.gca()
fig = plt.gcf()
p = plt.plot(dates, levels)
plot_fs1.set_xticks(dates) # Tickmark + label at every plotted point
plot_fs1.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
plot_fs1.xaxis.set_major_locator(matplotlib.dates.MinuteLocator())
plot_fs1.set_ylabel('Fluid (mL)')
plot_fs1.grid(True)
fig.autofmt_xdate(rotation= 45)
plot_fs1.plot_date(dates, levels, color='orange', ls='-', marker='o')
lastxValue = len(dates)-1
lastyValue = len(levels)-1
lastValue = levels[lastyValue]
lastDate = dates[lastxValue]
plot_fs1.annotate("{} mL".format(lastValue), (lastDate, lastValue), xytext=(15, 15), textcoords='offset points')
fig.subplots_adjust(bottom=0.15, right=0.85) # Add space at bottom and right
plt.show()
这将为您提供一个图形:
我会尝试:改变图形大小,改变字体大小,避免紧凑的布局