Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8417433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:02:50+00:00 2026-06-10T02:02:50+00:00

A new day brought new problem with Python, unfortunately :/ I have a file

  • 0

A new day brought new problem with Python, unfortunately :/

I have a file generated by my other app written in Java. This app generates files with some data, it’s kind a random stuff ’cause I’m not able to say how many rows each file would have. Example file looks like this:

3   Sat Jan 21 00:00:00 2012
7   Sun Mar 11 00:00:00 2012
5   Fri Jan  1 00:00:00 2010
4   Sat Feb  5 00:00:00 2011
8   Sun Apr 11 00:00:00 2010
4   Wed Aug 24 00:00:00 2011
8   Sat Feb 20 00:00:00 2010
3   Thu Oct 13 00:00:00 2011
9   Fri Dec 17 00:00:00 2010
4   Tue Jul 20 00:00:00 2010
8   Fri Dec  2 00:00:00 2011
6   Mon May 31 00:00:00 2010
5   Mon May 16 00:00:00 2011
8   Mon Apr 30 00:00:00 2012
3   Thu Oct 28 00:00:00 2010
1   Tue Jun 19 00:00:00 2012
7   Wed Sep  8 00:00:00 2010

And I want to draw a chart with use of this data. On X axis I would like to have formatted dates, and on Y axis numbers from the first column of my file. Heres my lovely python code:

# -*- coding: utf-8 -*-
#!/usr/bin/env python
import wx
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as pl
import datetime

def monthNum(month) :
        if month == "Jan" :
            return 1
        elif month == "Feb" :
            return 2
        elif month == "Mar" :
            return 3
        elif month == "Apr" :
            return 4
        elif month == "May" :
            return 5
        elif month == "Jun" :
            return 6
        elif month == "Jul" :
            return 7
        elif month == "Aug" :
            return 8
        elif month == "Sep" :
            return 9
        elif month == "Oct" :
            return 10
        elif month == "Nov" :
            return 11
        elif month == "Dec" :
            return 12

def convertDate(dateTime) :
        line = dateTime.split(' ')
        date = (str(line[2]) + "-" + str(monthNum(line[1])) + "-" + str(line[4]))
        return date

def readFile(filename) :
        values = []
        dates = []
        try :
                with open(filename, "r") as openedFile:
                        for line in openedFile :
                                line = line.strip()
                                data = line.split("\t")
                                values.append(int(data[0]))
                                newDate = convertDate(data[1])
                                dates.append(datetime.datetime.strptime(newDate, "%d-%m-%Y").date())
        except IOError :
                print("IOERROR")
        except ValueError :
                print("VALUE ERROR")
        if len(values) != 0 and len(dates) != 0 :
                drawChart(values, dates, filename)

def drawChart(values, dates, filename):
        fig = pl.figure(dpi=60,figsize=(18, 10))
        ax = fig.add_subplot(1,1,1)
        fig.subplots_adjust(left=0.1, right=0.95, top=0.95, bottom=0.2)
        ax.bar(range(len(dates)), values, facecolor='#777777', align='center', width=0.5, ecolor='black')
        pl.axis('tight')
        ax.set_xticks(range(len(dates)))
        pl.yticks(values)
        ax.set_xticklabels(dates, rotation = 90)
        pl.savefig(filename + ".png")
        pl.show()
        pl.close()

readFile("file.txt")

Everything is fine, if file.txt had a one, single row. When it has more rows, python code gives me an error:

VALUE ERROR
Traceback (most recent call last):
  File "test.py", line 71, in <module>
    readFile("file.txt")
  File "test.py", line 56, in readFile
    drawChart(values, dates, filename)
  File "test.py", line 62, in drawChart
    ax.bar(range(len(dates)), values, facecolor='#777777', align='center', width=0.5, ecolor='black')
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 4733, in bar
    nbars)
AssertionError: incompatible sizes: argument 'height' must be length 2 or scalar

And I don’t really know how to fix it. Its fine, if file.txt had one row but as I wrote earlier: I’m not able to say how many rows would file.txt have (it depends on my Java app).

Anybody? I use Python 2.7 and Matplotlib on Kubuntu 12.04.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:02:52+00:00Added an answer on June 10, 2026 at 2:02 am

    It’s because dates only has 2 values. The length of dates and the length of values must be the same for matplotlib to know what to do. If values was a scalar then all the bars would have the same height

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A new day, a new question! I have been working on an app that
hi i'm using log4net for logging my website. Every day a new file is
Good day! I'm new in using Java DB (Derby). I want to embed it
I have been slowing learning and building my first android app. I'm VERY new
Everyday is a new day with Symfony, but I'm loving it! This morning I
I have an object called 'SelectedDay' I instantiate it like this: Day SelectedDay =
I'm scratching my head over this. I have a moderately successful app which has
New day, new problem :-) Code: Client Side: void abw_Closed(object sender, EventArgs e) {
I am new to programming (day 3) and I spent the last two days
Good day, After play new todo (new scala project) -> cd todo -> play

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.