简体   繁体   English

将X绘制在X上,因为X是y的每个值的时间范围

[英]Plot Y against X as X is a time range for each value of y

I'm trying to plot a simple graph to no avail! 我试图绘制一个简单的图表无济于事!

Instead of this the best i can come up with is this one 而不是这个 ,我能想到的最好的就是这个

Even this simple one isn't working, the main problem is I need the line to go just horizontal say for 5 mins at a constant y value then the value changes and I want it to go constant for another 10 mins or so. 即使是这个简单的也不行,主要的问题是我需要在恒定的y值下直线水平说5分钟,然后值变化,我希望它再恒定10分钟左右。 Something like this: 像这样的东西:

Y = [5, 10, 11, 6] Y = [5,10,11,6]

X = [0-10mins, 10mins-25mins, 25-45mins, 45-50mins] X = [0-10分钟,10分钟-25分钟,25-45分钟,45-50分钟]

plot Y on against X 将Y与X对比

在此输入图像描述

在此输入图像描述

在此输入图像描述

I think you need a step function. 我认为你需要一个阶梯功能。 Please note that here I added 0 to X and 5 to Y in the beginning so as to have the correct first step span from 0 to 10 请注意,在这里我在开头添加0到X和5到Y,以便正确的第一步跨度从0到10

import matplotlib.pyplot as plt

X = [0, 10, 25, 45, 50]
Y = [5, 5, 10, 11, 6]

plt.step(X, Y)
plt.show()

EDIT: If you have unsorted X-array and you want to sum up the X values, you can use cumsum from NumPy. 编辑:如果你有未分类的X阵列,并且你想总结X值,你可以使用NumPy的cumsum The main idea here is to first insert 0 to your list and then convert to array followed by cumulative summing 这里的主要思想是先将0插入列表,然后转换为数组,然后进行累加求和

import matplotlib.pyplot as plt
import numpy as np

X = [6, 27, 9, 19]
X.insert(0,0)
X = np.cumsum(X)

Y = [5, 5, 10, 11, 6]

plt.step(X, Y)
plt.show()

在此输入图像描述

I propose a simple loop solution like below. 我提出了一个简单的循环解决方案如下。 In order to make the figure nicer, you can specify different colors, texts and so on. 为了使图形更好,您可以指定不同的颜色,文本等。

#!/usr/bin/env ipython
# ------------------
import numpy as np
import matplotlib as mpl
import matplotlib.pylab as plt
# --------------------
xdata=[0,10,25,45,50]
ydata=[5,10,11,6];
colors=['b','b','b','b']
# --------------------
fig=plt.figure();
ax=fig.add_subplot(111);
for i in range(len(xdata)-1):
    xloc=0.5*(xdata[i+1]+xdata[i])
    xwidth=xdata[i+1]-xdata[i];
    ax.bar(xloc,ydata[i],width=xwidth,color=colors[i]);
plt.show()

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

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