I have an amount of times written in the format hh:mm:ss, if I use the code below and print what x is I get 1900, 1, 1, 10, 29, 34 for every timestamp. How can I take away the year, month and date? As I want to have the time in the format hh:mm:ss
EDIT: Updated with the whole code as it looks now with help from comments.
import matplotlib.pyplot as plt
import matplotlib.ticker
import time
import datetime
x = ['10:29:55', '10:34:44']
sy1 = [679.162, 679.802]
x_labels = [datetime.datetime.strptime(elem, '%H:%M:%S') for elem in x]
formatter = matplotlib.ticker. FixedFormatter(x_labels)
plt.gca().xaxis.set_major_formatter(formatter)
plt.gca().xaxis.set_minor_formatter(formatter)
plt.plot(x_labels, sy1, 'ro')
plt.xlabel('Time')
plt.ylabel('Position')
plt.show()
But obviously it displays the time when taking into account the year, month and date too.
Plotting (wrong) time against y values
If I use strftime instead of strptime I get a TypeError: descriptor 'strftime' requires a 'datetime.date' object but received a 'str'
Ok. There are a few ways to get what you want.
If you're willing to settle for having microseconds, you should be really close to what you need:
import matplotlib.pyplot as plt
import datetime
time_strings = ['10:29:55', '10:34:44']
sy1 = [679.162, 679.802]
times = [datetime.datetime.strptime(elem, '%H:%M:%S') for elem in time_strings]
plt.plot(times, sy1, 'ro')
plt.xlabel('Time')
plt.ylabel('Position')
plt.show()
This should show the times you want in a plot, just with microseconds in the formatting. The microseconds make it all ugly, but my only changes were ones for clarity - I didn't import time or import matplotlib.ticker, I changed your x to a more accurate variable name, and created the datetimes as you did. To get rid of the microseconds, things get uglier. You can't just use the FixedFormatter because we only set 2 values, and the standard plot has more than 2 ticks; you have to find a way to get the FuncFormatter to work. This works as desired, but is still too noisy, so I'm adding in the plt.gcf().autofmt_xdate() as well.
import matplotlib.pyplot as plt
import matplotlib.ticker
import datetime
import pylab
time_strings = ['10:29:55', '10:34:44']
sy1 = [679.162, 679.802]
times = [datetime.datetime.strptime(elem, '%H:%M:%S') for elem in time_strings]
plt.plot(times, sy1, 'ro')
formatter = matplotlib.ticker.FuncFormatter(lambda tick_value, _: datetime.datetime.strftime(pylab.num2date(tick_value), '%H:%M:%S'))
plt.gca().xaxis.set_major_formatter(formatter)
plt.gca().xaxis.set_minor_formatter(formatter)
plt.xlabel('Time')
plt.ylabel('Position')
plt.gcf().autofmt_xdate()
plt.show()
The line defining the FuncFormatter is messy. I define a lambda, which is a function defined on a single line. FuncFormatter expects it to take 2 arguments. The first one is the tick_value, and we don't really care what the second one is, so I gave it the standard variable name of _ to show we don't care. The tick values are datetimes or timestamps. The way we get from the tick value to a datetime is by calling pylab.num2date.
You'll find that this second solution is just what you need. The key thing you needed to do was keep track of what your variable types were, and what variable types were needed where.
Related
I am trying to use the axvline function to plot datetime.time values and seeing this error. please help.
Here is the code:
import datetime as dt
from datetime import datetime
import matplotlib.pyplot as plt
i='wpt'
x = [datetime.time(12,10), datetime.time(12, 15)]
fig, axs = plt.subplots(3, sharex = True, figsize = (12,9), constrained_layout = True)
axs[i].axvline(x[0], color = 'lightskyblue', ls = '--', lw = 1)
When i run the code, i get the following error
TypeError: '>' not supported between instances of 'float' and 'datetime.time'
I checked online and found no solution that uses axvline with datetime.time
matplotlib expects a numeric x coordinate, not a datetime object.
You need to convert the time to a number, e.g. by using the matplotlib.dates.date2num function:
import matplotlib.dates as mdates
axs[i].axvline(mdates.date2num(datetime.combine(date.today(), x[0])), ...)
This will convert the time to a floating point number representing the number of days since January 1, 0001. Matplotlib will then use its date plotting machinery to format the axis appropriately.
Note that I used datetime.combine here to convert the time into a full datetime object by adding today's date.
I am plotting over a period of seconds and have time as the labels on the x-axis. Here is the only way I could get the correct time stamps. However, there are a bunch of zeros on the end. Any idea how to get rid of them??
plt.style.use('seaborn-whitegrid')
df['timestamp'] = pd.to_datetime(df['timestamp'])
fig, ax = plt.subplots(figsize=(8,4))
seconds=MicrosecondLocator(interval=500000)
myFmt = DateFormatter("%S:%f")
ax.plot(df['timestamp'], df['vibration(g)_0'], c='blue')
ax.xaxis.set_major_locator(seconds)
ax.xaxis.set_major_formatter(myFmt)
plt.gcf().autofmt_xdate()
plt.show()
This produces this image. Everything looks perfect except for all of the extra zeros. How can I get rid of them while still keeping the 5?
I guess you would want to simply cut the last 5 digits out of the string. That's also what answers to python datetime: Round/trim number of digits in microseconds suggest.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.dates import MicrosecondLocator, DateFormatter
from matplotlib.ticker import FuncFormatter
x = np.datetime64("2018-11-30T00:00") + np.arange(1,4, dtype="timedelta64[s]")
fig, ax = plt.subplots(figsize=(8,4))
seconds=MicrosecondLocator(interval=500000)
myFmt = DateFormatter("%S:%f")
ax.plot(x,[2,1,3])
def trunc_ms_fmt(x, pos=None):
return myFmt(x,pos)[:-5]
ax.xaxis.set_major_locator(seconds)
ax.xaxis.set_major_formatter(FuncFormatter(trunc_ms_fmt))
plt.gcf().autofmt_xdate()
plt.show()
Note that this format is quite unusual; so make sure the reader of the plot understands it.
When I plot my half hourly time series, my axis labels are odd (like 16:33:12h or so...)
When I use HourLocator to fix this (16:33h -> 16:00h), then my x label disappear completely.
My code is:
from datetime import date, timedelta, datetime, time
from matplotlib.dates import DayLocator, HourLocator
import matplotlib.pyplot as plt
start = time(0, 0, 0)
delta = timedelta(minutes=30)
times = []
for i in range(len(day_load)):
dt = datetime.combine(date.today(), time(0, 0)) + delta * i
times.append(dt.time())
load = [i/48 for i in range(48)]
fig, ax = plt.subplots()
ax.plot_date(times, load)
ax.xaxis.set_major_locator(HourLocator())
plt.show()
How can I achieve "even" labels (in a best practice way - I don't want to rewrite code for every other plot again).
When I comment second last line, I get normal "odd" labels :(
Thanks for answers!
There are two main issues:
You need to work with complete datetime objects, not only with time. So instead of dt.time() you should append dt directly.
You not only need a locator, but also a formatter to produce nice ticklabels. Here you may use a DateFormatter("%H:%M") to show hours and minutes.
Complete code:
from datetime import date, timedelta, datetime, time
from matplotlib.dates import DayLocator, HourLocator,DateFormatter
import matplotlib.pyplot as plt
start = time(0, 0, 0)
delta = timedelta(minutes=30)
times = []
n=48
for i in range(n):
# use complete datetime object, not only time
dt = datetime.combine(date.today(), time(0, 0)) + delta * i
times.append(dt)
load = [i/float(n) for i in range(n)]
fig, ax = plt.subplots()
ax.plot_date(times, load)
# set a locator, as well as a formatter
ax.xaxis.set_major_locator(HourLocator())
ax.xaxis.set_major_formatter(DateFormatter("%H:%M"))
#optionally rotate the labels and make more space for them
fig.autofmt_xdate()
plt.show()
This is an old question, but for anyone else facing this problem: you can leave the data types what they should be and use matplotlib.ticker.IndexLocator to get the axis ticks located nicely.
For example,
locator = mpl.ticker.IndexLocator(base=2 * 60 * 60, offset=0)
ax.xaxis.set_major_locator(locator)
places ticks at every two full hours, i.e. it uses the total number of seconds since midnight, regardless of the length of the intervals in the data.
Your code doesn't run, because day_load is undefined and I get other issues as well.
Not an answer, but I think you're better of using pandas. It makes it easy to create a date_range, and plotting is handled pretty well without adjustments.
from scipy import stats
import pandas as pd
n = 20
index = pd.date_range(start = '2016-01-01', periods = n, freq='1H')
df = pd.DataFrame(index = index)
df["value"] = stats.norm().rvs(n)
df.plot()
I have the following code to plot a chart with matplotlib
#!/usr/bin/env python
import matplotlib.pyplot as plt
import urllib2
import json
req = urllib2.urlopen("http://localhost:17668/retrieval/data/getData.json? pv=LNLS:ANEL:corrente&donotchunk")
data = json.load(req)
secs = [x['secs'] for x in data[0]['data']]
vals = [x['val'] for x in data[0]['data']]
plt.plot(secs, vals)
plt.show()
The secs arrays is epoch time.
What I want is to plot the data in the x axis (secs) as a date (DD-MM-YYYY HH:MM:SS).
How can I do that?
To plot date-based data in matplotlib you must convert the data to the correct format.
One way is to first convert your data to datetime objects, for an epoch timestamp you should use datetime.datetime.fromtimestamp().
You must then convert the datetime objects to the right format for matplotlib, this can be handled using matplotlib.date.date2num.
Alternatively you can use matplotlib.dates.epoch2num and skip converting your date to datetime objects in the first place (while this will suit your use-case better initially, I would recommend trying to keep date based date in datetime objects as much as you can when working, it will save you a headache in the long run).
Once you have your data in the correct format you can plot it using plot_date.
Finally to format your x-axis as you wish you can use a matplotlib.dates.DateFormatter object to choose how your ticks will look.
import matplotlib.pyplot as plt
import matplotlib.dates as mdate
import numpy as np
# Generate some random data.
N = 40
now = 1398432160
raw = np.array([now + i*1000 for i in range(N)])
vals = np.sin(np.linspace(0,10,N))
# Convert to the correct format for matplotlib.
# mdate.epoch2num converts epoch timestamps to the right format for matplotlib
secs = mdate.epoch2num(raw)
fig, ax = plt.subplots()
# Plot the date using plot_date rather than plot
ax.plot_date(secs, vals)
# Choose your xtick format string
date_fmt = '%d-%m-%y %H:%M:%S'
# Use a DateFormatter to set the data to the correct format.
date_formatter = mdate.DateFormatter(date_fmt)
ax.xaxis.set_major_formatter(date_formatter)
# Sets the tick labels diagonal so they fit easier.
fig.autofmt_xdate()
plt.show()
You can change the ticks locations and formats on your plot:
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
import time
secs = [10928389,102928123,383827312,1238248395]
vals = [12,8,4,12]
plt.plot(secs,vals)
plt.gcf().autofmt_xdate()
plt.gca().xaxis.set_major_locator(mtick.FixedLocator(secs))
plt.gca().xaxis.set_major_formatter(
mtick.FuncFormatter(lambda pos,_: time.strftime("%d-%m-%Y %H:%M:%S",time.localtime(pos)))
)
plt.tight_layout()
plt.show()
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.
Can I plot time on the x axis and the numbers on the y-axis using Matplotlib?
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?
Update:
This answer is outdated since matplotlib version 3.5. The plot function now handles datetime data directly. See https://matplotlib.org/3.5.1/api/_as_gen/matplotlib.pyplot.plot_date.html
The use of plot_date is discouraged. This method exists for historic
reasons and may be deprecated in the future.
datetime-like data should directly be plotted using plot.
If you need to plot plain numeric data as Matplotlib date format or
need to set a timezone, call ax.xaxis.axis_date / ax.yaxis.axis_date
before plot. See Axis.axis_date.
Old, outdated answer:
You must first convert your timestamps to Python datetime objects (use datetime.strptime). Then use date2num to convert the dates to matplotlib format.
Plot the dates and values using plot_date:
import matplotlib.pyplot
import matplotlib.dates
from datetime import datetime
x_values = [datetime(2021, 11, 18, 12), datetime(2021, 11, 18, 14), datetime(2021, 11, 18, 16)]
y_values = [1.0, 3.0, 2.0]
dates = matplotlib.dates.date2num(x_values)
matplotlib.pyplot.plot_date(dates, y_values)
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.)
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:
7 years later and this code has helped me.
However, my times still were not showing up correctly.
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.
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.
All thanks to the community.
I had trouble with this using matplotlib version: 2.0.2. Running the example from above I got a centered stacked set of bubbles.
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.
Pandas dataframes haven't been mentioned yet. I wanted to show how these solved my datetime problem. I have datetime to the milisecond 2021-04-01 16:05:37. I am pulling linux/haproxy throughput from /proc so I can really format it however I like. This is nice for feeding data into a live graph animation.
Here's a look at the csv. (Ignore the packets per second column I'm using that in another graph)
head -2 ~/data
date,mbps,pps
2021-04-01 16:05:37,113,9342.00
...
By using print(dataframe.dtype) I can see how the data was read in:
(base) ➜ graphs ./throughput.py
date object
mbps int64
pps float64
dtype: object
Pandas pulls the date string in as "object", which is just type char. Using this as-is in a script:
import matplotlib.pyplot as plt
import pandas as pd
dataframe = pd.read_csv("~/data")
dates = dataframe["date"]
mbps = dataframe["mbps"]
plt.plot(dates, mbps, label="mbps")
plt.title("throughput")
plt.xlabel("time")
plt.ylabel("mbps")
plt.legend()
plt.xticks(rotation=45)
plt.show()
Matplotlib renders all the milisecond time data. I've added plt.xticks(rotation=45) to tilt the dates but it's not what I want. I can convert the date "object" to a datetime64[ns]. Which matplotlib does know how to render.
dataframe["date"] = pd.to_datetime(dataframe["date"])
This time my date is type datetime64[ns]
(base) ➜ graphs ./throughput.py
date datetime64[ns]
mbps int64
pps float64
dtype: object
Same script with 1 line difference.
#!/usr/bin/env python
import matplotlib.pyplot as plt
import pandas as pd
dataframe = pd.read_csv("~/data")
# convert object to datetime64[ns]
dataframe["date"] = pd.to_datetime(dataframe["date"])
dates = dataframe["date"]
mbps = dataframe["mbps"]
plt.plot(dates, mbps, label="mbps")
plt.title("throughput")
plt.xlabel("time")
plt.ylabel("mbps")
plt.legend()
plt.xticks(rotation=45)
plt.show()
This might not have been ideal for your usecase but it might help someone else.