Matplotlib:绘制与x和y无关的轮廓线

时间:2018-02-05 23:41:17

标签: python arrays numpy matplotlib contour

我正在尝试绘制与x和y坐标相关联的轮廓线(高程)。我已经阅读了关于如何在x和y定义z时在Matplotlib上绘制轮廓的示例here但是如何绘制独立于x和y的轮廓线?

这是我的代码:

var htmlString = "";
var apiKey = 'AIzaSyDI4rWo_wVAxRZEIgF6_8sRZDj8OCOZZ38';
var playlistID = 'PLBhKKjnUR0XBVrHvtDOylN5XREHh9X1nt';
var maxResults = 7;

var playlists = [
  { playlistId: 'PLBhKKjnUR0XBVrHvtDOylN5XREHh9X1nt', el: '#youtube-playlist-feed_1' },
  { playlistId: 'PLBhKKjnUR0XB8DwQwXqBsChb48E8jzfr-', el: '#youtube-playlist-feed_2' },
  { playlistId: 'PLBhKKjnUR0XAM2Wvi7JY5gLRpFLzIE-An', el: '#youtube-playlist-feed_3' }
];

playlists.forEach(function(playlist){
  getVideoFeedByPlaylistId(playlist.playlistId, playlist.el);
})

function getVideoFeedByPlaylistId(playlistId, el){
  $.getJSON('https://www.googleapis.com/youtube/v3/playlistItems?key=' + apiKey + '&playlistId=' + playlistId + '&part=snippet&maxResults=' + (maxResults > 50 ? 50 : maxResults), function(data) {
  $.each(data.items, function(i, item) {
    var videoID = item['snippet']['resourceId']['videoId'];
    var title = item['snippet']['title'];
    var videoURL = 'https://www.youtube.com/watch?v=' + videoID + '&list=' + playlistID + '&index=1';
    htmlString += '<div class="video-wrap"><div class="video"><a target="_blank" href="' + videoURL + '"><img src="https://i.ytimg.com/vi/' + videoID + '/mqdefault.jpg"></a></div>' + '<div class="title"><a target="_blank" href="' + videoURL + '">' + title + '</a></div></div>';
  });
  $(el).html(htmlString);

  htmlString = '';
});
}

我收到错误“TypeError:输入z必须是2D数组。”

这是我第一次尝试绘制轮廓线,我对此表示感谢。

1 个答案:

答案 0 :(得分:1)

鉴于只有6个数据点,从这些数据点绘制的等高线图可能不是很有用。不过,对于更多的观点,这个概念也是一样的。

当然,无法绘制x,y和z独立的等高线。如果你有6个z点,你需要6个点和6个点 - 你有。所以解决方案可能相当简单 - 只需使用tricontour而不是contour

import numpy as np
import matplotlib.pyplot as plt

data = [(0, 200, 140), (100, 430, 260), (800, 340, 320), 
        (250, 110, 430), (290, 40, 100), (590, 35, 180)]
x,y,z = zip(*data)
plt.figure()
plt.tricontour(x,y,z)
plt.show()

enter image description here

更一般地说,您也可以插入数据。请参阅Make contour of scatter