【问题标题】:How to shift a line in a matplotlib plot?如何在 matplotlib 图中移动一条线?
【发布时间】:2018-03-09 14:13:07
【问题描述】:

我正在尝试在 python 中绘制两个列表,一个是 test1,另一个是 predictions1

我希望绘制 test1 列表的前 150 个条目,然后绘制 predictions1 列表的条目 101-150,以便两个图相互叠加。这是我尝试过的:

import matplotlib.pyplot as plt
plt.figure(figsize=(15,8))
plt.plot(test1[1:150])
plt.plot(predictions1[101:150], color='red')
plt.show()

但我得到了结果:

显然这不是我想要实现的,因为我希望红色情节在最后叠加在蓝色情节上。请帮忙。

【问题讨论】:

  • 您没有向plot 提供任何 x 数据,因此 matplotlib 将自动生成 x 数据。您可以手动创建 x 数据,以便将 predictions1 绘制在 test1 的右侧
  • @DavidG 你能详细解释一下吗?我对 Python 不太熟悉
  • 这是您的 pyplot.plot() 函数的文档。如果这只是如何处理python的问题,您应该能够理解它。第一个参数是 X,第二个是 Y。

标签: python python-3.x matplotlib


【解决方案1】:

我们的想法是创建一个数字列表以用作您的 x 数据,从 0 到 150:

x_data = range(150)

然后slice 这样对于第一组数据,您的 x 轴使用数字 0 到 149。然后需要对第二组数据进行切片以使用数字 100 到 149。

plt.plot(x_data[:], test1[:150])
plt.plot(x_data[100:], predictions1[100:150], color='red')

请注意,Python 索引从 0 开始,而不是 1

【讨论】:

  • 我明白你做了什么。我尝试了同样的方法,但也没有给出理想的结果。红色图完全移动到蓝色图的右侧,而我希望红色图叠加在值 101-150 的蓝色图上。就像我有 test1 从 1 到 150 的情节。然后在同一个图和轴上,我有 predictions1 从 101-150 的图。
  • @TheDoctor 查看我更新的答案,看看是否能解决问题
  • 是的,就是这样。非常感谢:)
【解决方案2】:

此建议适用于任何类型的索引值(字符串、日期或整数),只要它们是唯一的。


简答:

创建最长系列的熊猫数据框。该数据框将有一个索引。从该系列中获取最后 50 个索引值,并将其与新数据框中的预测值相关联。您的两个数据框将具有不同的长度,因此您必须将它们 merge 放在一起才能获得两个相等长度的系列。使用这种方法,您的前 100 个预测值将为空,但您的数据将具有关联的索引,以便您可以根据 test1 系列绘制它。

详情:

由于您没有共享可重现的数据集,因此我制作了一些应与您的数据集结构匹配的随机数据。下面的第一个 sn-p 将重现您的情况,并使两个数组 test1 和 **predictions1 ** 可用于建议的解决方案。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(123456)
rows = 150
df = pd.DataFrame(np.random.randint(-4,5,size=(rows, 1)), columns=['test1'])
datelist = pd.date_range(pd.datetime(2017, 1, 1).strftime('%Y-%m-%d'), periods=rows).tolist()
df['dates'] = datelist 
df = df.set_index(['dates'])
df.index = pd.to_datetime(df.index)
df['test1'] = df['test1'].cumsum()

# Get the last 50 values of test1 (as array instead of dataframe)
# to mimic the names and data types of your source data 
test1 = df['test1'].values
predicionts1 = df['test1'].tail(50).values
predictions1 = predicionts1*1.1

# Reproducing your situation:
import matplotlib.pyplot as plt
plt.figure(figsize=(15,8))
plt.plot(test1)
plt.plot(predictions1, color = 'red')
plt.show()

下面的sn-p会在test1上叠加predictions1:

# Make a new dataframe of your prediction values
df_new = pd.DataFrame(test1)
df_new.columns = ['test1']

# Retrieve index values
new_index = df_new['test1'].tail(len(predictions1)).index

# Make a dataframe with your prediction values and your index
new_series = pd.DataFrame(index = new_index, data = predictions1)

# Merge the dataframes
df_new = pd.merge(df_new, new_series, how = 'left', left_index=True, right_index=True)
df_new.columns = ['test1', 'predictions1']

# And plot it
import matplotlib.pyplot as plt
plt.figure(figsize=(15,8))
plt.plot(df_new['test1'])
plt.plot(df_new['predictions1'], color = 'red')
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多