散景线图正在合并图表上的数据

Bokeh line plot is merging data on graph

尝试使用散景绘制全球地表温度异常,但折线图呈锯齿状并合并了数据。

代码:

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

data = pd.read_csv("http://data.giss.nasa.gov/gistemp/tabledata_v3/GLB.Ts+dSST.csv", na_values = ["**** ","***  "])


#select the necessary columns
df = data.ix[:,1:13] #with Year Column

#convert to degrees C 
df = df.divide(100)

#add a year column
df['Year'] = data[' Year']

df2 = df.set_index('Year').stack().reset_index()

df2.columns = ['Year','Month','Value']

#plot using bokeh
from bokeh.io import output_file
from bokeh.models import Span

p = figure(plot_width = 800, plot_height=400, 
       title ='Monthly Mean Temperature Anomaly (1951-1980 baseline)',
       title_text_font_size='14pt') 

p.line(df2['Year'], df2['Value'])  

hline = Span(location=0, dimension='width', 
             line_color='red', 
             line_width=1) #red line at 0

p.renderers.extend([hline]) #add line to the plot
p.xaxis.axis_label = 'Year'
p.yaxis.axis_label = 'Temperature (Degrees C)'
output_file('myplot.html')      
show(p)

线图像 this 一样参差不齐且杂乱无章,最近一个月(3 月)合并到上个月(2 月)。有没有办法把这个更整洁的月份分开绘制?我试过扩展绘图区域,但这并不能解决问题

您需要将 YearMonth 列转换为 DateTime 列:

df2["Date"] = pd.to_datetime(df2.Year.astype(str) + "-" + df2.Month, format="%Y-%b")

要使用日期时间轴绘图:

from bokeh.models import Span

p = figure(plot_width = 800, plot_height=400, 
       title ='Monthly Mean Temperature Anomaly (1951-1980 baseline)',
       title_text_font_size='14pt', x_axis_type="datetime") 

p.line(df2['Date'], df2['Value'])  

hline = Span(location=0, dimension='width', 
             line_color='red', 
             line_width=1) #red line at 0

p.renderers.extend([hline]) #add line to the plot
p.xaxis.axis_label = 'Year'
p.yaxis.axis_label = 'Temperature (Degrees C)'

结果如下: