简体   繁体   中英

Standard ASCII File Format For Plotting from Matplotlib

I am working on a large hardware testing project, and one of the things I've run into is that I'd like to plot data coming out of the Python scripts on the systems doing the testing, but they aren't stout enough to run something like matplotlib. I started to do was to define a format to pass data back to the server that logs all the data coming from test systems; however, I would prefer to use something off the shelf if it exists for writing plots in a standardized way to an ASCII file and reading them back some where else and plotting.

I am partial to plotting in matplotlib, but I have some flexibility as to what I can use. I don't have any problem defining a custom format, I would just like to use something standard if matplotlib, (or any other full featured tool) has a way to build plots on the fly from some text format. Thanks in advance for your help.

Edit: To be more specific, transferring data is pretty straighforward; I like using JSON for that. What I am kind of looking for, is to build a file that not only holds data but also how to plot the data, (ie the script/application loads it up and builds a plot from it, the file specifies title/xlabel/ylabel, maybe that it needs a legend, etc.) Writing data via CSV or JSON isn't hard; it's finding a way to send plotting instructions to a host in a standardized way that might be.

Well, you could write python code to a file, and send that to the server to be executed, but that could be a security risk.

To mitigate some of the security risk (note XML vulnerabilities ), you could use xmlrpclib to restrict the set of functions that can be called from the source machine to be run on the server:

For example, you might run on the server:

from SimpleXMLRPCServer import SimpleXMLRPCServer
import matplotlib.pyplot as plt

def plot(filename, x, y):
    plt.plot(x, y)
    plt.savefig(filename)    
    return True

server = SimpleXMLRPCServer(("localhost", 8000))
server.register_function(plot, 'plot')
server.serve_forever()

And on the source machine:

import xmlrpclib
proxy = xmlrpclib.ServerProxy("http://localhost:8000/")

x = range(100)
y = [i**2 for i in x]
proxy.plot('/tmp/test.png', x, y)

You could add more parameters to the plot function to handle the title, legend, etc.

Running the code above on the source machine will generate the file /tmp/test.png on the server.

You might consider using Matlab's text format for transfering data. Scipy has a set of Matlab IO routines that you could use. You could also do it with plain Python by dumping data in JSON format or using pickle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM