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

Friday, June 22, 2012

Asymptote Vector Graphics Package


Asymptote

I've seen this before in my Ubuntu repository when searching for LaTeX packages.  I want to try this one day.  Asymptote is a vector graphics software, except it is all through its own programming language.  At first, it seems  scary, but in reality, it is the same thing as making a plot in Python, MATLAB, Mathematice, Sage, Maxima, etc.  Asymptote seems nice since it seems very streamlined like LaTeX is for documents.
Asymptote is a powerful descriptive vector graphics language that provides a natural coordinate-based framework for technical drawing. Labels and equations are typeset with LaTeX, for high-quality PostScript output.

Thursday, June 21, 2012

Importing data into Python for a 2D plot

Yay!  I'm learning some Python! (For Family Guy fans this is in Peter's voice when Lois lets him take his cage of parrots on a trip and he says, Yay, you're letting me be myself!).

So I needed to load a small sample of data (about 7 data points) into a 2D graph in Python.  After awhile of searching and trial and error, I finally found a solution.

First, I had to figure out how to get Python to read the data and read it in a proper format or format that I needed for use.  Basically, a 2x2 array for x and y values.

At first I thought plotfile under matplolib under cbook and using fname and get_sample_data would work.

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plotfile

http://matplotlib.sourceforge.net/examples/pylab_examples/plotfile_demo.html

http://matplotlib.sourceforge.net/mpl_examples/pylab_examples/plotfile_demo.py

http://matplotlib.sourceforge.net/api/cbook_api.html?highlight=fname

I also tried the csv package/module/library or whatever the proper term is in Python and its reader function.

http://docs.python.org/release/2.5.2/lib/csv-examples.html

http://www.endlesslycurious.com/2011/05/06/graphing-real-data-with-matplotlib/

http://docs.python.org/library/csv.html#csv.reader

http://docs.python.org/library/csv.html

http://www.doughellmann.com/PyMOTW/csv/

I did get this to display my data in a table
import csv
reader = csv.reader(open("some.csv", "rb"))
for row in reader:
    print row
Here is a screenshot:


However, I found loadtxt and upack=True but ended up using NumPy's genfromtxt which seems like they accomplish essentialyl the same thing, to load my data into a 2x2 matrix or array.

http://bulldog2.redlands.edu/facultyfolder/deweerd/tutorials/Tutorial-ReadingWritingData.pdf

http://www.programmingforbiologists.org/importing-data-python

http://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html

CSV stands for Comma Separated Value

http://docs.python.org/library/csv.html
The so-called CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases. There is no “CSV standard”, so the format is operationally defined by the many applications which read and write it. The lack of a standard means that subtle differences often exist in the data produced and consumed by different applications. These differences can make it annoying to process CSV files from multiple sources. Still, while the delimiters and quoting characters vary, the overall format is similar enough that it is possible to write a single module which can efficiently manipulate such data, hiding the details of reading and writing the data from the programmer. The csv module implements classes to read and write tabular data in CSV format. It allows programmers to say, “write this data in the format preferred by Excel,” or “read data from this file which was generated by Excel,” without knowing the precise details of the CSV format used by Excel. Programmers can also describe the CSV formats understood by other applications or define their own special-purpose CSV formats.
My code is:

import numpy as np
import matplotlib.pyplot as plt

x, y = np.genfromtxt('1952_Kelsall_ax_vel_ser_I_first_z_loc_closeup_2.csv', delimiter = ',', unpack=True)
y = np.multiply(1.62, y)
y = np.divide(y, 2885)

plt.plot(x, y, 'o')

plt.show()

So I imported my data into x and y using unpack=True to ensure that the data went into its own column. I then did some scaling using NumPy's multiply and divide. Then simple plotted onto a 2D graph using 'o' for data points only.

Screenshot:


http://matplotlib.sourceforge.net/examples/api/unicode_minus.html

http://matplotlib.sourceforge.net/mpl_examples/api/unicode_minus.py

http://docs.scipy.org/doc/numpy/reference/generated/numpy.divide.html

http://docs.scipy.org/doc/numpy/reference/generated/numpy.multiply.html

http://www.scipy.org/NumPy_for_Matlab_Users


Here is a NumPy tutorial and reference:

Tentative NumPy Tutorialhttp://www.scipy.org/Tentative_NumPy_Tutorial

Numpy Example Listhttp://www.scipy.org/Numpy_Example_List

Monday, April 16, 2012

Contour plotting (2D) - Sage - Streamlines

So, I am getting more familiar with other scientific software to use for my research.  One I've been trying is Sage, and I've made a few post about it.  I like it so far.  Here is an example of a contour plot I've done recently for some streamlines and some effects.

http://www.sagemath.org/doc/reference/sage/plot/contour_plot.html

kap_pa = 0.00621
r,z = var('r,z')
psee1 = kap_pa*z*sin(pi*r^2)
contour_plot(psee1, (r, 0, 1), (z, 0, 1), contours = 20, fill=False, axes_labels=['r','z'], aspect_ratio=1)


kap_pa = 0.00621
r,z = var('r,z')
psee1 = kap_pa*z*sin(pi*r^2)
contour_plot(psee1, (r, 0, 1), (z, 0, 2), contours = 20, fill=False, axes_labels=['r','z'], aspect_ratio=2)


kap_pa = 0.00621
r,z = var('r,z')
psee1 = kap_pa*z*sin(pi*r^2)
contour_plot(psee1, (r, 0, 1), (z, 0, 1), contours = 20, fill=True, axes_labels=['r','z'])

kap_pa = 0.00621
r,z = var('r,z')
psee1 = kap_pa*z*sin(pi*r^2)
contour_plot(psee1, (r, 0, 1), (z, 0, 2), contours = 20, fill=True, axes_labels=['r','z'], aspect_ratio=2)

kap_pa = 0.00311
r,z = var('r,z')
psee1 = kap_pa*z*sin(pi*r^2)
contour_plot(psee1, (r, 0, 1), (z, 0, 2), contours = 20, fill=True, axes_labels=['r','z'], aspect_ratio=2)

kap_pa = 0.00311
r,z = var('r,z')
psee1 = kap_pa*z*sin(pi*r^2)
contour_plot(psee1, (r, 0, 1), (z, 0, 1), contours = 20, fill=False, linestyles='dashdot', axes_labels=['r','z'])

kap_pa = 0.00621
r,z = var('r,z')
psee1 = kap_pa*z*sin(pi*r^2)
contour_plot(psee1, (r, 0, 1), (z, 0, 1), contours = 20, fill=False, linewidths=[1,5], linestyles=['solid','dashed'], axes_labels=['r','z'])

kap_pa = 0.00621
r,z = var('r,z')
psee1 = kap_pa*z*sin(pi*r^2)
contour_plot(psee1, (r, 0, 1), (z, 0, 1), contours = 20, cmap='hsv', axes_labels=['r','z'])

kap_pa = 0.00621
r,z = var('r,z')
psee1 = kap_pa*z*sin(pi*r^2)
contour_plot(psee1, (r, 0, 1), (z, 0, 1), contours = 20, cmap=[(1,0,0), (0,1,0), (0,0,1)], axes_labels=['r','z'])

kap_pa = 0.00621
r,z = var('r,z')
psee1 = kap_pa*z*sin(pi*r^2)
contour_plot(psee1, (r, 0, 1), (z, 0, 1), contours = 20, cmap='hsv', labels=True, axes_labels=['r','z'])

kap_pa = 0.00621
r,z = var('r,z')
psee1 = kap_pa*z*sin(pi*r^2)
contour_plot(psee1, (r, 0, 1), (z, 0, 1), contours = 20, cmap='hsv', labels=True, label_fmt="%1.4f", label_colors='black', axes_labels=['r','z'])

kap_pa = 0.00621
r,z = var('r,z')
psee1 = kap_pa*z*sin(pi*r^2)
contour_plot(psee1, (r, 0, 1), (z, 0, 1), contours = 20, cmap='hsv', labels=True, label_fmt="%1.4f", label_colors='black', label_fontsize=14, axes_labels=['r','z'])

kap_pa = 0.00621
r,z = var('r,z')
psee1 = kap_pa*z*sin(pi*r^2)
contour_plot(psee1, (r, 0, 1), (z, 0, 1), contours = 20, cmap='hsv', colorbar=True, labels=True, label_fmt="%1.4f", label_colors='black', label_fontsize=10, axes_labels=['r','z'])

Tuesday, January 31, 2012

matplotlib: python plotting

http://matplotlib.sourceforge.net/
matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. matplotlib can be used in python scripts, the python and ipython shell (ala MATLAB®* or Mathematica®†), web application servers, and six graphical user interface toolkits. matplotlib tries to make easy things easy and hard things possible. You can generate plots, histograms, power spectra, bar charts, errorcharts, scatterplots, etc, with just a few lines of code. For a sampling, see the screenshots, thumbnail gallery, and examples directory

Sage - 2D plot - simple plot, more than one function on the same plot, axes limits, change color of function in graph

There are many options when plotting in Sage.  Here is a simple example.

alpha = 30*pi/180
show(alpha)
show(float(alpha))
f = (arcsin(alpha))^2 + log(tan(alpha/2)) - arcsin(alpha)*arctan(alpha)
show(f)
show(float(f))
g = (1/(0.7*sin(x)))*sqrt(1 + ((pi)^(-2))*((0.7*sin(x))^2)*(f - csc(x)*tan(x/2) - log(tan(x/2))))
p = plot(g, (0, alpha))
show(p, ymin = 0, ymax = 20)

In Sage, the variable x is automatically known/defined.  I then create a simple function similar to how I created alpha and f.  I next equal p to the plot since I wanted to change the y axis range manually which you can do through the show command (or this can also be done in the plot command as well).



Here is another example of a 2D plot.  This one shows how to plot a couple of functions.

alpha = 30*pi/180
show(alpha)
show(float(alpha))
z = 0.5
r_max = z*tan(alpha)
show(r_max)
show(float(r_max))
lamb_da = (csc(alpha))^2 - (cot(alpha))*(csc(alpha)) + log(tan(alpha/2))
show(float(lamb_da))
cig_ma_c = 1
r = var('r')

psee = (1/2*pi)*(cig_ma_c)*(r^2)*(lamb_da + (z/r)*sqrt(1 + (z/r)^2) - log(sqrt(1 + (z/r)^2) - z/r) - 1 - (z/r)^2)

lamb_da_bi = (csc(alpha))^2 - (cot(alpha))*(csc(alpha)) + log(tan(alpha)/2)

show(float(lamb_da_bi))

psee_bi = (1/2*pi)*(cig_ma_c)*(r^2)*(lamb_da_bi + (z/r)*sqrt(1 + (z/r)^2) - log(r/(2*z)) - 1 - (z/r)^2)

p1 = plot(psee, (r, 0, r_max))

p2 = plot(psee_bi, (r, 0, r_max), color ='green')

show(p1 + p2)



Sunday, January 29, 2012

Maxima and wxMaxima in Windows 7

I am forced to use Windows every now and then, but I have recently found out that some of my favorite software works for Windows and is relatively easy to install.  Maxima is very easy, as you just download and run the Windows installer from here:  http://www.windows7download.com/win7-maxima/kqcjkmcz.html

Installing Maxima for Windows also installs wxMaxima (and Xmaxima if wanted) and gnuplot.  Here is a screenshot.


Friday, December 30, 2011

Moving the legend location in Maxima/Gnuplot

I had a plot where the function graph intersected the legend at its default position in the upper right hand corner.  I finally found a command which will move it to the lower right instead.  I'm sure you can move it to where you like, but this is the only one I found so far.  The command is:

[gnuplot_preamble,"set key bottom"]


For example,

plot2d([legendre_p(0, x), legendre_p(1, x), legendre_p(2, x),legendre_p(3, x), legendre_p(4, x), legendre_p(5, x)], [x, 0, 2], [y, -2, 5], [legend,"P_0 (x) = 1","P_1 (x) = x","P_2 (x) = (1/2)*(3*x^2 - 1)", "P_3 (x) = (1/2)*x*(5*x^2 - 3)","P_4 (x) = (1/8)*(35*x^4 - 30*x^2 + 3)","P_5 (x) = (1/8)*x*(63*x^4 - 70*x^2 + 15)" ], [ylabel,"P_n (x)"], [gnuplot_preamble,"set key bottom"]);


Found this here: http://maxima.sourceforge.net/maxima-gnuplot.html

Editing axis and legend labels in Maxima/Gnuplot

The commands for editing the labels for the x and y axes in Maxima are:

[xlabel,"expr"]

and

[ylabel,"expr"]

where expr is the argument holder where you place your text for whatever label you are wanting.  The quotes " " are necessary.

The command for the legend label is:

[legend,"expr1","expr2"]

where expr1, expr2, etc. are the argument holders where you place your text for whatever labels you are wanting depending on the number of legend labels.

These commands go inside your overall plot command.  For example,

plot2d(func, x, [xlabel,"expr"], [ylabel,"expr"], [legend,"expr1","expr2"])

Found these commands here: http://maxima.sourceforge.net/docs/manual/en/maxima_12.html#SEC65

Saturday, November 26, 2011

sin(x) + cos(x)


\( \sin x + \cos x \)

Code for Maxima:

f(x) := sin(x) + cos(x)
plot2d(f(x), [x, 0, 7], [y, -2, 2]);


sin(x) - cos(x)


\( \sin x - \cos x \)

Code for Maxima:

f(x) := sin(x) - cos(x)
plot2d(f(x), [x, 0, 7], [y, -2, 2]);


Friday, November 25, 2011

tan(x)


\( \tan x \)

Code for Maxima:

plot2d(tan(x), [x, 0, 7], [y, -2, 2]);


sin(x) + cos(x)


\( \sin x + \cos x \)

Code for Maxima:

plot2d(sin(x)+cos(x), [x, 0, 7]);


Bessel function of the second kind, orders 0, 1, 2



\( Y_0 \left( x \right) \)

Code to plot in wxMaxima:
plot2d(bessel_y (0, x), [x, 0, 20], [y, -2, 1]);


\( Y_1 \left( x \right) \)

Code to plot in wxMaxima:
plot2d(bessel_y (1, x), [x, 0, 20], [y, -2, 1]);


\( Y_2 \left( x \right) \)

Code to plot in wxMaxima:
plot2d(bessel_y (2, x), [x, 0, 20], [y, -2, 1]);


\( Y_0 \left( x \right), \quad Y_1 \left( x \right), \quad Y_2 \left( x \right) \)

Code to plot in wxMaxima:
plot2d([bessel_y (0, x), bessel_y (1, x), bessel_y (2, x)], [x, 0, 20]);



Bessel functions of the first kind, orders 0, 1, 2

Since Lybniz only recognizes the python standard math library, I used wxMaxima to plot the Bessel functions.

\( J_0 \left( x \right) \)

Code to plot in wxMaxima:
plot2d(bessel_j (0, x), [x, 0, 20]);



\( J_1 \left( x \right) \)

Code to plot in wxMaxima:
plot2d(bessel_j (1, x), [x, 0, 20]);


\( J_2 \left( x \right) \)

Code to plot in wxMaxima:
plot2d(bessel_j (1, x), [x, 0, 20]);


\( J_0 \left( x \right), \quad J_1 \left( x \right), \quad J_2 \left( x \right) \)

Code to plot in wxMaxima:
plot2d([bessel_j (0, x), bessel_j (1, x), bessel_j (2, x)], [x, 0, 20]);


Tuesday, November 22, 2011

Lybniz vs KAlgebra for graph visualization/plotting

In my work I sometimes need to quickly visualize a simple function, and software like Mathematica and Maxima seem to be a bit of overkill so I decided to try some simple plotting programs in Ubuntu from the SC.

The first, which I have evaluated before, is KAlgebra.  It's not too bad and has an upside in that it can also do calculations.  However, I don't really like the plot style.   This is an example for \( \sin^2 x \cos^2 x \).


So I typed in "graph" or "plot" in the SC and cam across the Lybniz graph plotting software.  It only plots  so it doesn't calculate like KAlgebra can and then show you a graph.  I like how Lybniz looks better than KAlgebra.  It doesn't have many options.  I wish you could thicken the line a bit, but it does let you zoom in better than KAlgebra.  Same example.