Atkins Dynamic graph: 5B.5

5b_5gibbs_energy_of_mixing_1000
import math
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import figure
from ipywidgets import interact
from ipywidgets import interact, fixed, FloatSlider, HBox, VBox, Layout, Output, Label, Box, GridspecLayout, GridBox
import ipywidgets as widgets
from matplotlib import ticker

# returns Gm(x) for given parameters: a, b, xx
def fvee(xi,xx):
  fval = xi*xx*(1-xx)+xx*math.log(xx)+(1-xx)*math.log(1-xx)
  return fval



#set up how range of plot, ticks, grid

n_dict={2.0:0.499997,2.05:0.366075,2.1:0.314647,2.15:
   0.277713, 2.2:0.24853,2.25:0.224405, 2.3:0.203923, 2.35:
   0.186218, 2.4: 0.170715, 2.45: 0.157008, 2.5: 0.144794, 2.55:
    0.133842, 2.60: 0.123971, 2.65: 0.115034,2.70:
   0.106913, 2.75: 0.0995075, 2.80: 0.0927357, 2.85:
   0.0865272, 2.90: 0.0808218, 2.95: 0.0755677, 3.: 0.0707202}
px=1.0/96 #pixel in inches
xmin=0.000001
xmax=0.999999
xrange=xmax-xmin
xgrid=.25
x_ticks = np.arange(xmin,xmax+xgrid,xgrid)
show_x_ticks=False
show_x_tick_labels=True
ymin=-0.5
ymax=0.1
yrange=ymax-ymin
ygrid=0.1
show_y_ticks=False
show_y_tick_labels=True
y_ticks = np.arange(ymin, ymax+ygrid, ygrid)
show_grid=True
npts=100
x = np.linspace(xmin, xmax, npts)
Gmix=np.linspace(0.0, 1.0, npts)
def pvplot(xi):
  for i in range (0,npts):
    Gmix[i] = xi*x[i]*(1-x[i])+x[i]*math.log(x[i])+(1-x[i])*math.log(1-x[i])
  # minimum value of Gmix(x) 
  if xi>2.0:
    xmin = n_dict[xi]
    fmin1= fvee(xi,xmin)
  else:
    xmin = 0.5
    fmin2= fvee(xi,xmin)
  fig = plt.figure(figsize=(920*px,569*px)) 
  ax = fig.add_subplot(111)
  plt.plot(x,Gmix,linewidth=2,linestyle='-',color='red',clip_on=True)
  plt.axis([0,1,ymin,ymax])
  plt.ylabel(r'$\Delta _\mathrm{mix} G/nRT$',fontsize='x-large')
  plt.xlabel(r'$x_\mathrm{A}$',fontsize='x-large')
  plt.xticks(x_ticks)
  plt.yticks(y_ticks)
  if show_grid: plt.grid(True,color='blue',linewidth=.25)
  if not show_x_tick_labels: ax.set_xticklabels([])
  if not show_y_tick_labels: ax.set_yticklabels([])
  plt.tick_params(axis='x',which='both',bottom = show_x_ticks, top= False,labelsize='large')
  plt.tick_params(axis='y',which='both',left = show_y_ticks, right= False,labelsize='large')
  if xi>2.0:
    plt.plot([xmin,xmin], [-0.5,fmin1], linestyle='--', lw=1,color='black')
    plt.plot(xmin, fmin1, 'o', markersize=10, markeredgecolor='black', markerfacecolor='white', markeredgewidth=1)
    plt.plot([1-xmin,1-xmin], [-0.5,fmin1], linestyle='--', lw=1,color='black')
    plt.plot(1-xmin, fmin1, 'o', markersize=10, markeredgecolor='black', markerfacecolor='white', markeredgewidth=1)

  else:
    plt.plot([0.5,0.5], [-0.5,fmin2], linestyle='--', lw=1,color='black')
    plt.plot(xmin, fmin2, 'o', markersize=10, markeredgecolor='black', markerfacecolor='white', markeredgewidth=1)
  plt.show()

#margin='top right bottom left'
xi=FloatSlider(description='',min=1, max=3, step=0.05, value=2.5,continuous_update=False,
orientation='vertical',readout=True,
style=dict(handle_color='gray'),
layout=dict(height='200px',width='60px',margin='0px 0px 0px 0px'))

out = widgets.interactive_output(pvplot, {'xi': xi})

xi_label=widgets.HTMLMath(value='<i>&xi;<i>',fontsize='x-large',layout=dict(margin='50px 0px 5px 0px'))

GridBox(children=[VBox([xi_label,xi],layout=Layout(align_items='center',width='80px')),out],
        layout=Layout(
            width='1000px',
            grid_template_columns='80px 920px',
            grid_template_rows='569px',
            grid_gap='0px 0px')
       )
 
Back to top