简体   繁体   English

在python的同一图上绘制数组列表?

[英]Plotting a list of arrays on the same plot in python?

I have a list of arrays or an array of arrays that looks like 我有一个数组列表或一个数组数组,看起来像

a=[array1([.....]),array2([.....]),array3([....]),.....]

and a separate array b (not a list) 和一个单独的数组b(不是列表)

b=np.array[()]

All the arrays in the list "a" are the same length and the same length as "b". 列表“ a”中的所有数组的长度和长度均与“ b”相同。 I want to plot all the arrays in list "a" on the y axis verses "b" on the x axis all on the same plot. 我想在同一图上绘制列表“ a”在y轴上的所有数组与在“ b”在x轴上的所有图。 So one plot that consists of a[0]vs b, a[1] vs b, a[2] vs b,...and so on. 因此,一个由a [0] vs b,a [1] vs b,a [2] vs b等组成的图。

How can I do this? 我怎样才能做到这一点?

I tried 我试过了

f, axes = plt.subplots(len(a),1)
for g in range(len(a)):
    axes[g].plot(b,a[g])
    plt.show()

but this gives me many plots stacked on each other and they don't even have all the data. 但这给了我很多彼此叠加的图,甚至没有所有的数据。 I want everything on one plot. 我希望一切都在一个情节上。

I just found some old code that should accomplish this. 我只是找到了一些可以完成此操作的旧代码。 Try: 尝试:

import random
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np

# define a and b here

# Helps pick a random color for each plot, used for readability
rand = lambda: random.randint(0, 255)
fig = plt.figure(figsize=(10,7.5))
ax = fig.add_subplot(111)
for ydata in a:
    clr = '#%02X%02X%02X' % (rand(),rand(),rand())
    plot, = ax.plot(b, ydata, color=clr)

Edit: To generate the same set of colors every time, as answered in this post , try: 编辑:为生成同一组的每一次颜色,回答这个帖子 ,尝试:

colors = cm.rainbow(np.linspace(0, 1, len(a)))
for ydata, clr in zip(a, colors):
    plot, = ax.plot(b, ydata, color=clr)

np.linspace gives you "evenly spaced numbers over a specified interval", [0,1] for this purpose. np.linspace为您提供“在指定间隔内均匀间隔的数字”,[0,1]。

You have to define only one subplot and plot all arrays on the same axis. 您只需要定义一个子图并在同一轴上绘制所有阵列即可。

fig, ax = plt.subplots(1, 1)
for g in range(len(a)):
   ax.plot(b, a[g])
plt.show()

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

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