Certainly! Below is a Python demo program that uses tkinter with a Motif style theme and plots an efficient frontier visualization (volatility vs annualized returns) for three indices with mocked data points.
- It uses matplotlib only for plotting (as you allowed it if strictly necessary).
- The GUI is styled with the Motif theme using
ttk and ttk.Style.
- The plot is embedded inside the tkinter window using
FigureCanvasTkAgg.
What the demo does:
- Creates mock data for 3 indices (scatter points of volatility vs returns).
- Displays a matplotlib scatter plot inside a tkinter window.
- Uses Motif style for the tkinter widgets.
Code:
import tkinter as tk
from tkinter import ttk
import random
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
# Mock data generation for 3 indices
def generate_mock_data(num_points=30):
data = {}
for index_name in ['Index A', 'Index B', 'Index C']:
# Volatility between 5% and 30%
volatility = [random.uniform(0.05, 0.3) for _ in range(num_points)]
# Returns roughly correlated with volatility but with noise
returns = [v * random.uniform(0.8, 1.5) for v in volatility]
data[index_name] = (volatility, returns)
return data
class EfficientFrontierApp(tk.Tk):
def __init__(self):
super().__init__()
self.title("Efficient Frontier Visualization")
self.geometry("800x600")
# Set Motif style
style = ttk.Style(self)
style.theme_use('clam') # 'clam' is closest to Motif in ttk, Motif itself may not be available
# If motif theme is available on your system, use:
# style.theme_use('aqua') or 'motif' if your system supports it
# Create matplotlib figure and axes
self.fig, self.ax = plt.subplots(figsize=(7, 5))
self.ax.set_title("Efficient Frontier (Volatility vs Annualized Returns)")
self.ax.set_xlabel("Volatility (Annualized Std Dev)")
self.ax.set_ylabel("Annualized Returns")
self.ax.grid(True)
# Generate mock data
self.data = generate_mock_data()
# Plot data
self.plot_data()
# Embed matplotlib figure in tkinter
self.canvas = FigureCanvasTkAgg(self.fig, master=self)
self.canvas.draw()
self.canvas.get_tk_widget().pack(fill=tk.BOTH, expand=1)
def plot_data(self):
colors = ['red', 'green', 'blue']
for (index_name, (volatility, returns)), color in zip(self.data.items(), colors):
self.ax.scatter(volatility, returns, label=index_name, alpha=0.7, color=color)
self.ax.legend()
if __name__ == "__main__":
app = EfficientFrontierApp()
app.mainloop()
Notes:
- The
ttk.Style().theme_use('motif') may not be available on all platforms. The closest built-in theme is 'clam' which has a similar look and feel. If your system supports 'motif', replace 'clam' with 'motif'.
- The mock data is randomly generated each run.
- The plot shows volatility on the x-axis and annualized returns on the y-axis for three indices.
If you want me to help you customize the GUI further or add interactive features, just ask!