使用python绘制mathematica数据 [英] Plotting mathematica data with python

查看:127
本文介绍了使用python绘制mathematica数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与我以前在"python中的出版质量图"上的问题有关.

我正在尝试编写一个后处理程序(bash脚本/python组合),该处理程序将梳理我拥有的数千个(字面意义上的)数据文件并绘制数据.麻烦的是,我的数据文件来自mathematica.

作为对非线性偏微分方程的NDSolve运算的结果,我在Mathematica(mma)中具有插值函数多项式.我能够从mma多项式中解构/提取离散数据.

以mma绘制时的数据将生成如下3D轮廓图:

现在这是我感兴趣的某个时间步长的轮廓图.它由我提取的X和Y坐标值(此文章末尾包含的数据)组成,绘制时如下图所示:

如何使用python绘制此X,Y数据?换句话说,最好的方法是什么?

我已经尝试了以下用于表面图和线框图的python代码,但是它们都需要一个我没有的Z坐标.

编辑

关于此特定3D轮廓图的完整数据是此处.我正在尝试弄清楚现在如何将其排列在行和列中...

线框图示例:

from numpy import *
from numpy.random import rand
from pylab import pcolor, show, colorbar, xticks, yticks
from pylab import *
from mpl_toolkits.mplot3d import axes3d
from matplotlib import *
#from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
ax.plot_wireframe(X, Y,Z, rstride=10, cstride=10, cmap="binary")

plt.show()

表面图

from numpy import *
from numpy.random import rand
from pylab import pcolor, show, colorbar, xticks, yticks
from pylab import *
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import *
#from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
surf = ax.plot_surface(X,Y,Z, rstride=1, cstride=1, cmap="binary",
        linewidth=0, antialiased=False)
#ax.set_zlim(-1.01, 1.01)

#ax.zaxis.set_major_locator(LinearLocator(10))
#ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

X,Y数据:

X:

{0.132737, 0.13191, 0.129746, 0.127106, 0.125117, 0.12483, 0.126976, \
0.131958, 0.140068, 0.151781, 0.167899, 0.189295, 0.216319, 0.248189, \
0.282795, 0.317034, 0.347415, 0.370662, 0.384156, 0.386236, 0.376402, \
0.355467, 0.325648, 0.290526, 0.254685, 0.222845, 0.198549, 0.183281, \
0.177335, 0.182392, 0.203451, 0.246983, 0.315549, 0.405471, 0.509765, \
0.621364, 0.73459, 0.845401, 0.951138, 1.05018, 1.14164, 1.22514, \
1.30063, 1.3683, 1.42847, 1.48156, 1.52802, 1.56836, 1.60306, \
1.63263, 1.65754, 1.67825, 1.69521, 1.70884, 1.71951, 1.72761, \
1.73347, 1.7374, 1.73971, 1.74066, 1.74049, 1.73944, 1.7377, 1.73545, \
1.73286, 1.73009, 1.72724, 1.72445, 1.7218, 1.71938, 1.71726, 1.7155, \
1.71414, 1.71322, 1.71275, 1.71275, 1.71322, 1.71415, 1.71551, \
1.71727, 1.71939, 1.72181, 1.72446, 1.72726, 1.7301, 1.73288, \
1.73547, 1.73772, 1.73947, 1.74052, 1.74069, 1.73974, 1.73744, \
1.7335, 1.72765, 1.71955, 1.70888, 1.69525, 1.67829, 1.65757, \
1.63266, 1.60309, 1.56838, 1.52803, 1.48156, 1.42846, 1.36828, \
1.30059, 1.22507, 1.14155, 1.05005, 0.950968, 0.845186, 0.734326, \
0.621052, 0.509426, 0.405163, 0.31538, 0.247033, 0.203632, 0.18257, \
0.177457, 0.183338, 0.198563, 0.222852, 0.254712, 0.290571, 0.325696, \
0.355504, 0.376422, 0.386237, 0.38414, 0.370635, 0.347385, 0.317008, \
0.282778, 0.24818, 0.216314, 0.189287, 0.167886, 0.151767, 0.140056, \
0.131952, 0.126973, 0.124829, 0.125116, 0.127103, 0.129743, 0.131907, \
0.132737}

Y:

{0.132737, 0.160814, 0.248665, 0.386715, 0.554857, 0.736199, \
0.919228, 1.09676, 1.26441, 1.41958, 1.56072, 1.68695, 1.79781, \
1.89307, 1.97268, 2.03665, 2.08504, 2.11789, 2.13527, 2.1372, \
2.12369, 2.0947, 2.05018, 1.99008, 1.91432, 1.82288, 1.71579, 1.5932, \
1.45551, 1.30345, 1.13838, 0.962571, 0.779892, 0.596757, 0.423678, \
0.276942, 0.177388, 0.136833, 0.13817, 0.160097, 0.190941, 0.222093, \
0.247023, 0.261917, 0.265986, 0.261225, 0.251781, 0.243058, 0.240757, \
0.250172, 0.275704, 0.320022, 0.382994, 0.461711, 0.551722, 0.648374, \
0.747602, 0.846211, 0.941863, 1.03295, 1.11842, 1.19768, 1.27043, \
1.33662, 1.39633, 1.4498, 1.49731, 1.53921, 1.57586, 1.60765, \
1.63497, 1.65821, 1.67774, 1.69392, 1.70711, 1.71765, 1.72583, \
1.73197, 1.73635, 1.73922, 1.74084, 1.74142, 1.74119, 1.74032, 1.739, \
1.73739, 1.73562, 1.73382, 1.73211, 1.73057, 1.72928, 1.72831, \
1.72768, 1.72744, 1.72759, 1.72812, 1.72901, 1.73023, 1.73172, \
1.7334, 1.73518, 1.73697, 1.73864, 1.74005, 1.74105, 1.74146, \
1.74111, 1.73977, 1.73724, 1.73326, 1.72759, 1.71994, 1.71003, \
1.69753, 1.68213, 1.66347, 1.64119, 1.61492, 1.58428, 1.54888, \
1.50832, 1.46224, 1.41028, 1.35213, 1.28756, 1.21644, 1.13877, \
1.05479, 0.965005, 0.870346, 0.772266, 0.672914, 0.575269, 0.483202, \
0.401281, 0.334109, 0.285083, 0.255065, 0.242016, 0.241877, 0.249537, \
0.25934, 0.265716, 0.264102, 0.251884, 0.229106, 0.198813, 0.166855, \
0.141209, 0.132737}

解决方案

使用plotscatter方法绘制x,y数据.在您的情况下,x是时间,y是X或Y之一.要生成时间坐标,请使用"publication quality plots in python".

I am trying to write a post processor (a bash script/python combo) that would comb through the thousands (literally!) of data files I have and plot data. The trouble is, my data files are from mathematica.

I have interpolating function polynomials in Mathematica (mma) as a result of an NDSolve operation on a non-linear partial differential equation. I was able to deconstruct/extract discrete data out of my mma polynomials.

The data when plotted in mma results in 3D profile plots such as this:

Now this is a profile plot at a certain time step that I am interested in. It is constituted of X and Y coordinate values (data included at the end of this post) which I extracted and when plotted look like this:

How do I plot this X, Y data with python? In other words, what is the best way of going about it?

I have tried the following python code for surface plots and wireframe plots but they all need a Z coordinate which I don't have.

Edit

My full data for this particular 3D profile plot is here. I am trying to figure out how to arrange this in rows and columns now...

Example wireframe plot:

from numpy import *
from numpy.random import rand
from pylab import pcolor, show, colorbar, xticks, yticks
from pylab import *
from mpl_toolkits.mplot3d import axes3d
from matplotlib import *
#from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
ax.plot_wireframe(X, Y,Z, rstride=10, cstride=10, cmap="binary")

plt.show()

Surface plot

from numpy import *
from numpy.random import rand
from pylab import pcolor, show, colorbar, xticks, yticks
from pylab import *
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import *
#from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
surf = ax.plot_surface(X,Y,Z, rstride=1, cstride=1, cmap="binary",
        linewidth=0, antialiased=False)
#ax.set_zlim(-1.01, 1.01)

#ax.zaxis.set_major_locator(LinearLocator(10))
#ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

X, Y data:

X:

{0.132737, 0.13191, 0.129746, 0.127106, 0.125117, 0.12483, 0.126976, \
0.131958, 0.140068, 0.151781, 0.167899, 0.189295, 0.216319, 0.248189, \
0.282795, 0.317034, 0.347415, 0.370662, 0.384156, 0.386236, 0.376402, \
0.355467, 0.325648, 0.290526, 0.254685, 0.222845, 0.198549, 0.183281, \
0.177335, 0.182392, 0.203451, 0.246983, 0.315549, 0.405471, 0.509765, \
0.621364, 0.73459, 0.845401, 0.951138, 1.05018, 1.14164, 1.22514, \
1.30063, 1.3683, 1.42847, 1.48156, 1.52802, 1.56836, 1.60306, \
1.63263, 1.65754, 1.67825, 1.69521, 1.70884, 1.71951, 1.72761, \
1.73347, 1.7374, 1.73971, 1.74066, 1.74049, 1.73944, 1.7377, 1.73545, \
1.73286, 1.73009, 1.72724, 1.72445, 1.7218, 1.71938, 1.71726, 1.7155, \
1.71414, 1.71322, 1.71275, 1.71275, 1.71322, 1.71415, 1.71551, \
1.71727, 1.71939, 1.72181, 1.72446, 1.72726, 1.7301, 1.73288, \
1.73547, 1.73772, 1.73947, 1.74052, 1.74069, 1.73974, 1.73744, \
1.7335, 1.72765, 1.71955, 1.70888, 1.69525, 1.67829, 1.65757, \
1.63266, 1.60309, 1.56838, 1.52803, 1.48156, 1.42846, 1.36828, \
1.30059, 1.22507, 1.14155, 1.05005, 0.950968, 0.845186, 0.734326, \
0.621052, 0.509426, 0.405163, 0.31538, 0.247033, 0.203632, 0.18257, \
0.177457, 0.183338, 0.198563, 0.222852, 0.254712, 0.290571, 0.325696, \
0.355504, 0.376422, 0.386237, 0.38414, 0.370635, 0.347385, 0.317008, \
0.282778, 0.24818, 0.216314, 0.189287, 0.167886, 0.151767, 0.140056, \
0.131952, 0.126973, 0.124829, 0.125116, 0.127103, 0.129743, 0.131907, \
0.132737}

Y:

{0.132737, 0.160814, 0.248665, 0.386715, 0.554857, 0.736199, \
0.919228, 1.09676, 1.26441, 1.41958, 1.56072, 1.68695, 1.79781, \
1.89307, 1.97268, 2.03665, 2.08504, 2.11789, 2.13527, 2.1372, \
2.12369, 2.0947, 2.05018, 1.99008, 1.91432, 1.82288, 1.71579, 1.5932, \
1.45551, 1.30345, 1.13838, 0.962571, 0.779892, 0.596757, 0.423678, \
0.276942, 0.177388, 0.136833, 0.13817, 0.160097, 0.190941, 0.222093, \
0.247023, 0.261917, 0.265986, 0.261225, 0.251781, 0.243058, 0.240757, \
0.250172, 0.275704, 0.320022, 0.382994, 0.461711, 0.551722, 0.648374, \
0.747602, 0.846211, 0.941863, 1.03295, 1.11842, 1.19768, 1.27043, \
1.33662, 1.39633, 1.4498, 1.49731, 1.53921, 1.57586, 1.60765, \
1.63497, 1.65821, 1.67774, 1.69392, 1.70711, 1.71765, 1.72583, \
1.73197, 1.73635, 1.73922, 1.74084, 1.74142, 1.74119, 1.74032, 1.739, \
1.73739, 1.73562, 1.73382, 1.73211, 1.73057, 1.72928, 1.72831, \
1.72768, 1.72744, 1.72759, 1.72812, 1.72901, 1.73023, 1.73172, \
1.7334, 1.73518, 1.73697, 1.73864, 1.74005, 1.74105, 1.74146, \
1.74111, 1.73977, 1.73724, 1.73326, 1.72759, 1.71994, 1.71003, \
1.69753, 1.68213, 1.66347, 1.64119, 1.61492, 1.58428, 1.54888, \
1.50832, 1.46224, 1.41028, 1.35213, 1.28756, 1.21644, 1.13877, \
1.05479, 0.965005, 0.870346, 0.772266, 0.672914, 0.575269, 0.483202, \
0.401281, 0.334109, 0.285083, 0.255065, 0.242016, 0.241877, 0.249537, \
0.25934, 0.265716, 0.264102, 0.251884, 0.229106, 0.198813, 0.166855, \
0.141209, 0.132737}

解决方案

Use the plot or scatter method to plot x, y data. In your case, x is time and y is either one of X or Y. To generate your time coordinates use the range function:

range(5) -> [0, 1, 2, 3, 4]

Here is an example.

#!python2

import matplotlib.pyplot as plt

X = [0.132737, 0.13191, 0.129746, 0.127106, 0.125117, 0.12483, 0.126976,
0.131958, 0.140068, 0.151781, 0.167899, 0.189295, 0.216319, 0.248189,
0.282795, 0.317034, 0.347415, 0.370662, 0.384156, 0.386236, 0.376402,
0.355467, 0.325648, 0.290526, 0.254685, 0.222845, 0.198549, 0.183281,
0.177335, 0.182392, 0.203451, 0.246983, 0.315549, 0.405471, 0.509765,
0.621364, 0.73459, 0.845401, 0.951138, 1.05018, 1.14164, 1.22514,
1.30063, 1.3683, 1.42847, 1.48156, 1.52802, 1.56836, 1.60306,
1.63263, 1.65754, 1.67825, 1.69521, 1.70884, 1.71951, 1.72761,
1.73347, 1.7374, 1.73971, 1.74066, 1.74049, 1.73944, 1.7377, 1.73545,
1.73286, 1.73009, 1.72724, 1.72445, 1.7218, 1.71938, 1.71726, 1.7155,
1.71414, 1.71322, 1.71275, 1.71275, 1.71322, 1.71415, 1.71551,
1.71727, 1.71939, 1.72181, 1.72446, 1.72726, 1.7301, 1.73288,
1.73547, 1.73772, 1.73947, 1.74052, 1.74069, 1.73974, 1.73744,
1.7335, 1.72765, 1.71955, 1.70888, 1.69525, 1.67829, 1.65757,
1.63266, 1.60309, 1.56838, 1.52803, 1.48156, 1.42846, 1.36828,
1.30059, 1.22507, 1.14155, 1.05005, 0.950968, 0.845186, 0.734326,
0.621052, 0.509426, 0.405163, 0.31538, 0.247033, 0.203632, 0.18257,
0.177457, 0.183338, 0.198563, 0.222852, 0.254712, 0.290571, 0.325696,
0.355504, 0.376422, 0.386237, 0.38414, 0.370635, 0.347385, 0.317008,
0.282778, 0.24818, 0.216314, 0.189287, 0.167886, 0.151767, 0.140056,
0.131952, 0.126973, 0.124829, 0.125116, 0.127103, 0.129743, 0.131907,
0.132737]


ax = plt.subplot(111)
ax.plot(range(len(X)), X)
#ax.scatter(range(len(X)), X)
plt.show()

Edit in answer to the comment

#!python2

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

DATA = 'test0005.dat'

def get_data(fname=DATA):
    '''Read 2d array of z coordinates from file. Convert to float values
    and wrap in a numpy array.'''
    with open(fname) as f:
        data = [map(float, line.split()) for line in f]
    return np.array(data)


def plot(x, y, z):
    '''Surface plot of 3d data.'''
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    ax.plot_surface(x, y, z, rstride=4, cstride=4)
    plt.show()


if __name__ == '__main__':
    z = get_data()
    x = range(z.shape[0])
    y = range(z.shape[1])
    x, y = np.meshgrid(x, y)
    plot(x, y, z)

这篇关于使用python绘制mathematica数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆