-1

我想寻求帮助。请帮助我编写 Python 2.7 中的代码。我想绘制气体释放的浓度。两周前我开始接触 Python,我之前没有看到它,我对 Pascal 有基本的了解,但它是不同的语言。

我不知道为什么不起作用...

这张图片展示了它的外观:flickr.com/photos/95370861@N02/8696658905/in/photostream/lightbox/

我的代码:

from __future__ import division
from matplotlib.patches import Polygon, Rectangle
import math
import numpy as np
import matplotlib.pyplot as plt


def draw(x, y,ax, color, lw=1):

    xr, yr = np.copy(x)[::-1], np.copy(y)[::-1]    
    for i in range(len(y)):
        y[i] = -y[i]
    verts = zip(x.tolist() + xr.tolist(), y.tolist() + yr.tolist())    
    poly = Polygon(verts, facecolor=color, edgecolor='k', lw=lw)
    ax.add_patch(poly)


def concentration_chart_plume(x,y, ax, ticks, colors, title):      

    maxX = 0
    for tick, color in zip(ticks[::-1], colors):
        x, y = calculation(tick)
        draw(np.array(x), np.array(y), ax, color)
        maxX = max([maxX] + x) 

    ''' plot parameters '''
    plt.xlim(0, maxX)
    plt.axis('equal')
    plt.xlabel('$x[m]$')
    plt.ylabel('$y[m]$')
    plt.title(title)
    sqrs = [Rectangle((0, 0), 1, 1, fc=clr) for clr in colors[::-1]]
    desc = ["> %g" % tick for tick in ticks]
    plt.legend(sqrs, desc, title="Concentration $kg/m^3$")
    plt.grid()



def calculation(tick,m):    
    cx = FILE_1
    sy = FILE_2       
    x  = FILE_3
    y  = [sy * np.sqrt(2 * np.log(cx / (tick)))]    #I do not know, how can be limited to non-zero values       
    return (x, y)



if __name__ == "__main__":    
    ''' Concentration plot of continuous gas release '''
    title = u"conturs"
    colors = ["#9EFF00", "#FFF500", "#FF9900", "#FF2E00"]    
    ticks = [0.1, 1e-2, 1e-3, 1e-4]  # value of concentration kg/m3, I want to show up
    fig = plt.figure()
    ax = fig.gca()    
    plt.show()

FILE_1 值:

[  2.21000000e+00   2.19000000e+00   2.16000000e+00   2.12000000e+00
   2.08000000e+00   1.90000000e+00   1.71000000e+00   1.18000000e+00
   7.93000000e-01   3.55000000e-01   1.93000000e-01   7.02500000e-02
   3.70800000e-02   1.37100000e-02   7.42100000e-03   4.77800000e-03
   3.39200000e-03   2.56200000e-03   2.02100000e-03   1.64600000e-03
   1.37300000e-03   1.16800000e-03   1.00900000e-03   8.82300000e-04
   7.80300000e-04   6.96400000e-04   6.26500000e-04   5.67500000e-04
   5.17200000e-04   4.73800000e-04   4.36200000e-04   4.03200000e-04
   3.74200000e-04   3.48500000e-04   3.25600000e-04   3.05100000e-04
   2.86700000e-04   2.70000000e-04   2.54900000e-04   2.28500000e-04
   2.17000000e-04   1.96700000e-04   1.87700000e-04   1.71600000e-04
   1.64300000e-04   1.51300000e-04   1.45500000e-04   1.34800000e-04
   1.29900000e-04   1.21100000e-04   1.17000000e-04   1.09500000e-04
   1.06000000e-04   9.96600000e-05   9.67000000e-05   9.12100000e-05
   8.86500000e-05   8.38800000e-05   8.16600000e-05   7.74900000e-05
   7.55300000e-05   7.18600000e-05   7.01300000e-05   6.68800000e-05
   6.53500000e-05   6.38700000e-05   6.24500000e-05   6.10800000e-05
   5.97600000e-05   5.84800000e-05   5.72600000e-05   5.60700000e-05
   5.49200000e-05   5.38100000e-05   5.27400000e-05   5.17100000e-05
   5.07000000e-05   4.97300000e-05   4.92400000e-05   4.72200000e-05
   4.35700000e-05   4.19100000e-05   3.88600000e-05   3.74700000e-05
   3.49000000e-05   3.37200000e-05   3.15400000e-05   3.05300000e-05
   2.86500000e-05   2.77800000e-05   2.61500000e-05   2.54000000e-05
   2.39800000e-05   2.33200000e-05   2.20700000e-05   2.14900000e-05
   2.03900000e-05   1.98700000e-05   1.89000000e-05   1.84300000e-05
   1.75600000e-05   1.71500000e-05   1.63700000e-05   1.60000000e-05
   1.53000000e-05   1.49700000e-05   1.43300000e-05   1.40300000e-05
   1.34600000e-05   1.31800000e-05   1.26600000e-05   1.24100000e-05
   1.19400000e-05   1.17100000e-05   1.12700000e-05   1.10600000e-05
   1.06600000e-05   1.04700000e-05   1.01000000e-05   9.92500000e-06
   9.58500000e-06   9.42200000e-06   9.10900000e-06   8.81100000e-06]

和 FILE_2 值:

[  9.21500000e-07   1.68000000e-01   3.13000000e-01   4.47000000e-01
   5.58000000e-01   8.37000000e-01   1.08000000e+00   1.67000000e+00
   2.20000000e+00   3.31000000e+00   4.30000000e+00   6.53000000e+00
   8.50000000e+00   1.29000000e+01   1.68000000e+01   2.03000000e+01
   2.37000000e+01   2.68000000e+01   2.98000000e+01   3.27000000e+01
   3.55000000e+01   3.82000000e+01   4.08000000e+01   4.34000000e+01
   4.59000000e+01   4.83000000e+01   5.07000000e+01   5.31000000e+01
   5.54000000e+01   5.77000000e+01   5.99000000e+01   6.21000000e+01
   6.43000000e+01   6.65000000e+01   6.86000000e+01   7.07000000e+01
   7.28000000e+01   7.49000000e+01   7.69000000e+01   8.09000000e+01
   8.29000000e+01   8.68000000e+01   8.87000000e+01   9.25000000e+01
   9.44000000e+01   9.81000000e+01   9.99000000e+01   1.04000000e+02
   1.05000000e+02   1.09000000e+02   1.11000000e+02   1.14000000e+02
   1.16000000e+02   1.19000000e+02   1.21000000e+02   1.24000000e+02
   1.26000000e+02   1.29000000e+02   1.31000000e+02   1.34000000e+02
   1.36000000e+02   1.39000000e+02   1.41000000e+02   1.44000000e+02
   1.46000000e+02   1.47000000e+02   1.49000000e+02   1.50000000e+02
   1.52000000e+02   1.53000000e+02   1.55000000e+02   1.57000000e+02
   1.58000000e+02   1.60000000e+02   1.61000000e+02   1.63000000e+02
   1.64000000e+02   1.66000000e+02   1.66000000e+02   1.69000000e+02
   1.73000000e+02   1.75000000e+02   1.80000000e+02   1.82000000e+02
   1.87000000e+02   1.89000000e+02   1.93000000e+02   1.96000000e+02
   2.00000000e+02   2.02000000e+02   2.07000000e+02   2.09000000e+02
   2.13000000e+02   2.15000000e+02   2.20000000e+02   2.22000000e+02
   2.26000000e+02   2.28000000e+02   2.33000000e+02   2.35000000e+02
   2.39000000e+02   2.41000000e+02   2.46000000e+02   2.48000000e+02
   2.52000000e+02   2.54000000e+02   2.59000000e+02   2.61000000e+02
   2.65000000e+02   2.67000000e+02   2.72000000e+02   2.74000000e+02
   2.78000000e+02   2.80000000e+02   2.84000000e+02   2.86000000e+02
   2.91000000e+02   2.93000000e+02   2.97000000e+02   2.99000000e+02
   3.03000000e+02   3.06000000e+02   3.10000000e+02   3.14000000e+02]

和 FILE_3 值:

[  2.59000000e+00   2.69000000e+00   2.92000000e+00   3.23000000e+00
   3.54000000e+00   4.48000000e+00   5.41000000e+00   8.00000000e+00
   1.06000000e+01   1.66000000e+01   2.27000000e+01   3.82000000e+01
   5.37000000e+01   9.37000000e+01   1.34000000e+02   1.74000000e+02
   2.14000000e+02   2.54000000e+02   2.94000000e+02   3.34000000e+02
   3.74000000e+02   4.14000000e+02   4.54000000e+02   4.94000000e+02
   5.34000000e+02   5.74000000e+02   6.14000000e+02   6.54000000e+02
   6.94000000e+02   7.34000000e+02   7.74000000e+02   8.14000000e+02
   8.54000000e+02   8.94000000e+02   9.34000000e+02   9.74000000e+02
   1.01400000e+03   1.05400000e+03   1.09400000e+03   1.17400000e+03
   1.21400000e+03   1.29400000e+03   1.33400000e+03   1.41400000e+03
   1.45400000e+03   1.53400000e+03   1.57400000e+03   1.65400000e+03
   1.69400000e+03   1.77400000e+03   1.81400000e+03   1.89400000e+03
   1.93400000e+03   2.01400000e+03   2.05400000e+03   2.13400000e+03
   2.17400000e+03   2.25400000e+03   2.29400000e+03   2.37400000e+03
   2.41400000e+03   2.49400000e+03   2.53400000e+03   2.61400000e+03
   2.65400000e+03   2.69400000e+03   2.73400000e+03   2.77400000e+03
   2.81400000e+03   2.85400000e+03   2.89400000e+03   2.93400000e+03
   2.97400000e+03   3.01400000e+03   3.05400000e+03   3.09400000e+03
   3.13400000e+03   3.17400000e+03   3.19500000e+03   3.25500000e+03
   3.37500000e+03   3.43500000e+03   3.55500000e+03   3.61500000e+03
   3.73500000e+03   3.79500000e+03   3.91500000e+03   3.97500000e+03
   4.09500000e+03   4.15500000e+03   4.27500000e+03   4.33500000e+03
   4.45500000e+03   4.51500000e+03   4.63500000e+03   4.69500000e+03
   4.81500000e+03   4.87500000e+03   4.99500000e+03   5.05500000e+03
   5.17500000e+03   5.23500000e+03   5.35500000e+03   5.41500000e+03
   5.53500000e+03   5.59500000e+03   5.71500000e+03   5.77500000e+03
   5.89500000e+03   5.95500000e+03   6.07500000e+03   6.13500000e+03
   6.25500000e+03   6.31500000e+03   6.43500000e+03   6.49500000e+03
   6.61500000e+03   6.67500000e+03   6.79500000e+03   6.85500000e+03
   6.97500000e+03   7.03500000e+03   7.15500000e+03   7.27500000e+03]

感谢您的帮助

4

1 回答 1

2

第 1 步:确保您实际上可以看到一个简单的情节。

您需要设置 python,以便以下脚本向您显示一些内容:

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,2*np.pi)
y = np.sin(x)
plt.plot(x,y)
plt.show()

如果这不起作用,那么您在更复杂的情节中看不到任何东西也就不足为奇了。让这个工作取决于你的系统的细节,但一个好的起点是弄清楚你想使用哪个后端。

对于交互式使用,大多数需要经常与绘图交互的人只会使用 ipython 和 pylab,从命令行启动为

ipython --pylab

您可能需要安装 ipython 和 pylab 才能使其正常工作。

步骤 1b:保存输出图形?

或者,如果交互式绘图不适合您,或者您只想将图形保存到文件中,您可以替换plt.show()

    plt.savefig('/path/to/file.png')

您可以替换pngpdf或各种其他文件类型。

第 2 步:让您的代码正常工作。

您发布的代码包含几个错误,并且没有绘制绘图的请求。这是我看到的一些更正。你的函数定义有太多的变量;他们应该是

def concentration_chart_plume(ax, ticks, colors, title):

def calculation(tick):

你的方程应该是这样的

    y  = np.asarray(sy) * np.sqrt(2 * np.log(np.asarray(cx) / (tick)))

Python 不知道如何将列表相乘,因此您必须将它们变成 numpy 数组。此外,您应该在代码的倒数第二行(就在之前plt.show())插入该行

    concentration_chart_plume(ax, ticks, colors, title)

这实际上绘制了情节。

最后,我假设您正在做一些明智的事情,以便您的脚本获得正确的FILE_1FILE_2FILE_3.

第 3 步:检查您的方程式

我不知道您要绘制什么,但是只要元素cx小于 1.0,您使用的方程式就会给出无效值(您似乎知道)。这真的是正确的方程式吗?如果是这样,您可能想使用类似的东西

cx = [max(1.0, c) for c in cx]

在设置 的值之后cx,但在使用它们之前。然后,对数将始终为 0.0 或更大,因此存在平方根。这基本上会丢弃您数据的较小值,所以我假设它们只是噪音或其他东西......

步骤 N+1:享受结果

在进行了步骤 2 和 3 中列出的所有更改(并更正了标题的拼写)之后,我得到了以下看起来非常合理的图:

等高线图

于 2013-05-01T15:06:08.390 回答