[英]Bokeh line plot is merging data on graph
试图使用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 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)
线条图变得像这样锯齿状且凌乱,最后一个月(3月)合并到了上个月(2月)。 有没有办法画出这种整洁的东西,所以几个月单独画图? 我已经尝试扩展绘图区域,但这不能解决问题
您需要将Year
和Month
列转换为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)'
结果如下:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.