├── .gitignore ├── LICENSE ├── Procfile ├── README.md ├── app.py ├── parameters.txt ├── plot.py ├── requirements.txt └── setup.sh /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Dominik Haitz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: sh setup.sh && streamlit run app.py 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # matplotlib-style-configurator 2 | Matplotlib style configuratior, built with Streamlit 3 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | from pathlib import Path 4 | import streamlit as st 5 | import matplotlib.pyplot as plt 6 | 7 | import plot 8 | import base64 9 | 10 | 11 | # Title 12 | st.title("Matplotlib Style Configurator") 13 | st.markdown("""[GitHub repository](https://github.com/dhaitz/matplotlib-style-configurator) - 14 | [Plotting code](https://matplotlib.org/gallery/style_sheets/style_sheets_reference.html) - 15 | [Matplotlib style gallery ](https://tonysyu.github.io/raw_content/matplotlib-style-gallery/gallery.html) - 16 | [mplcyberpunk](https://github.com/dhaitz/mplcyberpunk) - 17 | [MPL stylesheets](https://github.com/dhaitz/matplotlib-stylesheets)""") 18 | 19 | 20 | # Sidebar: basic config selectors 21 | base_styles = ['default'] + [style for style in plt.style.available if not style.startswith('_')] + [ 22 | "cyberpunk", 23 | "https://raw.githubusercontent.com/dhaitz/matplotlib-stylesheets/master/pitayasmoothie-dark.mplstyle", 24 | "https://raw.githubusercontent.com/dhaitz/matplotlib-stylesheets/master/pacoty.mplstyle", 25 | "https://raw.githubusercontent.com/dhaitz/matplotlib-stylesheets/master/pitayasmoothie-light.mplstyle", 26 | ] 27 | style = st.sidebar.selectbox("Choose base style:", base_styles) 28 | plt.style.use(style) 29 | 30 | n_columns = st.sidebar.selectbox("Number of columns", [1, 2, 3, 6], index=2) 31 | 32 | 33 | # Sidebar: parameter customization widgets 34 | st.sidebar.header("Customize style:") 35 | st.sidebar.text("(Parameter list is non-exhaustive)") 36 | params = Path('parameters.txt').read_text().splitlines() 37 | for param in params: 38 | widget_type = st.sidebar.checkbox if (type(plt.rcParams[param]) == bool) else st.sidebar.text_input 39 | 40 | if type(plt.rcParams[param]) == list: # can't put lists in text boxes -> use only first item 41 | plt.rcParams[param] = [widget_type(param, value=plt.rcParams[param][0])] 42 | else: 43 | plt.rcParams[param] = widget_type(param, value=plt.rcParams[param]) 44 | 45 | 46 | # Draw plot 47 | fig = plot.plot_figure(style_label=style, n_columns=n_columns) 48 | st.pyplot(fig=fig) 49 | plt.close(fig) 50 | 51 | # Link to download stylesheet 52 | def get_stylesheet_download_link(params, filename="my_style.mplstyle"): 53 | """Generates a download link for a stylesheet file. https://discuss.streamlit.io/t/heres-a-download-function-that-works-for-dataframes-and-txt/4052""" 54 | 55 | stylesheet_lines = [] 56 | for param in params: 57 | if plt.rcParamsDefault[param] != plt.rcParams[param]: # only store parameters which were changed from the defaults. 58 | if type(plt.rcParams[param]) == list: 59 | value = ', '.join(plt.rcParams[param]) 60 | else: 61 | value = plt.rcParams[param] 62 | stylesheet_lines.append(f"{param}: {value}".replace('#', '')) 63 | 64 | stylesheet_text = '\n'.join(stylesheet_lines) 65 | b64 = base64.b64encode(stylesheet_text.encode()).decode() 66 | return f'Download stylesheet (Installation instructions)' 67 | 68 | st.markdown(get_stylesheet_download_link(params), unsafe_allow_html=True) 69 | 70 | 71 | # workaround to open in wide mode (https://github.com/streamlit/streamlit/issues/314#issuecomment-579274365) 72 | max_width_str = f"max-width: 1000px;" 73 | st.markdown(f"""""", unsafe_allow_html=True) 74 | -------------------------------------------------------------------------------- /parameters.txt: -------------------------------------------------------------------------------- 1 | axes.edgecolor 2 | axes.facecolor 3 | axes.grid 4 | axes.labelcolor 5 | axes.labelsize 6 | axes.linewidth 7 | axes.prop_cycle 8 | axes.titlesize 9 | figure.facecolor 10 | font.family 11 | font.sans-serif 12 | font.serif 13 | font.size 14 | grid.color 15 | grid.linestyle 16 | grid.linewidth 17 | image.cmap 18 | legend.fancybox 19 | legend.fontsize 20 | lines.color 21 | lines.linewidth 22 | lines.markersize 23 | lines.solid_capstyle 24 | patch.edgecolor 25 | patch.facecolor 26 | patch.linewidth 27 | text.color 28 | xtick.color 29 | xtick.direction 30 | xtick.labelsize 31 | xtick.major.pad 32 | xtick.major.size 33 | xtick.major.width 34 | xtick.minor.size 35 | xtick.minor.width 36 | ytick.color 37 | ytick.direction 38 | ytick.labelsize 39 | ytick.major.pad 40 | ytick.major.size 41 | ytick.major.width 42 | ytick.minor.size 43 | ytick.minor.width 44 | -------------------------------------------------------------------------------- /plot.py: -------------------------------------------------------------------------------- 1 | """ 2 | Source: https://matplotlib.org/gallery/style_sheets/style_sheets_reference.html 3 | """ 4 | 5 | import matplotlib.pyplot as plt 6 | import numpy as np 7 | import math 8 | import mplcyberpunk 9 | 10 | 11 | def plot_scatter(ax, prng, nb_samples=100): 12 | """Scatter plot. 13 | """ 14 | for mu, sigma, marker in [(-.5, 0.75, 'o'), (0.75, 1., 's')]: 15 | x, y = prng.normal(loc=mu, scale=sigma, size=(2, nb_samples)) 16 | ax.plot(x, y, ls='none', marker=marker) 17 | ax.set_xlabel('X-label') 18 | ax.set_title('Axes title') 19 | return ax 20 | 21 | 22 | def plot_colored_sinusoidal_lines(ax): 23 | """Plot sinusoidal lines with colors following the style color cycle. 24 | """ 25 | L = 2 * np.pi 26 | x = np.linspace(0, L) 27 | nb_colors = len(plt.rcParams['axes.prop_cycle']) 28 | shift = np.linspace(0, L, nb_colors, endpoint=False) 29 | for s in shift: 30 | ax.plot(x, np.sin(x + s), '-') 31 | ax.set_xlim([x[0], x[-1]]) 32 | return ax 33 | 34 | 35 | def plot_bar_graphs(ax, prng, min_value=5, max_value=25, nb_samples=5): 36 | """Plot two bar graphs side by side, with letters as x-tick labels. 37 | """ 38 | x = np.arange(nb_samples) 39 | ya, yb = prng.randint(min_value, max_value, size=(2, nb_samples)) 40 | width = 0.25 41 | ax.bar(x, ya, width) 42 | ax.bar(x + width, yb, width, color='C2') 43 | ax.set_xticks(x + width) 44 | ax.set_xticklabels(['a', 'b', 'c', 'd', 'e']) 45 | return ax 46 | 47 | 48 | def plot_colored_circles(ax, prng, nb_samples=15): 49 | """Plot circle patches. 50 | 51 | NB: draws a fixed amount of samples, rather than using the length of 52 | the color cycle, because different styles may have different numbers 53 | of colors. 54 | """ 55 | for sty_dict, j in zip(plt.rcParams['axes.prop_cycle'], range(nb_samples)): 56 | ax.add_patch(plt.Circle(prng.normal(scale=3, size=2), 57 | radius=1.0, color=sty_dict['color'])) 58 | # Force the limits to be the same across the styles (because different 59 | # styles may have different numbers of available colors). 60 | ax.set_xlim([-4, 8]) 61 | ax.set_ylim([-5, 6]) 62 | ax.set_aspect('equal', adjustable='box') # to plot circles as circles 63 | return ax 64 | 65 | 66 | def plot_image_and_patch(ax, prng, size=(20, 20)): 67 | """Plot an image with random values and superimpose a circular patch. 68 | """ 69 | values = prng.random_sample(size=size) 70 | ax.imshow(values, interpolation='none') 71 | c = plt.Circle((5, 5), radius=5, label='patch') 72 | ax.add_patch(c) 73 | # Remove ticks 74 | ax.set_xticks([]) 75 | ax.set_yticks([]) 76 | 77 | 78 | def plot_histograms(ax, prng, nb_samples=10000): 79 | """Plot 4 histograms and a text annotation. 80 | """ 81 | params = ((10, 10), (4, 12), (50, 12), (6, 55)) 82 | for a, b in params: 83 | values = prng.beta(a, b, size=nb_samples) 84 | ax.hist(values, histtype="stepfilled", bins=30, 85 | alpha=0.8, density=True) 86 | # Add a small annotation. 87 | ax.annotate('Annotation', xy=(0.25, 4.25), 88 | xytext=(0.9, 0.9), textcoords=ax.transAxes, 89 | va="top", ha="right", 90 | bbox=dict(boxstyle="round", alpha=0.2), 91 | arrowprops=dict( 92 | arrowstyle="->", 93 | connectionstyle="angle,angleA=-95,angleB=35,rad=10"), 94 | ) 95 | return ax 96 | 97 | 98 | def plot_figure(style_label="", n_columns=6): 99 | """Setup and plot the demonstration figure with a given style. 100 | """ 101 | # Use a dedicated RandomState instance to draw the same "random" values 102 | # across the different figures. 103 | prng = np.random.RandomState(96917002) 104 | 105 | # Tweak the figure size to be better suited for a row of numerous plots: 106 | # double the width and halve the height. NB: use relative changes because 107 | # some styles may have a figure size different from the default one. 108 | n_rows = math.ceil(6/n_columns) 109 | 110 | (fig_width, fig_height) = plt.rcParams['figure.figsize'] 111 | fig_size = [fig_width * n_columns, fig_height * n_rows] 112 | 113 | fig, axs = plt.subplots(ncols=n_columns, 114 | nrows=n_rows, 115 | num=style_label, 116 | figsize=fig_size, 117 | squeeze=False) 118 | axs = [item for sublist in axs for item in sublist] 119 | #axs[0].set_ylabel(style_label) 120 | 121 | plot_scatter(axs[0], prng) 122 | plot_image_and_patch(axs[1], prng) 123 | plot_bar_graphs(axs[2], prng) 124 | plot_colored_circles(axs[3], prng) 125 | plot_colored_sinusoidal_lines(axs[4]) 126 | plot_histograms(axs[5], prng) 127 | 128 | if style_label == 'cyberpunk': 129 | for ax in axs: 130 | mplcyberpunk.make_lines_glow(ax) 131 | 132 | fig.tight_layout() 133 | 134 | return fig 135 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | streamlit 2 | matplotlib 3 | mplcyberpunk 4 | -------------------------------------------------------------------------------- /setup.sh: -------------------------------------------------------------------------------- 1 | mkdir -p ~/.streamlit/ 2 | 3 | echo "\ 4 | [general]\n\ 5 | email = \"\"\n\ 6 | " > ~/.streamlit/credentials.toml 7 | 8 | echo "\ 9 | [server]\n\ 10 | headless = true\n\ 11 | enableCORS=false\n\ 12 | port = $PORT\n\ 13 | " > ~/.streamlit/config.toml 14 | --------------------------------------------------------------------------------