这一切都在 Windows 7 x64 位机器上,在 PyCharm Educational Edition 1.0.1 编译器中运行 python 3.4.3 x64 位。该计划使用的数据取自纽约市的 Citi Bike 计划(数据可在此处找到:http://www.citibikenyc.com/system-data http://www.citibikenyc.com/system-data).
我对数据进行了排序,这样我就有了一个新的 CSV 文件,其中仅包含唯一的自行车 ID 以及每辆自行车的骑行次数(文件名为 Sorted_Bike_Uses.csv)。我正在尝试制作一个图表,其中自行车 ID 与使用次数相对应(自行车 ID 在 x 轴上,使用次数在 y 轴上)。我的代码如下所示:
import pandas as pd
import matplotlib.pyplot as plt
# read in the file and separate it into two lists
a = pd.read_csv('Sorted_Bike_Uses.csv', header=0)
b = a['Bike ID']
c = a['Number of Uses']
# create the graph
plt.plot(b, c)
# label the x and y axes
plt.xlabel('Bicycles', weight='bold', size='large')
plt.ylabel('Number of Rides', weight='bold', size='large')
# format the x and y ticks
plt.xticks(rotation=50, horizontalalignment='right', weight='bold', size='large')
plt.yticks(weight='bold', size='large')
# give it a title
plt.title("Top Ten Bicycles (by # of uses)", weight='bold')
# displays the graph
plt.show()
它创建了一个格式几乎正确的图表。唯一的问题是它对自行车 ID 进行排序,以便它们按数字顺序排列,而不是按使用顺序排列。我尝试过重新利用以前用来制作类似图表的旧代码,但它只是制作了一个更糟糕的图表,不知何故绘制了两组数据。它看起来像这样:
my_plot = a.sort(columns='Number of Uses', ascending=True).plot(kind='bar', legend=None)
# labels the x and y axes
my_plot.set_xlabel('Bicycles')
my_plot.set_ylabel('Number of Rides')
# sets the labels along the x-axis as the names of each liquor
my_plot.set_xticklabels(b, rotation=45, horizontalalignment='right')
# displays the graph
plt.show()
第二组代码使用与第一组代码相同的数据集,并且已对原始代码进行了更改以适合花旗自行车数据。我的 google-fu 已经用完了。我尝试过重新格式化 xticks,将第二个代码的片段添加到第一个代码中,将第一个代码的片段添加到第二个代码中,等等。它可能就在我面前,但我看不到它。任何帮助表示赞赏。