Python 3.x 如何绘制等高线图?

Python 3.x 如何绘制等高线图?,python-3.x,contour,Python 3.x,Contour,我是Python的初学者。我相信Python会为3D等高线图完成这项任务。 我有如下数据 Y/X (mm), 0, 10, 20, 30, 40 686.6, -5.02, -0.417, 0, 100.627, 0 694.08, -5.02, -4.529, -17.731, -5.309, -3.535 701.56, 1.869, -4.529, -17.731, -5.309, -3.535 709.04, 1.869, -4.689, -17.66

我是Python的初学者。我相信Python会为3D等高线图完成这项任务。 我有如下数据

Y/X (mm),   0,  10, 20, 30, 40
686.6,  -5.02,  -0.417, 0,  100.627,    0
694.08, -5.02,  -4.529, -17.731,    -5.309, -3.535
701.56, 1.869,  -4.529, -17.731,    -5.309, -3.535
709.04, 1.869,  -4.689, -17.667,    -5.704, -3.482
716.52, 4.572,  -4.689, -17.186,    -5.704, -2.51 
724,    4.572,  -4.486, -17.186,    -5.138, -2.51
731.48, 6.323,  -4.486, -16.396,    -5.138, -1.933
738.96, 6.323,  -4.977, -16.396,    -5.319, -1.933
746.44, 7.007,  -4.251, -16.577,    -5.319, -1.688
753.92, 7.007,  -4.251, -16.577,    -5.618, -1.688
761.4,  7.338,  -3.514, -16.78, -5.618, -1.207
768.88, 7.338,  -3.514, -16.78, -4.657, -1.207
776.36, 7.263,  -3.877, -15.99, -4.657, -0.822
如何开始有什么帮助吗

更新问题


(1) 如您所见,原始数据的第1行、第1列中分别有xlabel和ylabel。 如果我使用函数,如何拆分“xs”和“ys”

(2) 你有办法旋转矩阵M x N吗

(3) linespace有start=-70和stop=60,num=60,您知道如何进行步骤5吗

contour = subplot.contourf(xs, ys, data, levels=numpy.linspace(-70, 60, 60))
您可以使用,即其功能:

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy


xs = numpy.array([0,  10, 20, 30, 40])
ys = numpy.array([686.6, 694.08, 701.56, 709.04, 716.52,
    724, 731.48, 738.96, 746.44, 753.92, 761.4, 768.88, 776.36])

data = numpy.array([
    [-5.02,  -0.417, 0,  100.627,    0],
    [-5.02,  -4.529, -17.731,    -5.309, -3.535],
    [1.869,  -4.529, -17.731,    -5.309, -3.535],
    [1.869,  -4.689, -17.667,    -5.704, -3.482],
    [4.572,  -4.689, -17.186,    -5.704, -2.51],
    [4.572,  -4.486, -17.186,    -5.138, -2.51],
    [6.323,  -4.486, -16.396,    -5.138, -1.933],
    [6.323,  -4.977, -16.396,    -5.319, -1.933],
    [7.007,  -4.251, -16.577,    -5.319, -1.688],
    [7.007,  -4.251, -16.577,    -5.618, -1.688],
    [7.338,  -3.514, -16.78, -5.618, -1.207],
    [7.338,  -3.514, -16.78, -4.657, -1.207],
    [7.263,  -3.877, -15.99, -4.657, -0.822]])

fig = plt.figure()
subplot = fig.add_subplot(111, xlabel='$x$, mm', ylabel='$y$, mm')

contour = subplot.contourf(xs, ys, data, levels=numpy.linspace(-20, 120, 20))
subplot.set_xlim((xs[0], xs[-1]))
subplot.set_ylim((ys[0], ys[-1]))
fig.colorbar(contour)

fig.savefig('t.png')


您可以查看
matplotlib
的功能。

酷!谢谢但是,我有一个错误:没有名为“matplotlib”的模块。我使用的是Eclipse标准/SDK版本:开普勒发布版本id:20130614-0229。好吧,和其他任何Python包一样,您必须安装它。根据您的操作系统的不同,该过程可能会有所不同,如果您拥有所有必需的库(
ghostscipt
libpng
,以及我目前记不起来的其他几个库),则可以像
pip install matplotlib
一样简单。你应该查一下,安装过程已经在互联网上的许多地方被多次描述过。正如你所看到的原始数据,它们在第一行、第一列中分别有xlabel和ylabel。如果我使用numpy.loadtxt函数,如何拆分“xs”和“ys”?'data=numpy.loadtxt('131014-data-xy-conv-1.txt')你有办法从中旋转矩阵M x N吗?你真的应该创建一个新问题,因为你的更新与原始问题无关。但简单的回答是:使用数组拼接(它看起来像
xs=data[0,1:];ys=data[1:,0];heights=data[1:,1:][/code>)。我无法给出确切的答案,因为您问题中的数据无法以当前形式加载
loadtxt
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy


xs = numpy.array([0,  10, 20, 30, 40])
ys = numpy.array([686.6, 694.08, 701.56, 709.04, 716.52,
    724, 731.48, 738.96, 746.44, 753.92, 761.4, 768.88, 776.36])

data = numpy.array([
    [-5.02,  -0.417, 0,  100.627,    0],
    [-5.02,  -4.529, -17.731,    -5.309, -3.535],
    [1.869,  -4.529, -17.731,    -5.309, -3.535],
    [1.869,  -4.689, -17.667,    -5.704, -3.482],
    [4.572,  -4.689, -17.186,    -5.704, -2.51],
    [4.572,  -4.486, -17.186,    -5.138, -2.51],
    [6.323,  -4.486, -16.396,    -5.138, -1.933],
    [6.323,  -4.977, -16.396,    -5.319, -1.933],
    [7.007,  -4.251, -16.577,    -5.319, -1.688],
    [7.007,  -4.251, -16.577,    -5.618, -1.688],
    [7.338,  -3.514, -16.78, -5.618, -1.207],
    [7.338,  -3.514, -16.78, -4.657, -1.207],
    [7.263,  -3.877, -15.99, -4.657, -0.822]])

fig = plt.figure()
subplot = fig.add_subplot(111, xlabel='$x$, mm', ylabel='$y$, mm')

contour = subplot.contourf(xs, ys, data, levels=numpy.linspace(-20, 120, 20))
subplot.set_xlim((xs[0], xs[-1]))
subplot.set_ylim((ys[0], ys[-1]))
fig.colorbar(contour)

fig.savefig('t.png')