Separating code from for function while using values within for function in python
Sorry for the confusing title :s
for x in frequency:
alphab = [x]
frequencies = [frequency[x]]
print alphab, frequencies
How would I be able to separate the below code from the above code whilst still using the output from for x in frequency:
If I run what's here, the histogram is opened for each value of x rather than the whole stri开发者_如何学Gong.
If I indent anything below, as seen here, the histogram only runs for the first value of x as well.
Is there any possible way to use the entire string without having the histogram function indented within for
pos = np.arange(len(alphab))
width = 1.0
ax = plt.axes()
ax.set_xticks(pos + (width / 2))
ax.set_xticklabels(alphab)
plt.xlabel('Letter')
plt.ylabel('Absolute Frequency')
plt.title("Absolute Frequency of letters in text")
plt.bar(pos, frequencies, width, color='r')
plt.show()
def plotfreq(frequency, alphab):
pos = np.arange(len(alphab))
width = 1.0
ax = plt.axes()
ax.set_xticks(pos + (width / 2))
ax.set_xticklabels(alphab)
plt.xlabel('Letter')
plt.ylabel('Absolute Frequency')
plt.title("Absolute Frequency of letters in text")
plt.bar(pos, frequency, width, color='r')
plt.show()
for x in frequencies:
plotfreq(x, frequencies[x])
Is this something you were looking for ?
I guess you want to populate the arrays before calling the plot function, e.g.
alphab = []
frequencies = []
for x in frequency:
alphab.append(x)
frequencies.append(frequency[x])
# .. some more code here ..
plt.bar(pos, frequencies, width, color='r')
精彩评论