简体   繁体   中英

Bokeh line plot is merging data on graph

Trying to plot the global surface temperature anomaly using bokeh but the line graph is jagged and merges data.

Code:

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)

The line plot comes out jagged and messy like this and the latest month (March) is merged into the previous month (February). Is there a way of plotting this neater so months plot separately? I've tried extending the plot area but this doesn't fix the issue

You need to convert the Year and Month columns into a DateTime column:

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

To plot with datetime axis:

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)'

Here is the result:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM