This is python code written on jupyter notebook. which produces the following gr
ID: 3918873 • Letter: T
Question
This is python code written on jupyter notebook.
which produces the following graph.
Can the the points on the graph be connected with a line, all the blue points connected to each other and then all the red points connected to each other.
Can error be added nto the plot, can it be shaded like it is in the following plot. can the value for the error be 1.5.
# Create a blank figure with labels
p = figure(plot_width = 600, plot_height = 600,
title = 'Average Number of Units Lost per Engagement',
x_axis_label = 'X', y_axis_label = 'Y')
# Example data
squares_x = [1.295, 2.068, 2.915, 4.203, 5.05, 6.08, 6.891, 7.702, 8.07, 9.984]
squares_y = [0.0005627, 0.0377, 1.003, 1.01, 1.01, 1.01, 1.01, 1.01, 1.01, 1.01]
circles_x = [9, 12, 4, 3, 15]
circles_y = [8, 4, 11, 6, 10]
# Add squares glyph
p.square(squares_x, squares_y, size = 12, color = 'navy', alpha = 0.6)
# Add circle glyph
p.circle(circles_x, circles_y, size = 12, color = 'red')
# Set to output the plot in the notebook
output_notebook()
# Show the plot
show(p)
Explanation / Answer
The band thing can be done using patch of bookeh. Although, since the circles data is not sorted in x axis, the code gives a little unexpected filling in overlapping areas of plot. I advise you to sort your data according to x-axis data points and then it will give you a better plot. (Although, if data is collected in that order and needs to be strictly in that order than keep it as it is).
Here is the code in booked for what you want.
import numpy as np
from bokeh.plotting import figure
from bokeh.io import show, output_notebook
# Create a blank figure with labels
p = figure(plot_width = 600, plot_height = 600,
title = 'Average Number of Units Lost per Engagement',
x_axis_label = 'X', y_axis_label = 'Y')
# Example data
squares_x = [1.295, 2.068, 2.915, 4.203, 5.05, 6.08, 6.891, 7.702, 8.07, 9.984]
squares_y = [0.0005627, 0.0377, 1.003, 1.01, 1.01, 1.01, 1.01, 1.01, 1.01, 1.01]
circles_x = [9, 12, 4, 3, 15]
circles_y = [8, 4, 11, 6, 10]
# Add squares glyph
p.square(squares_x, squares_y, size = 12, color = 'navy', alpha = 0.6)
# Add circle glyph
p.circle(circles_x, circles_y, size = 12, color = 'red')
# Add lines connecting both datapoints
p.line(squares_x, squares_y, line_width = 2, color = 'navy')
p.line(circles_x, circles_y, line_width = 2, color = 'red')
# Error Band for Square line
# Define Error Bands for square line
upperband = [k+1.5 for k in squares_y] #can change error value here, currently put 1.5 as requested in question
lowerband = [k-1.5 for k in squares_y] #can change error value here, currently put 1.5 as requested in question
# Error shading glyph:
band_x = np.append(squares_x, squares_x[::-1])
band_y = np.append(lowerband, upperband[::-1])
p.patch(band_x, band_y, color='navy', fill_alpha=0.2)
# Error Band for Circle line
# Define Error Bands for circle line
upperband = [k+1.5 for k in circles_y] #can change error value here, currently put 1.5 as requested in question
lowerband = [k-1.5 for k in circles_y] #can change error value here, currently put 1.5 as requested in question
# Error shading glyph:
band_x = np.append(circles_x, circles_x[::-1])
band_y = np.append(lowerband, upperband[::-1])
p.patch(band_x, band_y, color='red', fill_alpha=0.2)
# Set to output the plot in the notebook
output_notebook()
# Show the plot
show(p)
Also, if you do not want the error band to have boundaries as with above bookeh code, you may give a try to matplotlib. A brief example to use it for your squares data is below:
import numpy as np
import matplotlib.pyplot as plt
def errorfill(x, y, yerr, color=None, alpha_fill=0.2):
ax = plt.gca()
if color is None:
color = 'navy'
if np.isscalar(yerr) or len(yerr) == len(y):
ymin = [x-yerr for x in y]
ymax = [x+yerr for x in y]
elif len(yerr) == 2:
ymin, ymax = yerr
ax.plot(x, y, color=color)
ax.fill_between(x, ymax, ymin, color=color, alpha=alpha_fill)
squares_x = [1.295, 2.068, 2.915, 4.203, 5.05, 6.08, 6.891, 7.702, 8.07, 9.984]
squares_y = [0.0005627, 0.0377, 1.003, 1.01, 1.01, 1.01, 1.01, 1.01, 1.01, 1.01]
errorfill(squares_x , squares_y, 1.5)
plt.show()
Hope this helps. Cheers!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.