简体   繁体   English

Matplotlib 不绘制某些数据..尽管 scatter 有效

[英]Matplotlib doesn't plot certain data..although scatter works

say

twenty = [[0.00186157 0.00201416 0.00216675 0.00213623 0.00253296 0.00250244  0.00280762 0.00292969 0.00308228 0.0032959  0.00338745 0.003479  0.003479   0.00341797 0.00335693 0.00320435 0.00308228 0.0027771  0.00253296 0.00216675]]
twentyfirst = [[0.00186157]]

Following function - while it should plot for both scatter as well as lineplot, (this is implemented exactly as in the page ) Got as far as to plot both in markers, but the matplotlib is lost in generating lines.以下函数 - 虽然它应该同时绘制散点图和线图,(这与页面中的实现完全一样)尽可能在标记中绘制两者,但matplotlib在生成线中丢失。

def plot_time_series(twenty, twentyfirst):
    xlabel = np.arange(0, 1, 1./20).reshape(1,20)
    print(np.ones(twenty.shape[1])[np.newaxis,:].shape) #(1,20)
    A = np.vstack([xlabel, np.ones(twenty.shape[1])[np.newaxis,:]]).T

    m, c = np.linalg.lstsq(A, twenty.T)[0]
    print(m, c)
    plt.scatter(xlabel, twenty.T, c='b', label='data')
    ylabel = m*xlabel + c
    print(ylabel.shape) #(1,20)
    plt.plot(xlabel, ylabel, '-ok', label = 'fitted line')
    plt.legend(loc='best')
    plt.ylabel('amplitudes')
    plt.savefig('timeseries_problem2'+'_4')
    plt.close()

在此处输入图片说明

Under the hood, this question asks about the difference between plotting在幕后,这个问题询问了绘图之间的区别

plt.plot([[1,2,3]],[[2,3,1]])

and

plt.plot([[1],[2],[3]],[[2],[3],[1]])

In both cases the lists are 2 dimensional.在这两种情况下,列表都是二维的。 In the first case, you have a single row of data.在第一种情况下,您只有一行数据。 In the second case you have a single column of data.在第二种情况下,您只有一列数据。

From the documentation :文档

x , y : array-like or scalar x , y : 类似数组或标量
[...] [...]
Commonly, these parameters are arrays of length N. However, scalars are supported as well (equivalent to an array with constant value).通常,这些参数是长度为 N 的数组。但是,也支持标量(相当于具有常量值的数组)。

The parameters can also be 2-dimensional .参数也可以是二维的 Then, the columns represent separate data sets .然后,列代表单独的数据集

The important part is the last sentence.重要的部分是最后一句话。 If data is 2D, as here, it is interpreted column-wise.如果数据是二维的,如此处,它是按列解释的。 Since the row-array [[2,3,1]] consists of 3 columns, each with a single value.由于行数组[[2,3,1]]由 3 列组成,每列都有一个值。 plot will hence produce 3 single "lines" with one point.因此, plot将产生 3 条单“线”和一个点。 But since a single point defines no line, it will only be visible when activating the marker, eg但是由于单个点不定义线,因此只有在激活标记时才可见,例如

plt.plot([[1,2,3]], [[2,3,1]], marker="o")

在此处输入图片说明

When transposing this row array to a column array, it will be interpreted as a single dataset with 3 entries.将此行数组转置为列数组时,它将被解释为具有 3 个条目的单个数据集。 Hence the desired outcome of a single line因此,单行的预期结果

plt.plot([[1],[2],[3]], [[2],[3],[1]])

在此处输入图片说明

Of course flattening the array to 1D is equally possible,当然,将数组展平为 1D 同样是可能的,

plt.plot(np.array([[1,2,3]]).flatten(), np.array([[2,3,1]]).flatten())

You may easily check how many lines you produced您可以轻松检查您生产了多少行

print(len(plt.plot([[1,2,3]],[[2,3,1]])))            # prints 3
print(len(plt.plot([[1],[2],[3]],[[2],[3],[1]])))    # prints 1

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

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