python - using input from file to define colour of bar in plot (matplotlib) -


i trying plot bar diagram using input .csv file.

my input file contains several columns sample names , data , column colour of sample. column contains strings, such 'w' meaning white, 'b' meaning brown , forth. each line contains values different sample.

now want bar each sample have colour specified in colour column.

my code looks bit this:

import numpy np import matplotlib.pyplot plt  results_dtype=np.dtype([('name', 's100'), ('colour', 's10'), ('data_this', 'float64'), ('data_that', 'float64'), ...])   data = np.genfromtxt('c:/data.csv', delimiter = ',', dtype =results_dtype, filling_values=np.nan, skip_header=1)  colours = {'w':'#ffffff', 'y':'#ffff00', 'b':'#cc8033', 'p':'#cc79a7'}  fig = plt.figure() plt.bar(np.arange(len(data)), data['data_this'], bottom=data['data_that'], align='center', color=colours[data['colour']]) plt.xticks(np.arange(len(data)), data['name'], rotation='vertical') plt.show() 

the error message following:

unhashable type: 'numpy.ndarray' (pointing plt.bar(...) line).

it sounds i'm calling dictionary wrong way or along lines can't figure out how properly.

i hope, explains i'm trying do.

python's dicts can indexed single key. don't allow "vectorized" indexing.

let's use simplified example gives same error:

import numpy np  lookup = {'a':1, 'b':2, 'c':3} values = np.array(['a', 'b', 'c', 'c', 'a', 'b', 'a']) data = lookup[values] 

which yields:

typeerror                                 traceback (most recent call last) <ipython-input-71-7d8663a08b8d> in <module>()       1 lookup = {'a':1, 'b':2, 'c':3}       2 values = np.array(['a', 'b', 'c', 'c', 'a', 'b', 'a']) ----> 3 data = lookup[values]  typeerror: unhashable type: 'numpy.ndarray' 

the exact error because we're trying use mutable type key dict. there sequences (e.g. tuples) use key, still wouldn't work in way want them to.

therefore, instead of using sequence of keys such data = lookup[values], you'll need use list comprehension:

data = [lookup[item] item in values] 

putting original example:

import numpy np import matplotlib.pyplot plt  results_dtype=np.dtype([('name', 's100'), ('colour', 's10'), ('data_this', 'float64'), ('data_that', 'float64'), ...])   data = np.genfromtxt('c:/data.csv', delimiter = ',', dtype=results_dtype,                       filling_values=np.nan, skip_header=1)  colours = {'w':'#ffffff',            'y':'#ffff00',            'b':'#cc8033',            'p':'#cc79a7'}  color = [colours[item] item in data['colour']]  fig = plt.figure() plt.bar(np.arange(len(data)), data['data_this'], bottom=data['data_that'],           align='center', color=color) plt.xticks(np.arange(len(data)), data['name'], rotation='vertical') plt.show() 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -