-2

I'm a beginner in python, and i'm trying to draw a graph using:

`nx.write_dot(G, "%s.dot"%(image))`

in a defined function. When I excute the program, I'm getting this error:

File "sim.py", line 31, in <module>
    main()

File "sim.py", line 30, in main
    sol.run() 

File "C:\Python27\My sim\Solution.py", line 221, in run
    self.drawGraph(G, "solution1")

File "C:\Python27\My sim\Solution.py", line 227, in drawGraph
    nx.write_dot(G, "%s.dot"%(image))

File "<decorator-gen-232>", line 2, in write_dot

File "C:\Python27\lib\site-packages\networkx\utils\decorators.py", line 220, in _open_file

result = func(*new_args, **kwargs)

File "C:\Python27\lib\site-packages\networkx\drawing\nx_pydot.py", line 58, in write_dot

P=to_pydot(G)

File "C:\Python27\lib\site-packages\networkx\drawing\nx_pydot.py", line 197, in to_pydot

P = pydot.Dot(graph_type=graph_type,strict=strict,**graph_defaults)
AttributeError: 'module' object has no attribute 'Dot'

It seems it is a Windows os problem (I'm on win7), because my colleague can run the same script on his ubuntu machine without any error.

Thanks for any help!

Betty
  • 237
  • 6
  • 16

1 Answers1

0

you are doing the tutorial Drawing graphs right?

here how it work:

import networkx as nx
import matplotlib.pyplot as plt

G=nx.Graph()
G.add_edges_from([(1,2),(1,3)])
nx.draw(G)
plt.show()

Edit: if you didn't install matplotlib, just open command line and type:

pip install matplotlib

matplotlib is optional, it doesn't come with networkx, you have to install it.

also for saving the .dot file just add this line:

nx.write_dot(G,'C:/file.dot')

Edit: without matplotlib will be like so:

import networkx as nx

G=nx.Graph()
G.add_edges_from([(1,2),(1,3)])
nx.draw(G)
nx.write_dot(G,'C:/file.dot')

I notice in your code nx.write_dot(G, "%s.dot"%(image)) you didn't define image and G, the error should be from one of those.

but if you want to install c++ compiler, i suggest downloading Visual C++ Compiler 33mb or Microsoft visual studio community, they are free.

Bear
  • 550
  • 9
  • 25
  • I have tried this method, but when I wanted to install matplotlib, it failed because it requires visual c++ if I remember wellans I don't have much space on my machine to install new softwares; so i'm dealing with my_dot problem. Thanks! – Betty Jan 27 '16 at 11:13
  • @Betty i see... you can just ignore `plt` on 2nd and 7th line. – Bear Jan 27 '16 at 11:20
  • G and image are my function attributes, so when i'm calling this function, there is the error. I used your last code propostion, but it has an error: ImportError: Matplotlib required for draw().I think I'll try to free some space and install visual c++.Thanks for your interaction! – Betty Jan 27 '16 at 12:04
  • @Betty `draw()` is one of `networkx` functions, it shouldn't show `Matplotlib` error. after importing `networkx`, you can check list of functions with `dir (networkx)`. – Bear Jan 27 '16 at 12:13
  • yes, there is `draw()` function, but can't understand why it said it requires `Matplotlib` ?!! – Betty Jan 27 '16 at 12:35
  • @Betty: maybe try to edit your question and put your main source code in there or link of it to your github / bitbucket / gitlab or something. – Bear Jan 27 '16 at 12:44
  • 1
    "but can't understand why it said it requires Matplotlib". `draw` requires matplotlib because it builds on matplotlib commands to draw the graph. If you don't have matplotlib, you're not going to be able to draw much of anything with python. – Joel Jan 28 '16 at 06:27
  • 1
    @bear - if you don't have matplotlib, `draw` won't work. Here's the [source](https://networkx.github.io/documentation/latest/_modules/networkx/drawing/nx_pylab.html#draw) – Joel Jan 28 '16 at 06:29
  • @Joel: you are right, i just uninstall the **matplotlib** and try the code, it show `ImportError: Matplotlib required for draw()` that's my bad, thanks for pointing it out. – Bear Jan 28 '16 at 07:15