我试图让x轴具有刻度线的范围,而不仅仅是单个数字。我从用户的列表中获取输入,并希望将该列表分成相等的部分。 (a_list
是用户输入列表)。
现在崩溃了。
import pylab
import numpy
def bar_graph(a_list):
'''bar graph of number-frequency, xaxis labeled with ranges'''
y_values = a_list.split(',')
# get ticks as ranges
bar_width=2
x_values = numpy.arange[(max-min)/10]-1
xvalues_ticked = x_values+bar_width/2.0
pylab.xticks(xvalues_ticked,a_list)
# create the bar graph
pylab.bar(x_values,y_values,width=bar_width,color='r')
pylab.xlabel("Ranges")
pylab.ylabel("Frequency")
pylab.title("Frequency of Numbers")
pylab.plot(x_values,value_list,color='b')
pylab.grid(True)
pylab.show()
#Create number list
a_list = [1,5,10,20,25,55,30,70,45,15]
bar_graph(a_list)
答案 0 :(得分:0)
这是我对你要做的事情的最好猜测:
import matplotlib.pyplot as plt
import numpy as np
def bar_graph(ax, a_list):
'''bar graph of number-frequency, xaxis labeled with ranges'''
if isinstance(a_list, str):
a_list = [float(_) for _ in a_list.split(',')]
y_values = a_list
x_values = np.arange(len(y_values)) * 2
# get ticks as ranges
bar_width = 2
# create the bar graph
ax.bar(x_values, y_values, width=bar_width, color='r', align='center')
ax.xaxis.set_ticks(x_values)
ax.xaxis.set_ticklabels(['{}-{}'.format(x, x+bar_width) for x in x_values])
ax.set_xlabel("Ranges")
ax.set_ylabel("Frequency")
ax.set_title("Frequency of Numbers")
ax.plot(x_values, y_values, color='b')
ax.grid(True)
a_list = [1,5,10,20,25,55,30,70,45,15]
fig, ax = plt.subplots()
bar_graph(ax, a_list)