2

I am building a tool that reads a uft-8 encoded text file with Pandas and displays the field on Matplotlib. Think of it as a poor man's telemetry display for training purposes. I am having an issue that the code is not displaying fields that are in Cyrillic but it is able to display other fields from the txt file. The plot does show Cyrillic when the field is 'hardcoded' into the plot.

Here is the code (you'll probably see I am an newby at this stuff):

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt # Plotting library
import numpy as np # Numpy library for arrays
import matplotlib.cbook as cbook
import pandas as pd
from pylab import *
import matplotlib as mpl
from pylab import rcParams
import matplotlib.cm as cm
from pandas import set_option
set_option("display.max_rows", 10) #Allows to control how Pandas displays the result

# Set image size
rcParams['figure.figsize'] = 12, 10

# Read the rndz telemetry file with pandas
filename = "data.txt"
data = pd.read_table(filename, sep="\s+")

# 361 is the size of the txt file
for i in range(361):

    plt.clf() # Clears the screen

    plt.xlim(0.5,8) # Set the x-limits on the display
    plt.ylim(1,7) # Set the y-limits on the display

    plt.text(5.9,6.70,str(data.one[i])) # Adds regular text

    #This field is the one giving me trouble
    plt.text(5.9,6.40,str(data.two[i]) # Add cyrillic text

    #The field below is displayed properly on the plot 
    plt.text(1.25,6.7,u'заглавие', color="#ffffff", fontdict=font) # Add title

    xticks([]), yticks([]) # Erase the axes

    plt.show() # Render the plot
    plt.pause(1) # Wait before the next loop executes

If I comment the display of the data.two field, the code runs fine. However, when the code is asked to plot the text from that field, which is the Cyrillic text, here is what I am getting:

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

I am running Enthough Canopy Python 2.7 in Windows 7. Any thoughts on what is wrong?

fonsi
  • 471
  • 8
  • 22

1 Answers1

1

Read back your utf8 encoded file by passing the encoding= argument to the read_table function:

data = pd.read_table(filename, sep="\s+", encoding="utf-8")
Zeugma
  • 31,231
  • 9
  • 69
  • 81
  • Thanks. That helped. I also found [this thread](http://stackoverflow.com/questions/28544686/unicodeencodeerror-ascii-codec-cant-encode-characters-in-position-0-5-ordin) that helped me set the default encoding in the script – fonsi Apr 06 '16 at 16:00