Showing posts with label plotting. Show all posts
Showing posts with label plotting. Show all posts

Saturday, May 30, 2015

Moire with Spyre (a Python web framework)

By Vasudev Ram




The image above is of Uppsala Cathedral, Sweden, with its spires.

From the Spyre site:

Spyre is a Web Application Framework for providing a simple user interface for Python data projects. It is by Adam Hajari

I had come across Spyre recently via this post by Ian Oszvald:

Data Science Deployed – Opening Keynote for PyConSE 2015. More about Ian Oszvald here.

So I installed Spyre with the command:
pip install dataspyre
which in turn installed NumPy, pandas and matplotlib, all of which went through okay, though I saw some compiler / linker error messages scroll by as some compilation of C modules was happening (as part of the installation).

Then I tried it out a little, this time just using one of the simple applications on the Spyre site, without any modifications, like I sometimes do.
from spyre import server

import matplotlib.pyplot as plt
import numpy as np

class SimpleSineApp(server.App):
    title = "Simple Sine App"
    inputs = [{ "input_type":"text",
                "variable_name":"freq",
                "value":5,
                "action_id":"sine_wave_plot"}]

    outputs = [{"output_type":"plot",
                "output_id":"sine_wave_plot",
                "on_page_load":True }]

    def getPlot(self, params):
        f = float(params['freq'])
        x = np.arange(0,2*np.pi,np.pi/150)
        y = np.sin(f*x)
        fig = plt.figure()
        splt1 = fig.add_subplot(1,1,1)
        splt1.plot(x,y)
        return fig

app = SimpleSineApp()
app.launch()
As you can see, the structure / model of the code for a Spyre app does not seem to be very similar to that of, say, a Flask or a Bottle app. But then, there is no reason that it should be.
Anyway, I ran this app with the command:

python simple_sine_example.py

and then browsed to:

http://127.0.0.1:8080/

where I then gave different values for the input parameter freq, to see what would happen. I found that running it with certain values of freq created interesting Moire patterns in the output; hence the title of this post.

Here are a few screenshots below that show those moire patterns:

With freq = 764:


With freq = 472:


With freq = 475:


With freq = 479:


Notice that in some cases, such as with the values of 472, 475 and 479 for freq, the small changes between those values results in significant changes in the output image. Not only that: for 472 and 479, the images are similar (look sine-wave-y), but for 475 (which is between the other two values), the image looks almost like straight vertical lines. (It isn't, of course; it's still a sine wave, but more compressed along the horizontal axis.) I've not analyzed the reason for this in detail; something to do with the periodicity of the sine function, is my rough guess, but I have seen similar phenomena when drawing other kinds of computer graphics on the screen; those also involved trigonometric functions such as sine and cosine.

Spyre away :)

P.S. In other news, I was recently profiled in PyDev of the Week.

- Vasudev Ram - Online Python training and programming

Dancing Bison Enterprises

Signup to hear about new products or services that I create.

Posts about Python  Posts about xtopdf

Contact Page

Wednesday, July 10, 2013

plot.ly now has an API


By Vasudev Ram

I had blogged a couple of times earlier about plot.ly, here:

plot.ly, fast plotting in the browser

and here:

Plot me some randomness

Saw today via Twitter that plot.ly now has an API: plot.ly API

The current Hacker News thread about plot.ly has co-founder cparmer participating. He says in a comment in that thread, that after they first released plot.ly, the top request was for an API. They have now released it, with support for Python, MATLAB, R and Arduino.

Took a look at the plot.ly API page and it looks interesting.

Excerpt from the page:

[ Send data to your Plotly account from your desktop environment through our APIs and view your plot at a unique URL. Available for Python, R, MATLAB, and Arduino. ]

You can install the plot.ly module for Python via pip (or other methods).

After that you can generate a plot.ly plot from code on your desktop via the API and then get back a link/URL to the plot on the plot.ly site.

An interesting feature is that you can sign up programmatically via the plot.ly API.

Here is an example program using the plot.ly API, only slightly modified from their docs:

# One-time stuff to import plotly, and sign up via the API.
import plotly

# Para 1
username='v.ram'
email='[email protected]'
response = plotly.signup(username, email)
api_key = response['api_key']
tmp_pw = response['tmp_pw']
print "api_key:", api_key
print "tmp_pw:", tmp_pw

# Stuff that can repeat/vary, to use plot.ly

# Para 2
py = plotly.plotly(username='v.ram', key='obscured')

x0 = ['giraffes', 'orangutans', 'monkeys']
y0 = [20, 14, 23]
data = {'x': x0, 'y': y0,
 'type': 'bar'}
response = py.plot([data])
url = response['url']
filename = response['filename']
print "url:", url
print "filename:", filename

And here is the output from running that program:
High five! You successfuly sent some data to your account on plotly. View your plot in your browser at https://plot.ly/~v.ram/0 or inside your plot.ly account where it is named 'plot from API'
url: https://plot.ly/~v.ram/0
filename: plot from API
I then pasted the link above in my browser and it showed the plot.

You will have to change the values in the above code for username and email, to values of your own, and then run the program.

Of course, the plot.ly API is new, but it looks useful and worth checking out.

Here is a screenshot of the plot generated by the above Python plot.ly code:




- Vasudev Ram - Dancing Bison Enterprises

Contact me

Wednesday, April 24, 2013

Plot me some randomness


By Vasudev Ram

In this recent post about plot.ly,

plot.ly, fast plotting in the browser,

I mentioned that plotting by pasting data worked, but plotting using Python had an issue, and that they said they were fixing it.

The plot.ly people got back to me saying that the Python issue is fixed.

I tried it out again with a couple of simple plotting examples involving random numbers. They worked.

Here are the examples:

from numpy import *
from random import randint

x = range(1, 10)
y = [ random.randint(1, 10) for i in x ]
plot(x, y)

from numpy import *
from random import randint

x = range(1, 20)
y = [ random.randint(1, 10) for i in x ]
plot(x, y)

(The only difference in the above two scripts is 10 vs 20.)

You can try out that code, or your own, in plot.ly.

Go to https://plot.ly/plot.

Click on the "tab" labeled "+" near the top left of the screen, then click "Script" from the drop-down menu.

Enter your script (no need to Save) and then click Run.

If your code has no errors, your plot will appear in a new tab (not a new browser tab, but a new "tab" within the plot.ly browser tab).

The plot may take 2 to 3 seconds to appear.

plot.ly is beta software, but looks useful. They have many more complex examples in their demos.


- Vasudev Ram - Dancing Bison Enterprises


Monday, April 8, 2013

EPD Free - Enthought Python Distribution, with scientific and other packages

By Vasudev Ram

The Enthought Free Python Distribution (EPD Free), is a free Python distribution from Enthought, which includes additional Python packages / modules for scientific computing and graphing / plotting, such as SciPy, matplotlib and more.

I downloaded and installed it, and tried out some of the matplotlib examples in the Examples directory of the installation.

They worked fine.

Only tried it out a little so far, but I've known about Enthought for some time now, and it looks like a useful Python distribution for the stated purposes. They also have higher versions, presumably for pay, with support, etc.

- Vasudev Ram - Dancing Bison Enterprises


Friday, March 29, 2013

plot.ly, fast plotting in the browser

By Vasudev Ram

I got to know about plot.ly via this PyCon blog post about Startup Row selections for PyCon US 2013.

Then I tried it out. plot.ly enables you to create plots in your browser. They support plotting data via entering it, importing it from a file, or programming the plot in Python - the Python method seems to be fairly simple, conceptually, at least for simple plots.

I first tried it out using the Python method, but it did not work. No error message, but not output was displayed. Then I tried it out using the method of copy-pasting some data into the screen, and that method worked; it instantly created a plot.

I wrote to them about the problem with the Python plotting method, and one of the founders, Jack, replied, saying they currently have some issue with that. They are working on it.

Once they solve the issue, it may be a useful way of plotting using Python in the browser.

About plot.ly

- Vasudev Ram - Dancing Bison Enterprises