python - Matplotlib 将数组转换为 1 条图表线

标签 python matplotlib plot data-visualization visualization

我有一个列表,我想将其转换为折线图,我得到的结果非常困惑,列表包含负值和正值。

我的代码:

macd = []
signals = []

for i in data['Technical Analysis: MACD'].values():
    macd.insert(len(macd), i['MACD'])
    signals.insert(len(signals), i['MACD_Signal'])

plt.plot(macd)
plt.plot(signals)
plt.show()

这是我得到的: enter image description here

数据示例:

macd = ['-0.0099', '-0.0108', '-0.0091', '-0.0074', '-0.0052', '-0.0002', '0.0032', '0.0064', '0.0083', '0.0107', '0.0106', '0.0102', '0.0126', '0.0145', '0.0137', '0.0114', '0.0093', '0.0055', '-0.0005', '-0.0070', '-0.0082', '-0.0116', '-0.0137', '-0.0152', '-0.0159', '-0.0175', '-0.0184', '-0.0191', '-0.0199', '-0.0205', '-0.0210', '-0.0214', '-0.0205', '-0.0191', '-0.0170', '-0.0161', '-0.0148', '-0.0118', '-0.0089', '-0.0091', '-0.0082', '-0.0070', '-0.0073', '-0.0076', '-0.0069', '-0.0064', '-0.0062', '-0.0048', '-0.0039', '-0.0028']

signals = ['-0.0042', '-0.0028', '-0.0008', '0.0012', '0.0034', '0.0056', '0.0070', '0.0079', '0.0083', '0.0083', '0.0077', '0.0070', '0.0062', '0.0046', '0.0021', '-0.0008', '-0.0038', '-0.0071', '-0.0102', '-0.0127', '-0.0141', '-0.0156', '-0.0165', '-0.0173', '-0.0178', '-0.0182', '-0.0184', '-0.0184', '-0.0182', '-0.0178', '-0.0172', '-0.0162', '-0.0149', '-0.0135', '-0.0121', '-0.0109', '-0.0096', '-0.0083', '-0.0074', '-0.0070', '-0.0065', '-0.0061', '-0.0059', '-0.0055', '-0.0050', '-0.0045', '-0.0041', '-0.0036', '-0.0033', '-0.0031']

最佳答案

数据中的值是str,在绘图之前需要将它们转换为float等数字类型:

macd = list(map(float, macd))
signals = list(map(float, signals))

enter image description here

关于python - Matplotlib 将数组转换为 1 条图表线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68921566/

相关文章:

python - 使用 SWIG 用指针参数包装 C 函数

python - Jupyter 实验室中的后台线程时钟

使用 dictconfig 的 Python Loggly 日志记录设置

python - 在 matplotlib 文本框中对齐 LaTeX 数学文本

python - 给定四边形的角,使用 matplotlib 在 3d 中绘制阴影四边形

r - ggplot2:适合 geom_smooth() 像分类变量是连续的

python - 使用 Python 对 CSV 进行 MD5 哈希处理

python - 无法在 pandas python 中绘制我的数据

matlab - plot 和 scatter matlab 的区别

swift - CorePlot实时绘图