我有一个
csv
文件,并试图使年份出现在 x 轴上,但年份无法出现。
from bokeh.plotting import figure
from bokeh.io import output_file, output_notebook, show
from bokeh.layouts import column
from bokeh.models import HoverTool, ColumnDataSource
data = pd.read_csv('amountsaved.csv')
data.head()
data['year']=pd.to_datetime(data['year'])
# create a new plot with a title and axis labels
p = figure(title="amount saved", x_axis_type='datetime', x_axis_label="year", y_axis_label="amount")
# add a line renderer with legend and line thickness
p.line(x, y, legend_label="amt saved", line_width=2)
# Plot date along the x axis and price along the y axis
p.line(data['year'], data['amountsaved'], line_color='green')
# show the results
show(p)
O输出:
我的
csv
文件中的数据只有年份(例如:2010,2011,2012),没有月份。我不知道为什么输出显示月份。
您的年份数据真的只有 2010 年、2011 年等吗? 我根据您的代码和这些数据做了一个简单的例子,它工作得很好。我不得不删除线
p.line(x, y, legend_label="amt saved", line_width=2)
但是,因为您的示例没有定义
x
和 y
。
from bokeh.plotting import figure
from bokeh.io import output_file, output_notebook, show
from bokeh.layouts import column
from bokeh.models import HoverTool, ColumnDataSource
import pandas
output_notebook()
data = {'year': ['2010', '2011', '2012'], 'amountsaved': [0,1,2,]}
data = pandas.DataFrame.from_dict(data)
data['year']=pandas.to_datetime(data['year'])
# create a new plot with a title and axis labels
p = figure(title="amount saved", x_axis_type='datetime', x_axis_label="year", y_axis_label="amount")
# Plot date along the x axis and price along the y axis
p.line(data['year'], data['amountsaved'], line_color='green')
# show the results
show(p)