pyplot x轴正在排序 [英] pyplot x-axis being sorted

查看:96
本文介绍了pyplot x轴正在排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一切都是在Windows 7 x64位计算机上运行的,该计算机运行python 3.4.3 x64位,在PyCharm Educational Edition 1.0.1编译器中。该程序使用的数据来自纽约市的Citi Bike程序(数据位于: http: //www.citibikenyc.com/system-data )。

This is all on a windows 7 x64 bit machine, running python 3.4.3 x64 bit, in the PyCharm Educational edition 1.0.1 compiler. The data being used for this program is taken from the Citi Bike program in New York City (data found here: http://www.citibikenyc.com/system-data).

我已经对数据进行了排序,以便有一个新的CSV文件,其中包含唯一自行车ID和每辆自行车的骑行次数(该文件称为Sorted_Bike_Uses.csv) 。我正在尝试用自行车ID相对于使用次数(X轴上为自行车ID,Y轴上为使用次数)绘制图表。我的代码如下所示:

I have sorted the data so that I have a new CSV file with just the uniqe bike ID's and how many times each bicycle was ridden (file is called Sorted_Bike_Uses.csv). I am trying to make a graph with the bike ID's against the number of uses (Bike ID's on the x-axis, # of uses on the y-axis). My code looks like this:

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()

它创建一个几乎正确格式的图。唯一的问题是,它会对Bike ID进行排序,以便它们以数字顺序而不是使用顺序。我已经尝试过重新使用曾经用来制作类似图形的旧代码,但是这只会使更糟的图形变得不知所措,因为它以某种方式绘制了两组数据。看起来像这样:

It creates an almost correctly formatted graph. The only issue is that it sorts the Bike ID's so that they are in numerical order, rather than being in order of uses. I have tried re-purposing old code that I used to make a similar graph, but it just makes an even worse graph that somehow has two sets of data being plotted. It looks like this:

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()

第二组代码是使用与第一组代码相同的数据集,并且已从原始代码更改为适合花旗自行车数据。我的谷歌夫筋疲力尽。我尝试过重新格式化xtick,将第二个代码段添加到第一个代码中,将第一个代码段添加到第二个代码,依此类推。这可能是我的直视,但我看不到它。感谢您的帮助。

The second set of code is using the same set of data as the first set of code, and has been changed from the original to fit the citi bike data. My google-fu is exhausted. I have tried reformatting the xticks, adding pieces of the second code to the first code, adding pieces of the first code to the second, etc. It is probably something staring me right in the face, but I can't see it. Any help is appreciated.

推荐答案

您只想使用绘图功能来绘制使用次数,然后设置x标签到自行车ID号。因此,在进行绘图时,请勿包括自行车ID号。只需执行plt.plot(c)。如果只给plot函数一个参数,它会自己创建x值,在这种情况下为range(len(c))。然后,您可以将x轴上的标签更改为自行车ID。这是通过plt.xticks完成的。您需要将创建的x值列表和标签列表传递给它。这样就是plt.xticks(range(len(c)),b)。

You want to plot just the number of uses using the plotting function, then set the x-labels to the bike ID numbers. So when you plot, don't include the bike ID numbers. Just do plt.plot(c). If you give the plot function only one argument, it creates the x-values itself, in this case as range(len(c)). Then you can change the labels on the x-axis to the bike IDs. This is done with plt.xticks. You need to pass it the list of x-values that it created and the list of labels. So that would be plt.xticks(range(len(c)), b).

尝试一下:

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(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(range(len(c)), b, 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()

这篇关于pyplot x轴正在排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆