filename = 'simple_matrix_plot_example' doc = Document(filename, doc_type='standalone', filepath=filepath, border='1cm') # Create the data X = np.linspace(-3, 3, 11) Y = np.linspace(-3, 3, 21) # Create a plot plot = doc.new( Plot(plot_name=filename, plot_path=filepath, as_float_env=False, grid=False, lines=False, enlargelimits='false', width=r'.5\textwidth', height=r'.5\textwidth')) XX, YY = np.meshgrid(X, Y) Z = np.exp( -(XX**2 + YY**2) / 6 ).T # Transpose is necessary because numpy puts the x dimension along columns and y dimension along rows, which is the opposite of a standard graph. plot.add_matrix_plot(X, Y, Z) # Adding titles and labels plot.x_label = 'X axis' plot.y_label = 'Y axis' plot.title = 'Some title'
from python2latex import Document, Plot import numpy as np # Document type 'standalone' will only show the plot, but does not support all tex environments. filepath = './examples/simple plot example/' filename = 'simple_plot_example' doc = Document(filename, doc_type='standalone', filepath=filepath) # Create the data X = np.linspace(0,2*np.pi,100) Y1 = np.sin(X) Y2 = np.cos(X) # Create a plot plot = doc.new(Plot(X, Y1, X, Y2, plot_path=filepath, as_float_env=False)) tex = doc.build()
# Create a dynamical palette which generates as many colors as needed from the cmap. Note that by default, the range of color used expands with the number of colors. palette = Palette(colors=cmap, color_model='rgb') pal = Palette(colors=cmap, n_colors=2) # Create the data X = np.linspace(-1, 1, 50) Y = lambda c: np.exp(X * c) + c # Let us compare the different color palettes generated for different number of line plots for n_colors in [2, 3, 5, 10]: # Create a plot plot = doc.new( Plot( plot_name=filename + f'n_colors={n_colors}', plot_path=filepath, width='\\textwidth', height='.21\\paperheight', palette=palette, )) for c in np.linspace(.5, 1.5, n_colors): plot.add_plot(X, Y(c), label=f'\\footnotesize $c={c:.3f}$' ) # Add labels (default is at end of line plot) plot.x_min = -1 plot.x_max = 1.3 doc += '\n' tex = doc.build()
data = np.full((n_datasets, n_models), 0, dtype=object) for i, row in enumerate(data): for j, _ in enumerate(row): data[i, j] = MeanWithStd(np.random.rand(n_draws)) # Third, create the table # Create a basic document doc = Document(filename='mean_with_std_table_example', filepath='examples/table examples', doc_type='article', options=('12pt', )) # Create table col, row = n_models + 1, n_datasets + 2 table = doc.new(Table(shape=(row, col), float_format='.3f')) # Set a caption table.caption = f'Mean test accuracy (and standard deviation) of {n_models} models on {n_datasets} datasets for {n_draws} different random states.' table.caption_space = '10pt' # Space between table and caption. # Set entries with slices table[2:, 1:] = data table[2:, 0] = [f'Dataset {i}' for i in range(n_datasets)] table[0, 1:] = 'Models' table[1, 1:] = [f'Model {i}' for i in range(n_models)] # Add rules table[1].add_rule() table[0, 1:3].add_rule(trim_left=True, trim_right=True) table[0, 3:].add_rule(trim_left=True, trim_right=True)
from python2latex import Document, Table import numpy as np # Create the document of type standalone doc = Document(filename='simple_table_from_numpy_array_example', filepath='examples/table examples', doc_type='standalone', border='10pt') # Create the data col, row = 4, 4 data = np.random.rand(row, col) # Create the table and add it to the document at the same time table = doc.new(Table(shape=(row+2, col+1), as_float_env=False)) # No float environment in standalone documents # Set entries with a slice directly from a numpy array! table[2:,1:] = data # Set a columns title as a multicell with a simple slice assignment table[0,1:] = 'Col title' # Set whole lines or columns in a single line with lists table[1,1:] = [f'Col{i+1}' for i in range(col)] table[2:,0] = [f'Row{i+1}' for i in range(row)] # Add rules where you want table[1,1:].add_rule() # Automatically highlight the best value(s) inside the specified slice, ignoring text for r in range(2,row+2): table[r].highlight_best('high', 'bold') # Best per row tex = doc.build() print(tex)
doc += f'{n_colors} colors: \\hspace{{10pt}}' tikzpicture = doc.new( TexEnvironment('tikzpicture', options=['baseline=-.5ex'])) for i, color in zip(range(n_colors), palette): tikzpicture += Node((i, 0), fill=color) doc += ' ' if __name__ == "__main__": # Create document filepath = './examples/plot examples/predefined palettes comparison/' filename = 'PREDEFINED_PALETTES_comparison' doc = Document(filename, doc_type='article', filepath=filepath) # Insert title center = doc.new(TexEnvironment('center')) center += r"\huge \bf Predefined color maps and palettes" doc += """\\noindent python2latex provides three color maps natively. They are defined in the JCh axes of the CIECAM02 color model, which is linear to human perception of colors. Moreover, three ``dynamic'' palettes have been defined, one for each color map. They are dynamic in that the range of colors used to produce the palette changes with the number of colors needed. This allows for a good repartition of hues and brightness for all choices of number of colors. All three color maps have been designed to be colorblind friendly for all types of colorblindness. To do so, all color maps are only increasing or decreasing in lightness, which helps to distinguish hues that may look similar to a colorblind. This also has the advantage that the palettes are also viable in levels of gray. """ # First section sec = doc.new_section(r'The \texttt{holi} color map') sec += """ The ``holi'' color map was designed to provide a set of easily distinguishable hues for any needed number of colors. It is optimized for palettes of 5 or 6 colors, but other numbers of color also generate very good palettes. It is colorblind friendly for all types of colorblindness for up to 5 colors, but can still be acceptable for more colors. The name ``holi'' comes from the Hindu festival of colors. This is the default color map of python2latex. Below is a graph of the color map in the J-h axes of the CIECAM02 color model, followed by the colors generated according to the number of colors needed. """
cmap_range=(0,1), n_colors=n_colors, color_transform=lambda color: (rgb2JCh(hsv_to_rgb(color))[0]/100,) ) palette_hsb_gray = Palette( cmap_hsb, color_model='gray', cmap_range=(0,1), n_colors=n_colors, color_transform=lambda color: (rgb2gray(hsv_to_rgb(color)),) ) # Create plots to compare the cmaps plot_color = doc.new(Plot(plot_path=filepath, plot_name=filename+'_color', lines='3pt', height='6cm', )) plot_lightness = doc.new(Plot(plot_path=filepath, plot_name=filename+'_lightness', lines='3pt', height='6cm', )) plot_gray = doc.new(Plot(plot_path=filepath, plot_name=filename+'_gray', lines='3pt', height='6cm', )) def unfold_hue(h): """Adds 360 degrees of hue when the hue value is less than the starting value to make sure the plots are continuous."""