plot time on X axis
Peter Pearson
pkpearson at nowhere.invalid
Wed Jun 7 12:56:31 EDT 2017
On Wed, 07 Jun 2017 09:20:25 -0300, jorge.conrado at cptec.inpe.br wrote:
[snip]
> I was an IDL user and I'm using Python. I have several meteorological
> daily time seriee for several years. Please can someone help me. I would
> like to plot on X axis only the values o the year.
Is matplotlib allowed? Example (Python 2.7)(Note that most of
this example is just parsing data, not plotting:):
from matplotlib import pyplot as plt
from datetime import date
import re
dwcd = """
2014-06-01: 82.00% ( 1) 89.50% ( 2) 39.00% ( 1) 0.259
2014-07-01: 100.00% ( 1) 89.00% ( 3) 0.00% ( 1) 0.264
2014-08-01: 79.50% ( 2) 85.50% ( 4) 53.00% ( 1) 0.273
2014-09-01: 85.00% ( 3) 98.00% ( 6) 87.00% ( 3) 0.495
2014-10-01: 86.00% ( 7) 97.00% (10) 82.50% ( 4) 0.553
2014-11-01: 93.50% (10) 98.50% (10) 39.00% ( 6) 0.215
2014-12-01: 97.00% (10) 100.00% (10) 66.50% ( 6) 0.025
2015-01-01: 72.50% (12) 94.00% (11) 39.00% ( 6) 0.025
2015-02-01: 66.00% (12) 88.50% (12) 58.50% ( 8) 0.248
2015-03-01: 79.00% (15) 95.50% (12) 77.00% ( 9) 0.360
2015-04-01: 87.00% (15) 95.50% (12) 68.00% ( 9) 0.039
2015-05-01: 85.50% (18) 90.00% (12) 87.00% ( 9) 0.479
"""
def get_time(r):
r = r.strip()
return date(year=int(r[0:4]),
month=int(r[5:7]),
day=int(r[8:10]))
def get_field(r, field_name):
m = re.match(r"(?P<date>[^:]+): +"
r"(?P<value_a>[0-9.]+)% +"
r"\( *(?P<n_a>[0-9]+)\) +"
r"(?P<value_b>[0-9.]+)% +"
r"\( *(?P<n_b>[0-9]+)\) +"
r"(?P<value_c>[0-9.]+)% +"
r"\( *(?P<n_c>[0-9]+)\) +"
r"(?P<p>[0-9.]+)", r.strip())
return m.group(field_name)
x = [get_time(r) for r in dwcd.split("\n") if r]
y_a = [float(get_field(r, "value_a")) for r in dwcd.split("\n") if r]
y_b = [float(get_field(r, "value_b")) for r in dwcd.split("\n") if r]
y_c = [float(get_field(r, "value_c")) for r in dwcd.split("\n") if r]
plt.plot(x, y_a, color="red", label="Group A")
plt.plot(x, y_b, color="green", label="Group B")
plt.plot(x, y_c, color="blue", label="Group C")
plt.plot(date(2015,5,20), 101, marker="x", color="white")
plt.ylabel("Y label")
plt.legend(loc="upper left")
fig = plt.gcf()
fig.autofmt_xdate()
plt.show()
--
To email me, substitute nowhere->runbox, invalid->com.
More information about the Python-list
mailing list