0

After running this code, i get this exception and i didn't found any place to fix it properly

import networkx as nx
from networkx.algorithms import bipartite
import numpy as np
from pandas import DataFrame, concat
import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit 
import ast
import csv
import sys

def plot_degree_dist(G):
in_degrees = G.in_degree()
in_degrees=dict(in_degrees)
in_values = sorted(set(in_degrees.values()))
in_hist = [in_degrees.values().count(x) for x in in_values]
plt.figure() 
plt.grid(True)
plt.loglog(in_values, in_hist, 'ro-') 
plt.plot(out_values, out_hist, 'bv-') 
plt.legend(['In-degree', 'Out-degree'])
plt.xlabel('Degree')
plt.ylabel('Number of nodes')
plt.title('network of places in Cambridge')
#plt.xlim([0, 2*10**2])

I expect to receive a proper graph but all i get is this warning

  File "<ipython-input-32-f89b896484d7>", line 2
    in_degrees = G.in_degree()
             ^
IndentationError: expected an indented block
Amit Mek
  • 105
  • 1
  • 1
  • 5

1 Answers1

2

Python relies on proper indentation to identify function blocks. This code should work:

import networkx as nx
from networkx.algorithms import bipartite
import numpy as np
from pandas import DataFrame, concat
import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit 
import ast
import csv
import sys

def plot_degree_dist(G):
    in_degrees = G.in_degree()
    in_degrees=dict(in_degrees)
    in_values = sorted(set(in_degrees.values()))
    in_hist = [in_degrees.values().count(x) for x in in_values]
    plt.figure() 
    plt.grid(True)
    plt.loglog(in_values, in_hist, 'ro-') 
    plt.plot(out_values, out_hist, 'bv-') 
    plt.legend(['In-degree', 'Out-degree'])
    plt.xlabel('Degree')
    plt.ylabel('Number of nodes')
    plt.title('network of places in Cambridge')
    #plt.xlim([0, 2*10**2])

Basically just indent it by 2 or 4 spaces as per your style requirements.

Sashi
  • 2,659
  • 5
  • 26
  • 38
  • Thanks a lot, it worked! No i got another Exception .. AttributeError: 'dict_values' object has no attribute 'count' – Amit Mek Dec 27 '18 at 18:34
  • Great. Please feel free to mark my answer as correct so it can be useful for future users who have similar problems! – Sashi Dec 27 '18 at 18:35
  • Just did! :) Think you know what could be the solution of the new exception ? – Amit Mek Dec 27 '18 at 18:37
  • Which line are you getting it on? I'm having trouble finding dict_values object. Would it be possible to create a new question for this and also share the error message? Official stackoverflow policy is that each question should only contain one problem! :) – Sashi Dec 27 '18 at 18:39
  • Great. Share a link to the question once you create it and lets get this sorted! – Sashi Dec 27 '18 at 18:43
  • It seems that i will be able to do it only an hour from now but maybe till then i'll get along! If not, i'll post it here! So again - Thank you ! – Amit Mek Dec 27 '18 at 18:56