numerik1/plot.py

97 lines
2.5 KiB
Python

import sys
import matplotlib.pyplot as plt
from collections import deque
#
# Copyright(c) 2018 Daniel Knüttel
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Dieses Programm ist Freie Software: Sie können es unter den Bedingungen
# der GNU General Public License, wie von der Free Software Foundation,
# Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
# veröffentlichten Version, weiterverbreiten und/oder modifizieren.
#
# Dieses Programm wird in der Hoffnung, dass es nützlich sein wird, aber
# OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
# Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
# Siehe die GNU General Public License für weitere Details.
#
# Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
# Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
"""
This script reads ``"x,y"`` values as lines from stdin and plots them.
If a line ``"XXX"`` is received the script will start a new plot.
Example::
./main | python3 plot.py
"""
datax = deque()
datay = deque()
cnt = 0
dpoints = 0
error_occured = False
for line in sys.stdin:
if(line == "XXX\n"):
if(not datax):
continue
plt.clf()
plt.plot(datax, datay)
plt.xlabel("N")
plt.ylabel("Error")
plt.savefig("{}.png".format(cnt))
cnt += 1
dpoints += len(datax)
datax = deque()
datay = deque()
continue
try:
x, y = line.split(",")
x = float(x)
y = float(y)
datax.append(x)
datay.append(y)
except Exception as e:
print(line)
print(e)
error_occured = True
break
if(datax):
plt.clf()
plt.plot(datax, datay)
plt.xlabel("N")
plt.ylabel("Error")
plt.savefig("{}.png".format(cnt))
cnt += 1
dpoints += len(datax)
if(error_occured):
print("Received malformed data and aborted early.")
print("Created", cnt, "plots containing", dpoints, "points in total.")