pad negative numbers in legend box

This commit is contained in:
Yannik Schmidt
2021-02-26 12:03:43 +01:00
parent be4ac62ece
commit 571cb6fb2b
2 changed files with 39 additions and 23 deletions

View File

@@ -30,31 +30,39 @@ def __plot(tup, datapoints, path, date1=None, date2=None, forcePath=False):
ls = CFG("plot_line_style")
tup[FIGURE],tup[AXIS] = plt.subplots(1, 1)
# generate datapoints #
tupelsToIterate = []
for key in datapoints.keys():
g = datapoints[key]
print(key)
#### Check if we are supposed to plot something ####
if not g.plot:
continue
#### GET AND CHECK TIMEFRAMES ####
x,y, = g.get_timeframe(tup[CALLBACK],date1,date2)
if not x or not y or len(x) <= 0 or len(y) <= 0:
print("Warning: Empty series of data '%s' (wrong start/end time?)"%g.name)
continue
else:
NO_SERIES = False
unix_x = [ el.timestamp() for el in x]
ymin,ymax = plot_graphutils.getlimits_y(y)
g = datapoints[key]
if not g.plot:
continue
x,y, = g.get_timeframe(tup[CALLBACK],date1,date2)
tupelsToIterate += [(x, y, g)]
#### GET LINE STYLES ####
legend_label = plot_graphutils.legend_box_contents(g.name,y)
tup[AXIS].plot(unix_x, y,ls=ls,lw=lw,marker="None", label=legend_label, color=g.color)
# check for negative values (legend padding) #
anyValueNegative = False
for x, y, g in tupelsToIterate:
anyValueNegative = anyValueNegative or min(y) < 0
# plot #
for x, y, g in tupelsToIterate:
if not x or not y or len(x) <= 0 or len(y) <= 0:
print("Warning: Empty series of data '%s' (wrong start/end time?)"%g.name)
continue
else:
NO_SERIES = False
unix_x = [ el.timestamp() for el in x]
ymin,ymax = plot_graphutils.getlimits_y(y)
legend_label = plot_graphutils.legend_box_contents(g.name, y, anyValueNegative)
tup[AXIS].plot(unix_x, y, ls=ls, lw=lw, marker="None",
label=legend_label, color=g.color)
legacy_x_save = x
lagacy_y_save = y
if NO_SERIES:
print("Error: no data, nothing to plot. cannot continue. exit.")
sys.exit(1)
print("Error: no data, nothing to plot. cannot continue. exit.")
sys.exit(1)
## GRID ##
plot_graphutils.general_background_setup(tup, ymin, ymax, legacy_x_save)

View File

@@ -31,24 +31,32 @@ def getlimits_y(y):
return (ymin, ymax)
def legend_box_contents(name, y):
def legend_box_contents(name, y, anyValueNegative):
'''Return a string with the formate content of the legend/caption'''
padding = 4
if anyValueNegative:
padding = 5
minFormatString = " min: {:" + str(padding) + ".1f},"
maxFormatString = " max: {:" + str(padding) + ".1f},"
avgFormatString = " Mittelwert: {:" + str(padding) + ".1f},"
# capping values at 99 makes formating easier#
if CFG("cap_values_at_99"):
y = [ min( [el, 99.9] ) for el in y ]
# add minimum values if configured #
if CFG("show_min"):
name += " min: {:4.1f},".format(min(y))
name += minFormatString.format(min(y))
# add maximum values if configured #
if CFG("show_max"):
name += " max: {:4.1f},".format(max(y))
name += maxFormatString.format(max(y))
# show average if configured #
if CFG("show_avg"):
name += " Mittelwert: {:4.1f},".format(sum(y)/float(len(y)))
name += avgFormatString.format(sum(y)/float(len(y)))
return name.rstrip(",")
def general_background_setup(tup,ymin,ymax,x):