interactive plot python for DBSCAN clustering - python

I'm trying to build a code for interactive DBSCAN clustering method. But when I run it I get some errors. But when I'm getting an error says "NameError: name 'train_test_split' is not defined
even I tried to import it using : from sklearn.model_selection import train_test_split
How can I solve to have the interactive plot working proberly?
df_mv = pd.read_csv(r"https://raw.githubusercontent.com/HanaBachi/MachineLearning/main/multishape.csv")
text_trap = io.StringIO()
sys.stdout = text_trap
l = widgets.Text(value=' DBSCAN, Hana Bachi, The University of Texas at Austin',
layout=Layout(width='950px', height='30px'))
eps = widgets.FloatSlider(min=0, max = 2, value=0.1, step = 0.1, description = 'eps',orientation='horizontal', style = {'description_width': 'initial'}, continuous_update=False)
minPts = widgets.FloatSlider(min=0, max = 5, value=1, step = 1, description = 'minPts %',orientation='horizontal',style = {'description_width': 'initial'}, continuous_update=False)
color = ['blue','red','green','yellow','orange','white','magenta','cyan']
style = {'description_width': 'initial'}
ui = widgets.HBox([eps,minPts],)
ui2 = widgets.VBox([l,ui],)
# create activation function plots
def DBSCAN_plot(eps, minPts):
db = DBSCAN(eps=0.155, min_samples=5).fit(df_mv)
labels = db.labels_
n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
x = df_mv.values[:,0]
y = df_mv.values[:,1]
cmap = plt.cm.rainbow
#norm = mc.BoundaryNorm(labels, cmap.N)
plt.figure(figsize=(14,7))
plt.scatter(x, y, c=labels, cmap='tab10', s=50)
plt.scatter(x[np.where(labels==-1)], y[np.where(labels==-1)], c='k', marker='x', s=100)
plt.title('DBSCAN of non-spherical data with noise', fontsize = 20)
plt.colorbar()
plt.show()
plt.subplots_adjust(left=0.0, bottom=0.0, right=2.0, top=1.0, wspace=0.2, hspace=0.3)
plt.show()
interactive_plot1 = widgets.interactive_output(DBSCAN_plot, {'eps': eps})
interactive_plot1 = widgets.interactive_output(DBSCAN_plot, {'minPts': minPts})
interactive_plot1.clear_output(wait = True) # reduce flickering by delaying plot updating
# create dashboard/formatting
uia = widgets.HBox([interactive_plot1],)
uia2 = widgets.VBox([eps, uia],)
uib = widgets.HBox([interactive_plot1],)
uib2 = widgets.VBox([minPts, uib],)
display(uib2, interactive_plot) # display the interactive plot
I'm trying to build an interactive plot for DBSCAN clustering

Related

Automatic Adjust of Y axis values using slider on matplotlib

I was working on making a graph that displays the full line by keping x axis constant and left axis adjusting. I am calculating the cost to produce egg tray with multiple variables.
Using jupyter notebook with ipywidgets as widgets i was able to get the answer.
jypyter auto adjusting y axis
import ipywidgets as widgets
from IPython.display import display
import matplotlib.pyplot as plt
import numpy as np
%matplotlib nbagg
x = np.linspace(50000, 80000, 30000)
fig, ax = plt.subplots(1, figsize=(10,4))
plt.suptitle('Cost To Produce')
def production_cost(carton_percent,assorted_percent,white_percent,dry_eggtray_weight,electric_cost,ebiogas_sold,skilled_labor,manual_labor):
ax.clear()
total_paper_weight = x*dry_eggtray_weight/1000
carton_price = total_paper_weight*carton_percent*1.9/100
assorted_price = total_paper_weight*assorted_percent*4.25/100
white_price = total_paper_weight*white_percent*10/100
burner_consumption = (x+8679)/17.3
electric_consumption = (x+11074)/19.1
skilledlabor_cost = skilled_labor*6*346/7+skilled_labor*6*3.5*54/7
manuallabor_cost = manual_labor*6*290/7+manual_labor*6*3.5*36/7
rawmats_price = carton_price + assorted_price + white_price
burner_price = burner_consumption*2.2*ebiogas_sold
electric_price = electric_consumption*electric_cost
labor_price = skilledlabor_cost + manuallabor_cost
cellophane_price = x*13/(140*2)
maintenance_price = 2000
admin_price = 4000
overall_price = rawmats_price + burner_price + electric_price + labor_price + cellophane_price + maintenance_price + admin_price
y = overall_price/x
ax.plot(x,y)
ax.set_xlabel('Egg Tray Production')
ax.set_ylabel('Cost per Tray')
plt.show()
carton_percent = widgets.FloatSlider(min=0, max=100, value=37.5, description='% Carton:')
assorted_percent = widgets.FloatSlider(min=0, max=100, value=37.5, description='% Assorted:')
white_percent = widgets.FloatSlider(min=0, max=100, value=25, description='% White:')
dry_eggtray_weight = widgets.IntSlider(min=0, max=90, value=80, description='Dry Try (g):')
electric_cost = widgets.FloatSlider(min=6, max=20, value=11.37, description='Elec Cost:')
ebiogas_sold = widgets.FloatSlider(min=6, max=20, value=6.7, description='EBio SellP:')
skilled_labor = widgets.IntSlider(min=0, max=12, value=6, description='Skilled C:')
manual_labor = widgets.IntSlider(min=0, max=16, value=12, description='Manual C:')
widgets.interactive(production_cost, carton_percent=carton_percent, assorted_percent=assorted_percent, white_percent=white_percent, dry_eggtray_weight=dry_eggtray_weight, electric_cost=electric_cost, ebiogas_sold=ebiogas_sold, skilled_labor=skilled_labor, manual_labor=manual_labor)
But using python idle, i wasnt able to copy the result
I tried narrowing variables so it wouldnt be hard to trace. So i tried this code with widgets from matplotlib directly. but only the drawing or graph is moving and both x and y axis is steady. I have tried autoscale but it does not work. Like ax.autoscale
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button
# The parametrized function to be plotted
def f(x,electric_cost,ebiogas_sold):
burner_consumption = (x+8679)/17.3
electric_consumption = (x+11074)/19.1
burner_price = burner_consumption*2.2*ebiogas_sold
electric_price = electric_consumption*electric_cost
overall_price = burner_price + electric_price
return overall_price/x
x = np.linspace(50000, 80000, 30001)
# Define initial parameters
init_electric_cost = 11.37
init_ebiogas_sold = 6.7
# Create the figure and the line that we will manipulate
fig, ax = plt.subplots()
line, = ax.plot(x, f(x, init_electric_cost, init_ebiogas_sold), lw=2)
ax.autoscale(enable=True, axis="y", tight=True)
ax.set_xlabel('Egg Tray Produced [pcs]')
# adjust the main plot to make room for the sliders
fig.subplots_adjust(left=0.25, bottom=0.25)
# Make a vertically oriented slider to control the cost of electricity
electric_cost = fig.add_axes([0.25, 0.1, 0.65, 0.03])
electric_cost_slider = Slider(
ax=electric_cost,
label="Electric Cost [pesos]",
valmin=0,
valmax=20,
valinit=init_electric_cost,
)
# Make a vertically oriented slider to control the cost of biogas converted to power and then sold
biogas_cost = fig.add_axes([0.1, 0.25, 0.0225, 0.63])
biogas_cost_slider = Slider(
ax=biogas_cost,
label="Biogas Power Sold [pesos]",
valmin=0,
valmax=20,
valinit=init_ebiogas_sold,
orientation="vertical"
)
# The function to be called anytime a slider's value changes
def update(val):
line.set_ydata(f(x, electric_cost_slider.val, biogas_cost_slider.val))
#fig.canvas.draw_idle()
# register the update function with each slider
electric_cost_slider.on_changed(update)
biogas_cost_slider.on_changed(update)
# Create a `matplotlib.widgets.Button` to reset the sliders to initial values.
resetax = fig.add_axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', hovercolor='0.975')
def reset(event):
biogas_cost_slider.reset()
electric_cost_slider.reset()
button.on_clicked(reset)
plt.show()
I finally got the answer.
I just have to change the limits of my Y
My changes are highlighted as BOLD
I dont know how to properly construct yet. so ill just paste the screenshot of changes
enter image description here
enter image description here

How to embed a python graph to a webserver created by an esp8266

So, three python graphs are created using matplotlib.pyplot while getting data from Serial via arduino.My goal is to get those graphs and someway display on a web server created by an esp8266.
Python code looks like this
import serial
import matplotlib.pyplot as plt
from drawnow import *
import atexit
import numpy as np
values = []
values1 = []
values2 = []
values3 = []
y=0
values4 = []
plt.ion()
cnt=0
BBox = ((24.5242, 26.4025, 40.2523,41.0255 ))
serialArduino = serial.Serial('COM7', 9600)
def plotValues():
plt.subplot(2, 2, 1)
plt.title('Serial value from Arduino')
plt.grid(True)
plt.ylabel('Values')
plt.plot(values, 'g', label='temp values')
#axes.legend(loc=4)
plt.plot(values1, 'r', label='ph values')
plt.legend(loc='upper left')
x = values2
#y = np.array([10, 20, 30, 40])
plt.subplot(2,2,2)
plt.plot(x, 'b', label='turb')
plt.legend(loc='upper left')
ruh_m = plt.imread(r'C:\Users\lordjimas\Desktop\map.png')
ax= plt.subplot(2,2,(3,4))
ax.set_title('Plotting Spatial Data on Alexandroupolis Map')
ax.set_xlim(BBox[0],BBox[1])
ax.set_ylim(BBox[2],BBox[3])
ax.imshow(ruh_m, extent = BBox, aspect= 'equal')
ax.scatter(values4, values3, color = 'black')
ax.fill_between((1,2), 0, 1, facecolor='green', alpha=0.2) # blocked area for first axes
ax.fill_between((5,6), 0, 1, facecolor='orange', alpha=0.2) # blocked area for second axes
def doAtExit():
serialArduino.close()
print("Close serial")
print("serialArduino.isOpen() = " + str(serialArduino.isOpen()))
atexit.register(doAtExit)
print("serialArduino.isOpen() = " + str(serialArduino.isOpen()))
#pre-load dummy data
while True:
while (serialArduino.inWaiting()==0):
pass
print("readline()")
valueRead = serialArduino.readline()
valueRead1 = serialArduino.readline()
valueRead2 = serialArduino.readline()
#valueRead3 = serialArduino.readline()
#valueRead4 = serialArduino.readline()
#check if valid value can be casted
try:
valueInInt = float(valueRead)
valueInInt1 = float(valueRead1)
valueInInt2 = float(valueRead2)
#valueInInt3 = float(valueRead3)
#valueInInt4 = float(valueRead4)
if valueInInt <= 8000000:
if valueInInt1 < 14:
values.append(valueInInt)
values1.append(valueInInt1)
values2.append(valueInInt2)
#values3.append(valueInInt3)
#values4.append(valueInInt4)
y=y+5
#values.pop(0)
#values4.pop(0)
#values3.pop(0)
drawnow(plotValues)
else:
print("Invalid!problem with serial ")
else:
print("Invalid! too large")
except ValueError:
print("Invalid! cannot cast")
I have tried converting those graphs to html script using the mpld3 library but I cant find a way to include that script to my esp8266 code

Encountering time out error in the middle of a matplotlib for loop

I have a code which will go through three dictionaries, and make some plots if the keys all match. I've been running into an odd issue due to the use of the matplotlib table.
When I first got this code to run, I had no issues finishing the whole loop. Now I am encountering a time out error by the second iteration
I tried moving the the table out of the for loop.
I added plt.close('all')
I also try importing matplotlib again at the end of the loop in hopes of resetting something in the backend.
for k, v in oct_dict.items():
for k2, v2 in stu_dict.items():
for k3, v3 in oct2_dict.items():
if k == k2 and k == k3:
with PdfPages('{}.pdf'.format(k)) as pdf:
#rc('font', **{'family': 'serif', 'serif': ['Computer Modern']})
#v = v[v['a_1920'] != 0]
rc('text', usetex=True)
fig = plt.figure(figsize = (8,10.5))
gs=GridSpec(2,2) # 2 rows, 3 columns
ax0 = fig.add_subplot(gs[0,0])
ax0.bar(x=np.arange(2), height = [float(v['a_1920'])*100, mean_a_1920*100], color = nice)
plt.xticks(np.arange(2), ['{}'.format(k), 'D75'])
for p in ax0.patches:
a =p.get_height()
ax0.annotate('{:0.2f}'.format(float(a)), (p.get_x()+.1, p.get_height() * .75), weight = 'bold')
ax1 = fig.add_subplot(gs[0,1])
c = str(len(v2['student_id']))
c2 = int(v['c_1920'])
props = dict(boxstyle='round', facecolor='white', alpha=0.0)
c3 = int(v['b_1920'])
# place a text box in upper left in axes coords
c4 = int(v['d_1920'])
ax1.text(0.0, 0.95, 'Number of Age : {}'.format(c3), transform=ax1.transAxes, fontsize=12,
verticalalignment='top')
ax1.text(0.0, 0.85, 'Number of Incomplete : {}'.format(c2), transform=ax1.transAxes, fontsize=12,
verticalalignment='top')
ax1.text(0.0, 0.75, 'Number of Invalid : {}'.format(c4), transform = ax1.transAxes, fontsize = 12,
verticalalignment = 'top' )
ax1.text(0.0, 0.65, 'Number of who will reach Age:\n{}'.format(c), transform=ax1.transAxes, fontsize=12,
verticalalignment='top' )
#ax1.table(cellLoc = 'center', cellText = [] , loc = 'upper center')
ax1.axis('off')
ax1.axis('tight')
#fig.suptitle('Monthly Summary', va = 'top', ha= 'center')
fig.text(0.3, 1, 'Monthly Summary '+ dt.date.today().strftime("%b %d, %Y"), fontsize=12, verticalalignment='top', bbox=props)
#plt.subplots_adjust(top = .75)
#plt.tight_layout()
#gs.constrained_layout()
#print(float(v3['inc']))
#print(float(v3['com']))
ax2 = fig.add_subplot(gs[1,0])
plt.sca(ax2)
p1 = plt.bar(np.arange(1), int(v3['com']), width=.25,color = 'b',label = 'Complete')
p2 = plt.bar(np.arange(1), int(v3['inc']), width = .25, bottom = int(v3['com']), color = 'r', label = 'Incomplete')
plt.legend()
for p in ax2.patches:
ax2.annotate((p.get_height()), (p.get_x()+.1, p.get_height() * .75), weight = 'bold')
ax2.set_xticks([])
# # #ax2.set_xlabel='Students Who Will Turn 15'
ax2.set_title('Students who will turn 15 later in the school year')
ax2.set_xticks([])
ax3 = fig.add_subplot(gs[1,1])
a = int(v3['com'])+int(v3['inc'])
ax3.axis('off')
plt.tight_layout()
pdf.savefig()
plt.close('all')
fig = plt.figure(figsize = (8,11.5))
gs=GridSpec(1,1)
axs = fig.add_subplot(gs[0])
cell_text = []
v2 = v2.drop(['Grand Total','birth_dte','loc'],axis = 1)
binarymap = {0:'No',1:'Yes'}
v2['Plan Not Complete'] = v2['Plan Not Complete'].map(binarymap)
v2['Plan Already Complete'] = v2['Plan Already Complete'].map(binarymap)
labels = [six column titles here]
for row in range(len(v2)):
try:
cell_text.append(v2.iloc[row])
except:
pass
table = axs.table(cellLoc = 'center', cellText = cell_text, colLabels = labels,
rowLoc = 'center', colLoc = 'center',loc = 'upper center',fontsize = 32)
table.set_fontsize(32)
table.scale(1, 1.5)
#axs.text(0.0,0.5,'For the column')
axs.axis('off')
pdf.savefig()
#plt.savefig('{}_list.pdf'.format(k))
plt.show()
plt.close('all')
import matplotlib.pyplot as plt
TimeoutError: Lock error: Matplotlib failed to acquire the following lock file:
C:\Users\myusername.matplotlib\tex.cache\23c95fa5c37310802233a994d78d178d.tex.matplotlib-lock
NOTE: If some of the key names dont match in this code it is on purpose, I had to change them up for this post since it is public. The error is thrown by the second iteration once the code reaches the axs.table line.
I got everything to run properly after using the conda command prompt to clean the environments
conda clean --all
Something that works but I would have liked to avoid was just removing the use of tex for this script. rc param tex set to False, code finished running pretty quickly as well

How to display second Y-axis's values in matplotlib?

I like to display values on second y-axis (right vertical) in matplotlib.
My code is as follow.
def main(files):
plt.style.use('ggplot')
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.set_xlabel('iteration')
losslabel = ax1.set_ylabel('loss')
losslabel.set_color("red")
accuracylabel = ax2.set_ylabel('accuracy %')
accuracylabel.set_color("green")
disp_results(fig, ax1, ax2, loss_iterations, losses, accuracy_iterations, accuracies, accuracies_iteration_checkpoints_ind, fileName, color_ind=i)
plt.show()
def disp_results(fig, ax1, ax2, loss_iterations, losses, accuracy_iterations, accuracies, accuracies_iteration_checkpoints_ind, fileName, color_ind=0):
colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS)
acrIterations =[]
top_acrs={}
if accuracies.size:
if accuracies.size>4:
top_n = 4
else:
top_n = accuracies.size -1
temp = np.argpartition(-accuracies, top_n)
result_indexces = temp[:top_n]
temp = np.partition(-accuracies, top_n)
result = -temp[:top_n]
for acr in result_indexces:
acrIterations.append(accuracy_iterations[acr])
top_acrs[str(accuracy_iterations[acr])]=str(accuracies[acr])
sorted_top4 = sorted(top_acrs.items(), key=operator.itemgetter(1))
maxAcc = np.amax(accuracies, axis=0)
iterIndx = np.argmax(accuracies)
maxAccIter = accuracy_iterations[-1]
maxIter = accuracy_iterations[-1]
consoleInfo = format('\n[%s]:Test accuracy Vs Train loss [from 0 to %s ] = [Iteration %s]: %s ' %(fileName,maxIter,maxAccIter ,maxAcc))
plotTitle = format('Test accuracy Vs Train loss(%s) [Iteration %s]: %s ' % (fileName,maxAccIter, maxAcc))
print (consoleInfo)
print ('Top 4 accuracies:'+str(sorted_top4))
plt.title(plotTitle)
ax1.plot(loss_iterations, losses, colors['red'])
ax2.plot(accuracy_iterations, accuracies, colors['green'], label=str(fileName))
ax2.plot(accuracy_iterations[accuracies_iteration_checkpoints_ind], accuracies[accuracies_iteration_checkpoints_ind], 'o', colors['red'])
The plot is fine, just that right vertical axis values are not displayed.
Plot is here.
Python code and log files are here and here.

How to manually change the tick labels of the margin plots on a Seaborn jointplot

I am trying to use a log scale as the margin plots for my seaborn jointplot. I am usings set_xticks() and set_yticks(), but my changes do not appear. Here is my code below and the resulting graph:
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import seaborn as sns
import pandas as pd
tips = sns.load_dataset('tips')
female_waiters = tips[tips['sex']=='Female']
def graph_joint_histograms(df1):
g=sns.jointplot(x = 'total_bill',y = 'tip', data = tips, space = 0.3,ratio = 3)
g.ax_joint.cla()
g.ax_marg_x.cla()
g.ax_marg_y.cla()
for xlabel_i in g.ax_marg_x.get_xticklabels():
xlabel_i.set_visible(False)
for ylabel_i in g.ax_marg_y.get_yticklabels():
ylabel_i.set_visible(False)
x_labels = g.ax_joint.get_xticklabels()
x_labels[0].set_visible(False)
x_labels[-1].set_visible(False)
y_labels = g.ax_joint.get_yticklabels()
y_labels[0].set_visible(False)
y_labels[-1].set_visible(False)
g.ax_joint.set_xlim(0,200)
g.ax_marg_x.set_xlim(0,200)
g.ax_joint.scatter(x = df1['total_bill'],y = df1['tip'],data = df1,c = 'y',edgecolors= '#080808',zorder = 2)
g.ax_joint.scatter(x = tips['total_bill'],y = tips['tip'],data = tips, c= 'c',edgecolors= '#080808')
ax1 =g.ax_marg_x.get_axes()
ax2 = g.ax_marg_y.get_axes()
ax1.set_yscale('log')
ax2.set_xscale('log')
ax1.set_yscale('log')
ax2.set_xscale('log')
ax2.set_xlim(1e0, 1e4)
ax1.set_ylim(1e0, 1e3)
ax2.xaxis.set_ticks([1e0,1e1,1e2,1e3])
ax2.xaxis.set_ticklabels(("1","10","100","1000"), visible = True)
plt.setp(ax2.get_xticklabels(), visible = True)
colors = ['y','c']
ax1.hist([df1['total_bill'],tips['total_bill']],bins = 10, stacked=True,log = True,color = colors, ec='black')
ax2.hist([df1['tip'],tips['tip']],bins = 10,orientation = 'horizontal', stacked=True,log = True,color = colors, ec='black')
ax2.set_ylabel('')
Any ideas would be much appreciated.
Here is the resulting graph:
You should actually get an error from the line g.ax_marg_y.get_axes() since an axes does not have a get_axes() method.
Correcting for that
ax1 =g.ax_marg_x
ax2 = g.ax_marg_y
should give you the desired plot. The ticklabels for the log axis are unfortunately overwritten by the histogram's log=True argument. So you can either leave that out (since you already set the axes to log scale anyways) or you need to set the labels after calling hist.
import matplotlib.pyplot as plt
import seaborn as sns
tips = sns.load_dataset('tips')
def graph_joint_histograms(tips):
g=sns.jointplot(x = 'total_bill',y = 'tip', data = tips, space = 0.3,ratio = 3)
g.ax_joint.cla()
g.ax_marg_x.cla()
g.ax_marg_y.cla()
for xlabel_i in g.ax_marg_x.get_xticklabels():
xlabel_i.set_visible(False)
for ylabel_i in g.ax_marg_y.get_yticklabels():
ylabel_i.set_visible(False)
x_labels = g.ax_joint.get_xticklabels()
x_labels[0].set_visible(False)
x_labels[-1].set_visible(False)
y_labels = g.ax_joint.get_yticklabels()
y_labels[0].set_visible(False)
y_labels[-1].set_visible(False)
g.ax_joint.set_xlim(0,200)
g.ax_marg_x.set_xlim(0,200)
g.ax_joint.scatter(x = tips['total_bill'],y = tips['tip'],data = tips,
c = 'y',edgecolors= '#080808',zorder = 2)
g.ax_joint.scatter(x = tips['total_bill'],y = tips['tip'],data = tips,
c= 'c',edgecolors= '#080808')
ax1 =g.ax_marg_x
ax2 = g.ax_marg_y
ax1.set_yscale('log')
ax2.set_xscale('log')
ax2.set_xlim(1e0, 1e4)
ax1.set_ylim(1e0, 1e3)
ax2.xaxis.set_ticks([1e0,1e1,1e2,1e3])
ax2.xaxis.set_ticklabels(("1","10","100","1000"), visible = True)
plt.setp(ax2.get_xticklabels(), visible = True)
colors = ['y','c']
ax1.hist([tips['total_bill'],tips['total_bill']],bins = 10,
stacked=True, color = colors, ec='black')
ax2.hist([tips['tip'],tips['tip']],bins = 10,orientation = 'horizontal',
stacked=True, color = colors, ec='black')
ax2.set_ylabel('')
graph_joint_histograms(tips)
plt.show()

Categories

Resources