从nump ndarray绘制matplotlib

时间:2017-03-22 18:16:20

标签: python numpy matplotlib

这很令人尴尬,但我无法将其描绘出来:

  import numpy as np
  import matplotlib.pyplot as plt
  datf=np.loadtxt(filename, dtype=float,delimiter=" ")
  print((datf))
  plt.plot(datf[:0], datf[:1])
  plt.show()

这是datf

[[  1.         19.778986 ]
 [  1.3625678  -1.9363698]
 [  1.4142136   6.5144132]
 [  1.6901453   3.8092139]
 [  2.         -4.0222051]]

错误是:

ValueError: x and y must have same first dimension

2 个答案:

答案 0 :(得分:2)

看起来您试图将第一列绘制为x,将第二列绘制为y。你在编制索引时犯了一个错误。要获取datf的第一列,您需要执行function drawChart($element,layout,dat){ console.log(dat); var chart_margin = { top:10, bottom:20, left:10, right:10 }; var height = $element.width()*0.1; var width = $element.width(); $("#est-chart").empty(); var svg = d3.select('#est-chart').append("svg") .attr("width", width) .attr("height", height) .append("g"); var y = d3.scaleLinear() .domain([0, d3.max(dat, function(d){return d;})]) .range([0, height]); var yAxis = d3.axisLeft(dat); var yAxis_g = svg.append("g") .attr("class", "y axis") .call(yAxis); } (请注意逗号)。

您的最终代码如下:

datf[:, 0]

答案 1 :(得分:0)

要获得第1列和第2列,索引必须是

plt.plot(datf[:,0],datf[:,1])