Repository navigation
Expand file tree
/
Copy pathplot_styles.py
More file actions
80 lines (49 loc) · 2.09 KB
/
Copy pathplot_styles.py
File metadata and controls
80 lines (49 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import matplotlib.pyplot as plt
import numpy as np
import os
def show_styles():
'''Prints out the names of the currently stored plotting styles that are in the plot_styles
folder.
'''
data_vis_path = os.path.dirname(os.path.realpath(__file__)) # package folder location
plt_styles_folder_path = os.path.join(data_vis_path, "plot_styles") # folder path that holds our plot styles
print(os.listdir(plt_styles_folder_path))
return
def return_styles():
'''Returns the names of the currently stored plotting styles that are in the plot_styles
folder.
'''
data_vis_path = os.path.dirname(os.path.realpath(__file__)) # package folder location
plt_styles_folder_path = os.path.join(data_vis_path, "plot_styles") # folder path that holds our plot styles
return os.listdir(plt_styles_folder_path)
def get_plot_style_path(plot_style):
'''Helper function for "set_plot_style". Take a plot style name (a *.mplstyle file), and returns the file path.
For later usage.
Inputs:
plot_style, .mplstyle file
Outputs:
plot_style_path, absolute path of the plot_style file
'''
data_vis_path = os.path.dirname(os.path.realpath(__file__)) # package folder location
plt_styles_folder_path = os.path.join(data_vis_path, "plot_styles")
try:
# Testing if "plot_style" is a string and a valid option
if not isinstance(plot_style, str):
show_styles()
raise TypeError("plot_style must be a string. Review the valid plot styles.")
elif plot_style not in os.listdir(plt_styles_folder_path):
raise ValueError
else:
plot_style_path = os.path.join(plt_styles_folder_path, plot_style)
except ValueError:
print("Please select a valid option for plot_style: ")
show_styles()
return
return plot_style_path
def set_plot_style(style):
'''Take a valid string plot style
'''
plot_path = get_plot_style_path(style)
plt.style.use(plot_path)
print(f"Successfully changed to {style}")
return