I’m working on a Python project for portfolio optimization using the PyPortfolioOpt library. When I try to import the plotting module and plot the efficient frontier, I get the following error:
oserror: 'seaborn-deep' is not a valid package style, path of style file, URL of style file, or library style name (library styles are listed in `style.available`)
Here’s a simplified version of my code:
# Importation of libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pypfopt import EfficientFrontier, risk_models, expected_returns, plotting
# ... (data loading and processing)
# Plotting the efficient frontier
ef = EfficientFrontier(mu, S, weight_bounds=(0, 1))
fig, ax = plt.subplots(figsize=(10, 7))
# Generate the efficient frontier
plotting.plot_efficient_frontier(ef, ax=ax, show_assets=False)
# Mark the minimum volatility portfolio
ef.min_volatility()
ret_min_vol, vol_min_vol, _ = ef.portfolio_performance()
ax.scatter(vol_min_vol, ret_min_vol, marker='*', s=200, c='r', label='Minimum Volatility Portfolio')
# Mark the maximum Sharpe ratio portfolio
ef = EfficientFrontier(mu, S)
ef.max_sharpe()
ret_max_sharpe, vol_max_sharpe, _ = ef.portfolio_performance()
ax.scatter(vol_max_sharpe, ret_max_sharpe, marker='*', s=200, c='g', label='Maximum Sharpe Ratio Portfolio')
# Labels and titles
ax.set_title('Efficient Frontier')
ax.set_xlabel('Volatility (Risk)')
ax.set_ylabel('Expected Return')
ax.legend()
plt.show()
When I run this code, I get the error mentioned above. However, if I remove the plotting module and the plotting code, everything runs fine.
I’ve tried the following to fix the issue:
Reinstalled PyPortfolioOpt using pip install PyPortfolioOpt –upgrade.
Installed older versions of PyPortfolioOpt to see if the issue persists.
Ensured that seaborn and matplotlib are up to date by running pip install seaborn –upgrade and pip install matplotlib –upgrade.
Verified that seaborn is installed and can be imported without issues.
Additional Information:
I’m using Python 3.12.
My current versions of the packages are:
PyPortfolioOpt: 1.5.5
matplotlib: 3.8.0
seaborn: 0.13.0
The output of plt.style.available does not include ‘seaborn-deep’.
Questions:
Why am I getting this error when importing plotting from PyPortfolioOpt?
Is there a compatibility issue with Python 3.12 or specific versions of matplotlib or seaborn?
How can I fix this error so I can plot the efficient frontier using PyPortfolioOpt?
Any help would be greatly appreciated!
G_brandao is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.