为什么这个散景散点图没有显示任何内容?

时间:2015-08-31 12:15:36

标签: python scatter-plot bokeh

我正在尝试使用python制作散点图,它显示了逐年的联赛位置和新秀/退伍军人团队信息。 我希望有一些点,每年一列,每个位置一个点,点的颜色取决于他们是新手还是老手。

我有这段代码,但它似乎没有显示任何数据。为什么呢?

from bokeh.charts import Scatter, show, output_file
from bokeh.models import Range1d
#from vdata.data import get_schools
from collections import OrderedDict

def get_data(years):
    schools = get_schools(['raw_data/2015-teams.yaml'], [2015])

    d = OrderedDict()
    d['rookie'] = []
    d['veteran'] = []

    for s in schools:
        for year in years:
            rookie = s.data[year]['rookie']
            league_position = s.data[year]['league']

            if rookie:
                d['rookie'].append((year, league_position))
            else:
                d['veteran'].append((year, league_position))
    return d

data = OrderedDict([('rookie', [(2015, 14), (2015, 26), (2015, 47), (2015, 41), (2015, 24), (2015, 45), (2015, 35), (2015, 46), (2015, 21), (2015, 47), (2015, 5), (2015, 31), (2015, 28), (2015, 30), (2015, 18)]), ('veteran', [(2015, 13), (2015, 27), (2015, 42), (2015, 18), (2015, 39), (2015, 34), (2015, 22), (2015, 2), (2015, 3), (2015, 43), (2015, 8), (2015, 40), (2015, 1), (2015, 29), (2015, 4), (2015, 18), (2015, 44), (2015, 7), (2015, 23), (2015, 16), (2015, 32), (2015, 6), (2015, 37), (2015, 25), (2015, 11), (2015, 38), (2015, 17), (2015, 12), (2015, 15)])])

def league_rookie(d):
    scatter = Scatter(d, title='League positions and rookie values', legend=True, ylabel='League Position', xlabel='Year', width=1000, height=600, y_range=Range1d(60, 0))
    output_file('visuals/html/league_rookie.html')
    show(scatter)

if __name__ == '__main__':
    #data = get_data([2015])
    league_rookie(data)

我觉得这不是一个使用Scatter情节的好方法,但我不知道怎么做。

1 个答案:

答案 0 :(得分:1)

您发布的代码包含只有一个x值的示例数据。如果您更改样本数据,以便只有一个点用于不同的年份(我将第一个点的年份更改为2014年),那么数据图表使用与您发布的完全相同的代码(只要您创建保存)位置)。

似乎你的get_data()函数只返回一年的数据,这将导致同样的问题。

如果您想要的图形实际上只是一个垂直的点列,那么也许您可以使用不同年份的第三个虚拟系列,然后隐藏图例条目并设置查看窗口以排除虚拟数据。

我猜你想要比这更好的东西。如果你提供一个模拟你希望图形实际看起来像什么,也许使用excel或其他东西,你会得到更好的答案。

相关问题