0

I am trying to plot a python dictionary with {'file/path':number of files matching pattern}. according to Plotting a python dict in order of key values this should work however I also realize that my x values are not an integer so this might be the case but I am not able to find an example where x axis is a string and y is number corresponding to it,

code

import os
import re

import numpy as np
import matplotlib.pyplot as plt


def find_pattern(root_dir,keyword):
    out = {}
    pattern = re.compile(keyword)
    for filepath, subdirs, files in os.walk(root_dir):
        for filename in files:
            if pattern.match(filename):
                if filepath not in out:
                    out[filepath] = 1
                else:
                    out[filepath] = out[filepath] + 1
    width = 1.0
    print(out)
    plt.bar(out.keys(), out.values(), width, color='g')
    plt.show()
    return out

find_pattern('/Users/vector8188/AlgorithmAnalysisPython', '^(.(?!.*\.css$|.*\.html))*$')`

content of out dictionary

`out = {'/Users/vector8188/AlgorithmAnalysisPython/.git/objects/28': 1, '/Users/vector8188/AlgorithmAnalysisPython/.git/objects/9a': 1, '/Users/vector8188/AlgorithmAnalysisPython/.git/objects/22': 1
}`

error:

Traceback (most recent call last):
  File "apple_test.py", line 70, in <module>
    find_pattern('/Users/vector8188/AlgorithmAnalysisPython', '^(.(?!.*\.css$|.*\.html))*$')
  File "apple_test.py", line 66, in find_pattern
    plt.bar(out.keys(), out.values(), width, color='g')
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/pyplot.py", line 2515, in bar
    ret = ax.bar(left, height, width=width, bottom=bottom, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.py", line 5053, in bar
    self.add_patch(r)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.py", line 1562, in add_patch
    self._update_patch_limits(p)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.py", line 1580, in _update_patch_limits
    xys = patch.get_patch_transform().transform(vertices)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/patches.py", line 576, in get_patch_transform
    self._update_patch_transform()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/patches.py", line 569, in _update_patch_transform
    bbox = transforms.Bbox.from_bounds(x, y, width, height)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/transforms.py", line 821, in from_bounds
    return Bbox.from_extents(x0, y0, x0 + width, y0 + height)
TypeError: cannot concatenate 'str' and 'float' objects

2 Answers 2

1

You are running code that is written for python 2 in python 3.

In python 3 you should convert the keys and values to a list first:

plt.bar(list(out.keys()), list(out.values()), width, color='g', ec="k")
1
  • I have made some changes which address issues you mentioned
    – vector8188
    Commented Oct 20, 2018 at 19:50
0

Bar needs x-coordinates. For

out = {'/Users/vector8188/AlgorithmAnalysisPython/.git/objects/28': 1, '/Users/vector8188/AlgorithmAnalysisPython/.git/objects/9a': 1, '/Users/vector8188/AlgorithmAnalysisPython/.git/objects/22': 1
} 

Try this instead:

w = 1
print(out)

x_axis = np.arange(0, len(out.keys())*w, w) 

fig, ax = plt.subplots(1)
ax.bar(x_axis, out.values(), width = w, color='g', align='center')
ax.set_xticks(x_axis)
ax.set_xticklabels(out.keys(), rotation=90)
plt.show()
3
  • I do not think we need to initialize to zero as its initialize to 1 if filename does not exists to begin with and it has been seen for the first time, please keep in mind I am only interested in filename which satisfies my regex criterion.
    – vector8188
    Commented Oct 20, 2018 at 17:47
  • Totally misunderstood. Sorry. According to matplotlib.org/api/_as_gen/matplotlib.pyplot.bar.html, bar needs to plot scalar values but in your case they are represented as strings. Will update my answer.
    – BernardL
    Commented Oct 20, 2018 at 18:00
  • these line did the trick, plt.bar(range(len(out)), list(out.values()), align='center') plt.xticks(range(len(out)), list(out.keys()), rotation='vertical') even though I found this answer way before I looked at your comment/answer, I will accept your answer as it's very similar to my current code.
    – vector8188
    Commented Oct 20, 2018 at 19:46

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.