Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'str' object has no attribute 'zorder' #9471

Closed
nstoeckigt opened this issue Oct 18, 2017 · 3 comments
Closed

AttributeError: 'str' object has no attribute 'zorder' #9471

nstoeckigt opened this issue Oct 18, 2017 · 3 comments

Comments

@nstoeckigt
Copy link

nstoeckigt commented Oct 18, 2017

Bug report

I try to create a plot from temperatures over datetimes by using an numpy.array. The plot should only be saved as PNG to be display via a servers webserver.

The data is retrieved from a logfile that looks like:

10.09.2017 08:43:01 - 32°C
10.09.2017 10:04:01 - 33°C
10.09.2017 12:32:01 - 31°C

Code for reproduction

The Code is highly inspired by → https://matplotlib.org/gallery/api/date.html#sphx-glr-gallery-api-date-py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import matplotlib 
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime   
import numpy as np
import re
import os


def getDataFromLog():
    hddData = np.empty((0,), dtype=[('dates', 'datetime64[s]'), ('temperature', 'int64')])

    with open('/var/log/hddtemp.log', 'r') as f:
        for line in f:
            (hd, ht) = line.strip().split(' - ')

            hd = datetime.datetime.strptime(hd, '%d.%m.%Y %H:%M:%S')
            ht = int(re.findall(r'\d+', ht)[0].strip())

            hddEntry = np.array([(hd, ht)], dtype=hddData.dtype)
            hddData = np.hstack((hddData, hddEntry))

    # plt.ioff()

    years = mdates.YearLocator()   # every year
    months = mdates.MonthLocator()  # every month
    days = mdates.DayLocator()  # every day
    dayFmt = mdates.DateFormatter('%d.%m.%Y')
    monthFmt = mdates.DateFormatter('%b.%Y')
    yearFmt = mdates.DateFormatter('%Y')

    Date = hddData['dates'].astype('O')

    Fig = plt.figure()
    Ax = plt.subplot()
    Ax.plot(Date)

    Ax.xaxis.set_major_locator(months)
    Ax.xaxis.set_major_formatter(monthFmt)
    Ax.xaxis.set_minor_locator(days)

    minDate = Date.min()
    maxDate = Date.max()

    datemin = datetime.date(minDate.year, minDate.month, 1)
    datemax = datetime.date(maxDate.year, maxDate.month + 1, 1)
    Ax.set_xlim(datemin, datemax)

    # format the coords message box
    def temperature(x):
        return '$%d' % x
    Ax.format_xdata = mdates.DateFormatter('%d.%m.%Y %H:%M')
    Ax.format_ydata = temperature

    #Ax.ylabel('temperature')
    Ax.title = "HDD Temperature"
    Ax.grid(True) 

    Fig.autofmt_xdate()
    plt.show()
    #plt.savefig('hddtemp.png')
    Fig.savefig('hddtemp.png')
    plt.close(Fig)


def main():
    getDataFromLog()

if __name__ == "__main__":
    main()

Actual outcome

Traceback (most recent call last):
  File "./draw_hddtemp.py", line 75, in <module>
    main()
  File "./draw_hddtemp.py", line 72, in main
    getDataFromLog()
  File "./draw_hddtemp.py", line 67, in getDataFromLog
    Fig.savefig('hddtemp_f.png')
  File "/usr/lib/python2.7/dist-packages/matplotlib/figure.py", line 1572, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 2244, in print_figure
    **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.py", line 545, in print_png
    FigureCanvasAgg.draw(self)
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.py", line 464, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/python2.7/dist-packages/matplotlib/artist.py", line 63, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/figure.py", line 1143, in draw
    renderer, self, dsu, self.suppressComposite)
  File "/usr/lib/python2.7/dist-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images
    a.draw(renderer)
  File "/usr/lib/python2.7/dist-packages/matplotlib/artist.py", line 63, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 2381, in draw
    dsu = [(a.zorder, a) for a in artists]
AttributeError: 'str' object has no attribute 'zorder'

Expected outcome

A PNG file containing the plot corresponding to the temperatures from the log.

Matplotlib version

  • Operating system: Debian GNU/Linux 9.2 (stretch)
  • Matplotlib version: 2.0.0+dfsg1-2
  • Matplotlib backend (print(matplotlib.get_backend())): u'TkAgg'
  • Python version: 2.7.13
  • Jupyter version (if applicable): ---
  • Other libraries: numpy:1.12.1

apt install python-matplotlib python-matplotlib-data

@WeatherGod
Copy link
Member

WeatherGod commented Oct 18, 2017 via email

@WeatherGod
Copy link
Member

Also, I should note that this code will break in December because you are adding 1 to the month. Use timedelta instead for doing date math.

@jklymak
Copy link
Member

jklymak commented Oct 18, 2017

...also please try and minimize your example before writing an issue. You can often find the error that way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants