Reputation: 27594
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:
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!
Upvotes: 3
Views: 5602
Reputation: 87376
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
Upvotes: 3