Python Bokeh线图正在合并图形上的数据
试图用bokeh绘制全球地表温度异常图,但线图参差不齐并合并了数据 代码:Python Bokeh线图正在合并图形上的数据,python,pandas,bokeh,Python,Pandas,Bokeh,试图用bokeh绘制全球地表温度异常图,但线图参差不齐并合并了数据 代码: 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 necessar
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)
线条图呈锯齿状,杂乱无章,最近一个月(三月)与前一个月(二月)合并。有没有一种方法可以把这个整洁的东西分开画出来?我已尝试扩展绘图区域,但这无法解决问题您需要将
年
和月
列转换为日期时间
列:
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)'
结果如下:
您需要将
年
和月
列转换为日期时间
列:
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)'
结果如下: