I have an array of timestamps in the format (HH:MM:SS.mmmmmm) and another array of floating point numbers, each corresponding to a value in the timestamp array.
我有一组时间戳格式(HH:MM: ss.mmmmm)和另一个浮点数数组,每个浮点数对应于时间戳数组中的值。
Can I plot time on the x axis and the numbers on the y-axis using Matplotlib?
我可以用Matplotlib在x轴上绘制时间和y轴上的数字吗?
I was trying to, but somehow it was only accepting arrays of floats. How can I get it to plot the time? Do I have to modify the format in any way?
我一直在尝试,但是它只是接受了浮点数。我怎么才能画出时间?我需要以任何方式修改格式吗?
4 个解决方案
#1
115
You must first convert your timestamps to Python datetime
objects (use datetime.strptime
). Then use date2num
to convert the dates to matplotlib format.
您必须首先将时间戳转换为Python datetime对象(使用datetime.strptime)。然后使用date2num将日期转换为matplotlib格式。
Plot the dates and values using plot_date
:
使用plot_date绘制日期和值:
dates = matplotlib.dates.date2num(list_of_datetimes)
matplotlib.pyplot.plot_date(dates, values)
#2
57
You can also plot the timestamp, value pairs using pyplot.plot (after parsing them from their string representation). (Tested with matplotlib versions 1.2.0 and 1.3.1.)
您还可以使用pyplot绘制时间戳、值对。绘图(在解析它们的字符串表示之后)。(使用matplotlib版本1.2.0和1.3.1测试。)
Example:
例子:
import datetime
import random
import matplotlib.pyplot as plt
# make up some data
x = [datetime.datetime.now() + datetime.timedelta(hours=i) for i in range(12)]
y = [i+random.gauss(0,1) for i,_ in enumerate(x)]
# plot
plt.plot(x,y)
# beautify the x-labels
plt.gcf().autofmt_xdate()
plt.show()
Resulting image:
生成的图片:
Here's the same as a scatter plot:
这和散点图是一样的:
import datetime
import random
import matplotlib.pyplot as plt
# make up some data
x = [datetime.datetime.now() + datetime.timedelta(hours=i) for i in range(12)]
y = [i+random.gauss(0,1) for i,_ in enumerate(x)]
# plot
plt.scatter(x,y)
# beautify the x-labels
plt.gcf().autofmt_xdate()
plt.show()
Produces an image similar to this:
产生与此类似的图像:
#3
7
7 years later and this code has helped me. However, my times still were not showing up correctly.
7年后,这个代码帮助了我。然而,我的时代仍然没有正确地出现。
Using Matplotlib 2.0.0 and I had to add the following bit of code from Editing the date formatting of x-axis tick labels in matplotlib by Paul H.
使用Matplotlib 2.0.0,我必须添加以下代码,来自于Paul H在Matplotlib中编辑x轴标记的日期格式。
import matplotlib.dates as mdates
myFmt = mdates.DateFormatter('%d')
ax.xaxis.set_major_formatter(myFmt)
I changed the format to (%H:%M) and the time displayed correctly.
我将格式更改为(%H:%M)并正确显示时间。
All thanks to the community.
都要感谢社区。
#4
0
I had trouble with this using matplotlib version: 2.0.2. Running the example from above I got a centered stacked set of bubbles.
我在使用matplotlib版本2.0.2时遇到了问题。从上面运行这个例子,我得到了一组集中的泡泡。
I "fixed" the problem by adding another line:
我又加了一行“修正”了这个问题:
plt.plot([],[])
The entire code snippet becomes:
整个代码段变成:
import datetime
import random
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# make up some data
x = [datetime.datetime.now() + datetime.timedelta(minutes=i) for i in range(12)]
y = [i+random.gauss(0,1) for i,_ in enumerate(x)]
# plot
plt.plot([],[])
plt.scatter(x,y)
# beautify the x-labels
plt.gcf().autofmt_xdate()
myFmt = mdates.DateFormatter('%H:%M')
plt.gca().xaxis.set_major_formatter(myFmt)
plt.show()
plt.close()
This produces an image with the bubbles distributed as desired.
这将产生一个气泡按需要分布的图像。
#1
115
You must first convert your timestamps to Python datetime
objects (use datetime.strptime
). Then use date2num
to convert the dates to matplotlib format.
您必须首先将时间戳转换为Python datetime对象(使用datetime.strptime)。然后使用date2num将日期转换为matplotlib格式。
Plot the dates and values using plot_date
:
使用plot_date绘制日期和值:
dates = matplotlib.dates.date2num(list_of_datetimes)
matplotlib.pyplot.plot_date(dates, values)
#2
57
You can also plot the timestamp, value pairs using pyplot.plot (after parsing them from their string representation). (Tested with matplotlib versions 1.2.0 and 1.3.1.)
您还可以使用pyplot绘制时间戳、值对。绘图(在解析它们的字符串表示之后)。(使用matplotlib版本1.2.0和1.3.1测试。)
Example:
例子:
import datetime
import random
import matplotlib.pyplot as plt
# make up some data
x = [datetime.datetime.now() + datetime.timedelta(hours=i) for i in range(12)]
y = [i+random.gauss(0,1) for i,_ in enumerate(x)]
# plot
plt.plot(x,y)
# beautify the x-labels
plt.gcf().autofmt_xdate()
plt.show()
Resulting image:
生成的图片:
Here's the same as a scatter plot:
这和散点图是一样的:
import datetime
import random
import matplotlib.pyplot as plt
# make up some data
x = [datetime.datetime.now() + datetime.timedelta(hours=i) for i in range(12)]
y = [i+random.gauss(0,1) for i,_ in enumerate(x)]
# plot
plt.scatter(x,y)
# beautify the x-labels
plt.gcf().autofmt_xdate()
plt.show()
Produces an image similar to this:
产生与此类似的图像:
#3
7
7 years later and this code has helped me. However, my times still were not showing up correctly.
7年后,这个代码帮助了我。然而,我的时代仍然没有正确地出现。
Using Matplotlib 2.0.0 and I had to add the following bit of code from Editing the date formatting of x-axis tick labels in matplotlib by Paul H.
使用Matplotlib 2.0.0,我必须添加以下代码,来自于Paul H在Matplotlib中编辑x轴标记的日期格式。
import matplotlib.dates as mdates
myFmt = mdates.DateFormatter('%d')
ax.xaxis.set_major_formatter(myFmt)
I changed the format to (%H:%M) and the time displayed correctly.
我将格式更改为(%H:%M)并正确显示时间。
All thanks to the community.
都要感谢社区。
#4
0
I had trouble with this using matplotlib version: 2.0.2. Running the example from above I got a centered stacked set of bubbles.
我在使用matplotlib版本2.0.2时遇到了问题。从上面运行这个例子,我得到了一组集中的泡泡。
I "fixed" the problem by adding another line:
我又加了一行“修正”了这个问题:
plt.plot([],[])
The entire code snippet becomes:
整个代码段变成:
import datetime
import random
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# make up some data
x = [datetime.datetime.now() + datetime.timedelta(minutes=i) for i in range(12)]
y = [i+random.gauss(0,1) for i,_ in enumerate(x)]
# plot
plt.plot([],[])
plt.scatter(x,y)
# beautify the x-labels
plt.gcf().autofmt_xdate()
myFmt = mdates.DateFormatter('%H:%M')
plt.gca().xaxis.set_major_formatter(myFmt)
plt.show()
plt.close()
This produces an image with the bubbles distributed as desired.
这将产生一个气泡按需要分布的图像。