使用for循环创建等效的np.meshgrid

时间:2018-10-09 03:57:51

标签: python numpy for-loop

此代码在二维x,y网格上输出e ^(-r ^ 2)的图:

import numpy as np
import matplotlib.pyplot as plt

x1d = np.arange(-3, 3, 0.006)
y1d = np.arange(-3, 3, 0.006)

x2d, y2d = np.meshgrid(x1d, y1d)
r2d = x2d**2 + y2d**2

z = np.exp(-r2d)

plt.imshow(z, extent = (3, -3 , 3 , -3))
plt.title("A 2-D Image plot")
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.savefig('2dexp_array.pdf', format ='pdf')
plt.show(block = False)

我想更改代码,以代替使用np.meshgrid,它使用for循环生成2d网格空间。

类似的东西:

import numpy as np
import matplotlib.pyplot as plt

x1d = np.arange(-3, 3, 0.006)
y1d = np.arange(-3, 3, 0.006)

z = np.zeros([2000, 2000])

,然后使用for循环根据x1d,y1d用正确的值替换z中的零。

但是我不确定如何使用for循环来镜像meshgrid的功能。

如果有人可以指出正确的方向,我将不胜感激 谢谢

1 个答案:

答案 0 :(得分:0)

这应该做:

import express from 'express';
import { matchRoutes } from 'react-router-config';
import Routes from './client/Routes';
import renderer from './helpers/renderer';
import createStore from './helpers/createStore';
const app = express();

app.use(express.static('public'));
app.get('*', (req, res) => {
  const store = createStore(req);

  const promises = matchRoutes(Routes, req.path)
    .map(({ route }) => {
      return route.loadData ? route.loadData(store) : null;
    })
    .map(promise => {
      if (promise) {
        return new Promise((resolve, reject) => {
          promise.then(resolve).catch(resolve);
        });
      }
    });

  Promise.all(promises).then(() => {
    const context = {};
    const content = renderer(req, store, context);

    if (context.url) {
      return res.redirect(301, context.url);
    }
    if (context.notFound) {
      res.status(404);
    }

    res.send(content);
  });
});

app.listen(3000, () => {
  console.log('Listening on prot 3000');
});