1

When I execute this code:

plt.figure(figsize=(12,8))
ax = sns.heatmap(result, annot=True, linewidths=.5) #YlGnBu
plt.ylabel('Hour', fontsize=12)
plt.xlabel('Id', fontsize=12)
plt.xticks(rotation='vertical')
plt.show()

I get this error:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 11: ordinal not in range(128)

Cannot understand how to resolve this issue.

This is an extract from my dataframe df:

df =
    Id  Hour    FREQ
    1   17      19
    1   18      16
    1   19      2
    2   17      49

Then I create a pivot table result as follows:

result = df.pivot("Hour", "CameraId", "FREQ")

UPDATE:

The dataframe df was obtained as follows (if it may help finding an issue):

df = df_original.groupby([df_original['Id'], df_original['Hour'], df_original['Date']]).size()
#groupby by ID and hours
df1 = df.groupby(level=[0,1]).mean()
mux = pd.MultiIndex.from_product([df1.index.levels[0], df1.index.levels[1]],names=df1.index.names)
df = df1.reindex(mux, fill_value=0).reset_index(name='FREQ')

norm = plt.Normalize(df["FREQ"].values.min(), df["FREQ"].values.max())
colors = plt.cm.YlGnBu(norm(gr_vol_1["FREQ"]))
Dinosaurius
  • 8,306
  • 19
  • 64
  • 113
  • Same question: https://stackoverflow.com/questions/26979948/unicodedecodeerror-ascii-codec-cant-decode-byte-0xc3-in-position – DYZ Jul 03 '17 at 18:58
  • 1
    The answer in the linked thread is the following: `The problem is that the ntlk version is not compatabile with the python version, so it requires an older version of the nltk toolkit.` But I am not using `nltk`. – Dinosaurius Jul 03 '17 at 19:04
  • 1
    What python version are you using? How are you reading the dataframe? Also the extract of df does not show all fields. Try using "encoding='utf-8'" in your pandas read method. – Oleg Medvedyev Jul 03 '17 at 19:08
  • @omdv: I am using Python 2.7. I successfully apply the same code of creating a heatmap to another data frame. So, I have an original dataframe and then I do some groupings. The problem arises with dataframe obtained by one of groupby statements. I will publish it now. – Dinosaurius Jul 03 '17 at 19:15
  • 2
    What I meant is where do you get the original dataframe from? This is the unicode error, which means you are not encoding your input properly. E.g. if you use pd.read_csv to read the df_original try using read_csv(..., encoding="utf-8") – Oleg Medvedyev Jul 03 '17 at 19:26
  • 1
    To add to @omdv's point, it's likely that the string being read is encoded in utf-8. In utf-8, 0xc3 is the leading byte for a range of two-byte encodings of [vowels with common diacritical marks](http://www.utf8-chartable.de/unicode-utf8-table.pl). In other words, your input probably has an accented character somewhere, but Python is expecting plain ascii characters. – senderle Jul 03 '17 at 19:30
  • @senderle: Thanks a lot. Actually I had an accent in a title of the chart. – Dinosaurius Jul 03 '17 at 19:40

0 Answers0