Bokeh:无法显示具有月份轴的数据

huangapple go评论94阅读模式
英文:

Bokeh : unable to display data with month axis

问题

我使用SQL和Bokeh进行数据可视化。以下是代码的翻译部分:

  1. # 工作正常的情况
  2. import psycopg2
  3. from bokeh.plotting import figure, show
  4. import numpy as np
  5. from bokeh.palettes import Category10_10 as palette
  6. import itertools
  7. # 数据库连接
  8. conn = psycopg2.connect(database="data", user="postgres", password="privatepassword", host="localhost", port="5432")
  9. cursor = conn.cursor()
  10. cursor.execute("SELECT date_of_search, job_search, COUNT(*) FROM occurrences GROUP BY date_of_search, job_search ORDER BY date_of_search DESC")
  11. results = cursor.fetchall()
  12. cursor.close()
  13. conn.close()
  14. # 数据准备
  15. dates = [np.datetime64(row[0]) for row in results]
  16. job_searches = [row[1] for row in results]
  17. counts = [row[2] for row in results]
  18. # 图表选项
  19. plot = figure(x_axis_type="datetime", title="occurrences evolution over time",
  20. x_axis_label="date", y_axis_label="occurrences",
  21. sizing_mode="stretch_width",
  22. height=700)
  23. # 尝试颜色调色板
  24. colors = itertools.cycle(palette)
  25. for job_search in list(set(job_searches)):
  26. job_dates = [date for date, job, count in zip(dates, job_searches, counts) if job == job_search]
  27. job_counts = [count for date, job, count in zip(dates, job_searches, counts) if job == job_search]
  28. plot.line(job_dates, job_counts, line_width=3, legend_label=job_search, color=next(colors))

这是工作正常的情况下的代码部分。

以下是不工作情况下的代码翻译:

  1. # 不工作的情况
  2. import psycopg2
  3. from bokeh.plotting import figure, show
  4. from bokeh.palettes import Category10_10 as palette
  5. import numpy as np
  6. import itertools
  7. # 数据库连接
  8. conn = psycopg2.connect(database="data", user="postgres", password="privatepassword", host="localhost", port="5432")
  9. cursor = conn.cursor()
  10. cursor.execute("""
  11. SELECT DATE(DATE_TRUNC('month', date_of_search)), job_search, COUNT(*)
  12. FROM occurrences
  13. WHERE DATE_TRUNC('month', date_of_search) != DATE_TRUNC('month', current_date)
  14. GROUP BY DATE_TRUNC('month', date_of_search), job_search
  15. ORDER BY DATE_TRUNC('month', date_of_search) DESC
  16. """)
  17. results = cursor.fetchall()
  18. cursor.close()
  19. conn.close()
  20. # 数据准备
  21. dates = [np.datetime64(row[0]) for row in results]
  22. job_searches = [row[1] for row in results]
  23. counts = [row[2] for row in results]
  24. # 图表选项
  25. plot = figure(x_axis_type="datetime", title="occurrences evolution over time",
  26. x_axis_label="date", y_axis_label="occurrences",
  27. sizing_mode="stretch_width",
  28. height=700)
  29. # 尝试颜色调色板
  30. colors = itertools.cycle(palette)
  31. for job_search in list(set(job_searches)):
  32. job_dates = [date for date, job, count in zip(dates, job_searches, counts) if job == job_search]
  33. job_counts = [count for date, job, count in zip(dates, job_searches, counts) if job == job_search]
  34. plot.line(job_dates, job_counts, line_width=3, legend_label=job_search, color=next(colors))

这是不工作的情况下的代码翻译部分。

英文:

I use SQL and Bokeh for data visualization. Here's the context: I need to represent a linechart according to occurrences of words related to job names. The aim is a monthly granularity to follow an evolution.

I first tested a daily granularity with the following code:

  1. # Working case
  2. import psycopg2
  3. from bokeh.plotting import figure, show
  4. import numpy as np
  5. from bokeh.palettes import Category10_10 as palette
  6. import itertools
  7. # Database connexion
  8. conn = psycopg2.connect(database="data", user="postgres", password="privatepassword", host="localhost", port="5432")
  9. cursor = conn.cursor()
  10. cursor.execute("SELECT date_of_search, job_search, COUNT(*) FROM occurrences GROUP BY date_of_search, job_search ORDER BY date_of_search DESC")
  11. results = cursor.fetchall()
  12. cursor.close()
  13. conn.close()
  14. # Data preparation
  15. dates = [np.datetime64(row[0]) for row in results]
  16. job_searches = [row[1] for row in results]
  17. counts = [row[2] for row in results]
  18. # Chart options
  19. plot = figure(x_axis_type="datetime", title="occurrences evolution over time",
  20. x_axis_label="date", y_axis_label="occurrences",
  21. sizing_mode="stretch_width",
  22. height=700)
  23. # Trying color palette
  24. colors = itertools.cycle(palette)
  25. for job_search in list(set(job_searches)):
  26. job_dates = [date for date, job, count in zip(dates, job_searches, counts) if job == job_search]
  27. job_counts = [count for date, job, count in zip(dates, job_searches, counts) if job == job_search]
  28. plot.line(job_dates, job_counts, line_width=3, legend_label=job_search, color=next(colors))

The result is perfectly working :

Bokeh:无法显示具有月份轴的数据

So I tried changing the granularity in my SQL query, but the X-axis is unusable. I tried to change the date extraction in the SQL query in various ways, I tried to convert the month of the X axis via .astype('datetime64[M]') :

  1. job_months = np.unique([date.astype('datetime64[M]') for date, job, count in zip(dates, job_searches, counts) if job == job_search])

Unfortunately, nothing works. Here's the (almost identical) non-functional code:

  1. # Not working case
  2. import psycopg2
  3. from bokeh.plotting import figure, show
  4. from bokeh.palettes import Category10_10 as palette
  5. import numpy as np
  6. import itertools
  7. # Database connexion
  8. conn = psycopg2.connect(database="data", user="postgres", password="privatepassword", host="localhost", port="5432")
  9. cursor = conn.cursor()
  10. cursor.execute("""
  11. SELECT DATE(DATE_TRUNC('month', date_of_search)), job_search, COUNT(*)
  12. FROM occurrences
  13. WHERE DATE_TRUNC('month', date_of_search) != DATE_TRUNC('month', current_date)
  14. GROUP BY DATE_TRUNC('month', date_of_search), job_search
  15. ORDER BY DATE_TRUNC('month', date_of_search) DESC
  16. """)
  17. results = cursor.fetchall()
  18. cursor.close()
  19. conn.close()
  20. # Data preparation
  21. dates = [np.datetime64(row[0]) for row in results]
  22. job_searches = [row[1] for row in results]
  23. counts = [row[2] for row in results]
  24. # Chart options
  25. plot = figure(x_axis_type="datetime", title="occurrences evolution over time",
  26. x_axis_label="date", y_axis_label="occurrences",
  27. sizing_mode="stretch_width",
  28. height=700)
  29. # Trying color palette
  30. colors = itertools.cycle(palette)
  31. for job_search in list(set(job_searches)):
  32. job_dates = [date for date, job, count in zip(dates, job_searches, counts) if job == job_search]
  33. job_counts = [count for date, job, count in zip(dates, job_searches, counts) if job == job_search]
  34. plot.line(job_dates, job_counts, line_width=3, legend_label=job_search, color=next(colors))

This is the only result i'm able to have :

Bokeh:无法显示具有月份轴的数据

Do you have any idea? I'm stuck Bokeh:无法显示具有月份轴的数据

The WHERE DATE_TRUNC('month', date_of_search) != DATE_TRUNC('month', current_date) in SQL statement is because I don't want the data of unfinished month (allways the current one).

答案1

得分: 0

OK, 我明白了。

只是因为只有一个月的数据... 我尝试添加一些七月的数据来检查,像这样:

  1. (SELECT DATE(DATE_TRUNC('month', date_of_search)), job_search, COUNT(*)
  2. FROM occurrences
  3. GROUP BY DATE_TRUNC('month', date_of_search), job_search
  4. ORDER BY DATE_TRUNC('month', date_of_search) DESC)
  5. UNION
  6. (SELECT DATE('2023-07-01') AS date, '数据分析师' AS job_search, '487' AS count)
  7. UNION
  8. (SELECT DATE('2023-07-01') AS date, '数据工程师' AS job_search, '1202' AS count)

而且它正常工作了...

Bokeh:无法显示具有月份轴的数据

我的错,也许这个主题将来某天会帮助到别人。

英文:

OK, i figured out.

It's just because there is data for one month... I tried to add some data for July to check like this :

  1. (SELECT DATE(DATE_TRUNC('month', date_of_search)), job_search, COUNT(*)
  2. FROM occurrences
  3. GROUP BY DATE_TRUNC('month', date_of_search), job_search
  4. ORDER BY DATE_TRUNC('month', date_of_search) DESC)
  5. UNION
  6. (SELECT DATE('2023-07-01') AS date, 'Data Analyst' AS job_search, '487' AS count)
  7. UNION
  8. (SELECT DATE('2023-07-01') AS date, 'Data Engineer' AS job_search, '1202' AS count)

And it's working...

Bokeh:无法显示具有月份轴的数据

My bad, maybe the topic will help someone someday.

huangapple
  • 本文由 发表于 2023年7月11日 01:13:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定