Gianluca Bianco
Gianluca Bianco

Reputation: 786

Strange message when saving a plot in pdf format with Matplotlib, why?

I am producing some plots using Matplotlib (v 3.2.2) and the mplhep tu embed the style used by my collaboration. I noticed that, if I save plots with .png format the output is normal, but if I save them in .pdf I got this message:

'texgyreheros-regular.otf' can not be subsetted into a Type 3 font. The entire font will be embedded in the output.

However it seems that the output plot is always ok. Do you know how to "correct" this or at least hide this sort of warning message? Thanks!

The function I am using to plot is the following:

def PlotVarVsTime( dataframe, title, channel ):
    """
    Function used to plot each row of the dataframe and save it.

    Args:
        dataframe ( dataframe ): the interested dataframe.
        title ( string ): the variable name for plot title.
        channel ( string ): the channel name.
    """   
    
    # Filling a plot, for each dataframe row, with points
    print( "Making plots...", end = "\n" )
    for row_index in dataframe.index:
        
        # Filling single plot for the correspinding row
        columns_container = np.array( [] )
        fig, ax = plt.subplots()
        for column in dataframe.loc[ :, dataframe.columns != "Channel" ]:
            x = Decimal( column )
            y = dataframe[ column ][ row_index ]
            ax.scatter( x, y, c = "blue" )
            columns_container = np.append( columns_container, column )
        
        # Plot settings
        time_start = ft.IntToTime( int( columns_container[ 0 ] ) )
        time_end = ft.IntToTime( int( columns_container[ -1 ] ) )
        ax.set_title( dataframe[ "Channel" ][ row_index ] + "\n" + "(" + time_start + " - " + time_end + ")", fontsize = 15 )
        ax.set_xlabel( "Time (yy/mm/dd/h/m/s)", fontsize = 15 )
        ax.set_ylabel( title, fontsize = 15 )
        ax.set_xlim( columns_container[ 0 ], columns_container[ -1 ] )
        ax.tick_params( axis = 'both', labelsize = 13 )
        ax.xaxis.offsetText.set_fontsize( 15 )

        # Saving plot
        output_name = ft.NameToStr( dataframe[ "Channel" ][ row_index ] )
        print( "Doing " + output_name + " plots..." )
        
        fig.canvas.start_event_loop( sys.float_info.min ) # Workaround for Exception in Tkinter callback
        plt.savefig( "img/" + channel + "/" + title + "/pdf/" + output_name + ".pdf", bbox_inches = "tight", dpi = 100 );
        fig.canvas.start_event_loop( sys.float_info.min ) # Workaround for Exception in Tkinter callback
        plt.savefig( "img/" + channel + "/" + title + "/png/" + output_name + ".png", bbox_inches = "tight", dpi = 100 )
        plt.clf()
        plt.close()
        
        columns_container = np.array( [] )
        
    print()
    print( "Plots have been saved in:", end = "\n" )
    print( "- PDF:", ft.Colored( "img/" + channel + "/" + title + "/pdf/", cl.OutColor.green ), end = "\n" )
    print( "- PNG:", ft.Colored( "img/" + channel + "/" + title + "/png/", cl.OutColor.green ), end = "\n" )

EDIT 1

I am using the option:

plt.style.use( hep.style.ATLAS )

from mplhep library.

Upvotes: 0

Views: 140

Answers (2)

Gianluca Bianco
Gianluca Bianco

Reputation: 786

I finally solved this issue. The problem was due to the usage of an older version of Matplotlib. Once I've updated the module the problem disappeared.

Upvotes: 0

Pantelis
Pantelis

Reputation: 156

You probably need to change the type of your font as type 3 seems not be supported for PDFs.

Try changing fonttype to 42.

import matplotlib
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42  

**Source: http://phyletica.org/matplotlib-fonts/

Upvotes: 1

Related Questions