0

I have a problem, In my case I get names from a database, the error occurs when there is a name that contains letter Ñ for example a last name BOLAÑOS, I used # -- coding: utf8 -- but isn't enough. I read that matplotlib needs a file which should contain special characters. Thanks in advance

This is the code:

# -*- coding: utf8 -*-
import matplotlib.pyplot as plt
import networkx as nx
socialNetworl = nx.Graph()
socialNetworl.add_nodes_from([1,2,3,4,5,6])
socialNetworl.add_edges_from([(1,2),(1,3),(2,3),(2,5),(2,6)]) 
labels = {1:'King Bolaños', 2:'Lancelot', 3:'shopkeeper', 4:'dead parrot', 5:'Brian', 6:'Sir Robin'}
nx.draw(socialNetworl, node_size = 800, node_color="cyan", labels=labels, with_labels = True)
plt.show()

1 Answer 1

1

Matplotlib wants Python unicode (for Python2). So you can use

labels = {1:'King Bolaños'.decode('utf-8'), 2:'Lancelot', 3:'shopkeeper', 4:'dead parrot', 5:'Brian', 6:'Sir Robin'}
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your answer, but how can I do when the labels come from a database and I don't know where the letter Ñ appears?.
Make sure you get a unicode object from the database, not a str.
I'm using oracle database and cx_oracle libraries, how can I convert my str to unicode object?
You can just s.decode('utf-8') every string s if you want.

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.