无法使用散景创建图表

Unable to create charts using bokeh

所以我在 Spyder 中使用以下代码创建一个将显示在网络浏览器中的图表:

import pandas as pd
import numpy as np
from bokeh.plotting import figure, output_file, show

car = pd.read_csv('car_sales.csv')

Month = car['Month']
Sales = car['Sales']

output_file("bokeh_scatter_example.html", title="Bokeh Scatter Plot Example")

fig2 = figure(title="Bokeh Scatter Plot Example", x_axis_label='Month', 
              y_axis_label='Sales')

fig2.circle(Month, Sales, size=5, alpha=0.5)

show(fig2)

我意识到,如果 x 轴值是数字,则此代码有效。但我的月份专栏是字符串格式,即 Jan、Feb 等,这是代码停止工作的时间。任何帮助,将不胜感激。谢谢

编辑:car.head() 的输出 月销售额

1808 年 1 月 0 日

1251 年 2 月 1 日

3023 年 3 月 2 日 等等。

您的 X 轴本质上是分类的,因此需要特殊处理。你必须像这样创建一个图形:

fig2 = figure(title="Bokeh Scatter Plot Example", x_axis_label='Month', 
              y_axis_label='Sales',
              x_range=Month)

可以在此处找到更多详细信息:

https://docs.bokeh.org/en/latest/docs/user_guide/categorical.html