简体   繁体   English

Array 的 Python Plot 产生两行而不是一行

[英]Python Plot of Array Produces Two Lines Instead of One

Plotted an array generated from comma separated files and it plotted two lines instead of the correct one (orange).绘制了一个由逗号分隔的文件生成的数组,它绘制了两行而不是正确的行(橙色)。 If any one has could offer a solution or any suggestions it would be greatly appreciated!如果有人可以提供解决方案或任何建议,将不胜感激!

import numpy as np
import matplotlib.pyplot as plt
file = open("com.txt","r")
X, Y = [], []
npy_array = np.loadtxt(file, delimiter=",")
plt.plot(npy_array)
plt.show()

enter image description here在此处输入图像描述

It seems that you have loaded tabulated data into a 2d array.您似乎已将列表数据加载到二维数组中。 You need to separate this into independent and dependent variables and then plot one against the other, rather than plot both lines against the array index.您需要将其分为独立变量和因变量,然后将 plot 一对一分开,而不是 plot 两行都针对数组索引。 (Clearly the linearly increasing data in your blue line is intended to be the independent variable, and should be your "x" values, ranging from 0 to approximately 0.2.) Assuming that the lines of the file are in the order x,y, you would do this -- otherwise swap the 0 and 1. (很明显,你的蓝线中线性增加的数据是自变量,应该是你的“x”值,范围从 0 到大约 0.2。)假设文件的行是 x,y 的顺序,你会这样做——否则交换 0 和 1。

# to start with, some lines unchanged from the question
import numpy as np
import matplotlib.pyplot as plt
file = open("com.txt","r")
npy_array = np.loadtxt(file, delimiter=",")

# below here: what you need to change
x = npy_array[:,0]
y = npy_array[:,1]
plt.plot(x, y)

matplotlib plots 2d numpy arrays assuming each column is a different set of data, and should each be plotted indigently matplotlib 绘制 2d numpy arrays 假设每一列是一组不同的数据,并且应该每一个都被精心绘制

a = [[0,1,2,3], [0,2,3,6]]
np_a = np.array(a)
plt.plot(np_a,'x:')
plt.show()

4 个单独的图表

because the line is a strait one, I assume you want that to be your x axis.因为这条线是一条海峡,我假设你希望它成为你的 x 轴。 To do that you have to pass the x axis as the first perimeter, and the y axis as the second为此,您必须将 x 轴作为第一个周长,将 y 轴作为第二个周长

a = [[0,1,2,3], [0,2,3,6]]
np_a = np.array(a)
plt.plot(np_a[0],np_a[1],'x:')
plt.show()

x vs y 图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM