用ggplot绘制2 geom_line()

时间:2016-06-27 12:13:15

标签: python pandas python-ggplot

Simple.csv:

"","teamID","yearID","HR"
"1320","LAN",1958,172
"1325","SFN",1958,170
"1336","LAN",1959,148
"1341","SFN",1959,167
"1352","LAN",1960,126
"1357","SFN",1960,130
"1369","LAN",1961,157
"1375","SFN",1961,183
"1388","LAN",1962,140
"1395","SFN",1962,204
"1408","LAN",1963,110
"1415","SFN",1963,197
"1428","LAN",1964,79
"1435","SFN",1964,165
"1448","LAN",1965,78

我正在尝试绘制2个geom_line()对应2个团队(LAN / SFN):

hr_by_team_year = pandas.read_csv("simple.csv")

print ggplot(hr_by_team_year, aes("yearID", "HR", color="teamID")) + geom_point()+ geom_line()

仅获得离散点: enter image description here

但是我想要绘制一个这样的图表:

enter image description here

根据Plotting Line Charts

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:1)

如果指定参数名称(即使它们是可选的),有时也更容易调试。建议对您的代码进行此修改。

ggplot(hr_by_team_year, aes(x="yearID", y="HR", color="teamID")) + geom_line() 

然后,您的下一个问题可能与x轴上的年份有关。如果您的数据只有数字年份,则上述内容可能会有效。如果它有您需要显示年份的日期,则可能需要添加以下内容:+ scale_x_date(labels='%Y')

我刚做了一个带日期的情节,我希望x轴显示每3个月左右的日期。此代码示例也可以帮助您:

ggplot(aes(x='Date', y='Price', color='Daily'), data=googleHiLoPlotData) \
    + geom_line() + scale_x_date(breaks=date_breaks('3 months'), labels='%m-%d-%Y') \
    + ggtitle("Daily Prices of Google")

最后,在研究如何执行此操作时,以下网址帮助了我:

http://blog.yhat.com/posts/ggplot-for-python.html