python - 如何使用 pyplot.plot 的可变数字参数?

标签 python matplotlib

这是我的数据和我想要生成的图表:

from numpy import array
from os.path import expanduser
import matplotlib.pyplot as plt
sns.set(style="darkgrid")

data = [array([8, 4, 9, 6, 2]), array([5, 4, 4, 1, 2]), array([4, 3, 1, 5, 6]), array([8, 3, 5, 6, 4])]

for d in data:
    plt.plot(d)

# plt.savefig(expanduser("~/Documents/rural_juror.pdf"))

enter image description here

因为我想在同一个 seaborn facetgrid 中绘制其中的几个,所以我无法使用循环,但必须在一行上完成,如下所示:

plt.plot(*data)
# produces same result as 
# plt.plot(data[0], data[1], data[2], data[3])

然而,这不起作用,但会产生如下所示的图表:

enter image description here

我猜这与绘图的参数类型为数据、外观、数据、外观...有关,但我该如何解决这个问题?

最佳答案

是的,如果您想用一行代码来完成,您需要给出xy值。你可以这样做:

import numpy as np
x = np.arange(5)
data = [x, array([8, 4, 9, 6, 2]),
        x, array([5, 4, 4, 1, 2]),
        x, array([4, 3, 1, 5, 6]),
        x, array([8, 3, 5, 6, 4])]
plt.plot(*data)

关于python - 如何使用 pyplot.plot 的可变数字参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35313641/

相关文章:

python - 生成对的pythonic方式

python - 通过使用 matplotlib 或 seaborn 循环列表来更新图形/绘图

python - matplotlib savefig 图像大小与 bbox_inches ='tight'

python - 获取 DF 中最大值的索引名称

python - 推广 ODE 求解器的函数

java - 什么是类似于 Rails 的面向对象桌面应用程序框架?

python - 彩色 3D 图

python - Matplotlib 图例 +ight_layout = 压扁的子图

python - 在python中生成日期数组

python - 如何在 Python 中打印没有子字符串的文本