【问题标题】:Plotting w/ Matplotlib使用 Matplotlib 绘图
【发布时间】:2020-09-26 02:17:03
【问题描述】:

我在 Google Colab 工作,想知道为什么在尝试使用 Matplotlib 绘制数据时会得到一个糟糕的图表。

import pandas as pd
from matplotlib import pyplot as plt

data = pd.read_csv('TSLA.csv')

df_raw = pd.DataFrame(data)

df_working = df_raw[['Date', 'Adj Close']]

df_adj_close = df_working['Adj Close'].rolling(window = 50).mean()
rolling_50 = df_adj_close.rolling(window = 50).mean()
rolling_200 = df_adj_close.rolling(window = 200).mean()

fig = plt.figure(figsize=(20, 10))
x = df_working['Date']
y = df_working['Adj Close']
a = rolling_50
b = rolling_200
plt.plot(x, y, a, b)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title('Title')
plt.show()

【问题讨论】:

    标签: matplotlib graph google-colaboratory


    【解决方案1】:

    替换:

    plt.plot(x, y, a, b)
    

    与:

    plt.plot(x, y, x, a, x, b)
    

    编辑:解释:

    假设剧情需要:

    x_axis --> x(三个图共有)

    y_axis --> y、a 和 b

    在上面的代码中plt.plot(x, y, a, b) 没有共同的x 轴。它的代码在同一个图上绘制 x 与 y 和 a 与 b,具有“不同”的 x_axis 值。因此,plt.plot(x, y, x, a, x, b) 现在将使用相同的公共 x_axis 创建三个图的叠加。

    【讨论】:

    • 请解释为什么此更改会起作用。
    • @Alex Stephens:根据要求添加了解释。
    猜你喜欢
    • 1970-01-01
    • 2017-12-12
    • 2015-01-15
    • 2015-04-24
    • 2021-01-31
    • 2019-04-12
    • 2018-01-20
    • 2022-12-06
    相关资源
    最近更新 更多