3

I have a dictionary aggregate_dict that has letters a:z as keys, with each key having a list containing exactly 11 numerical values between 0 and 1 as its associated value. I wish to use this data to create a scatter plot with the letters in the alphabet arranged from a to z on the x axis, and each of the values for the given letter on the y axis, like this:

enter image description here

I normally hash this out in R in a second, but I don't have R on the machine I'm working on and I don't have admin rights, so I'm left to do it in Python. I'm trying to go about this in the following way (with simplified dict below):

from matplotlib import pyplot

def scatterplot(x,y):
    pyplot.plot(x,y,'b.')
    pyplot.ylim(0,1)
    pyplot.title("Relative Frequency of Letters")
    pyplot.show()

x_vals = []
y_vals = []

aggregate_dict = {'a':[.1,.2,.3],'b':[.2,.3,.4]}

for u in aggregate_dict:
    x_vals.append(u)
    y_vals.append(aggregate_dict[u])

scatterplot( x_vals, y_vals )

Unfortunately, this approach yields the following trace:

Traceback (most recent call last):
  File "C:/Users/dduhaime/Desktop/letter_distributions_each_section_tale.py", line 74, in <module>
    scatterplot(x_vals, y_vals )
  File "C:/Users/dduhaime/Desktop/letter_distributions_each_section_tale.py", line 56, in scatterplot
    pyplot.plot(x,y,'b.')
  File "C:\Python27\ArcGIS10.2\lib\site-packages\matplotlib\pyplot.py", line 2987, in plot
    ret = ax.plot(*args, **kwargs)
  File "C:\Python27\ArcGIS10.2\lib\site-packages\matplotlib\axes.py", line 4138, in plot
    self.add_line(line)
  File "C:\Python27\ArcGIS10.2\lib\site-packages\matplotlib\axes.py", line 1497, in add_line
    self._update_line_limits(line)
  File "C:\Python27\ArcGIS10.2\lib\site-packages\matplotlib\axes.py", line 1508, in _update_line_limits
    path = line.get_path()
  File "C:\Python27\ArcGIS10.2\lib\site-packages\matplotlib\lines.py", line 743, in get_path
    self.recache()
  File "C:\Python27\ArcGIS10.2\lib\site-packages\matplotlib\lines.py", line 420, in recache
    x = np.asarray(xconv, np.float_)
  File "C:\Python27\ArcGIS10.2\lib\site-packages\numpy\core\numeric.py", line 320, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: could not convert string to float: a

Does anyone know how I might be able to resolve this error and carry on with my plot? I would be grateful for any advice you can offer!

1 Answer 1

3
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter, MultipleLocator

x_data = np.arange(97, 110)
y_data = np.random.rand(len(x_data))

def ord_to_char(v, p=None):
    return chr(int(v))

fig, ax = plt.subplots()
ax.plot(x_data, y_data, 'x')
ax.xaxis.set_major_formatter(FuncFormatter(ord_to_char))
ax.xaxis.set_major_locator(MultipleLocator(1))
plt.show()

Relevant doc: http://matplotlib.org/api/ticker_api.html#module-matplotlib.ticker

Sign up to request clarification or add additional context in comments.

4 Comments

excellent @tcaswell! Is there a way to plot multiple values for each tick or value on the x-axis, so that there will be 3+ values for each letter on the x axis?
plot puts a point at every (x, y) pair, just tripple up x values (so it looks like x = [97, 97, 97, 98, 98, 98] and y = range(6)
perfect--got me there! I didn't realize how easy it is to convert str to int with ord()! What a useful method! Thank you!
Sorry, should have included that detail

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.