making matplotlib graphs look like R by default?

后端 未结 8 1726
自闭症患者
自闭症患者 2021-01-29 23:02

Is there a way to make matplotlib behave identically to R, or almost like R, in terms of plotting defaults? For example R treats its axes pretty differently from

8条回答
  •  北海茫月
    2021-01-29 23:35

    Setting spines in matplotlibrc explains why it is not possible to simply edit Matplotlib defaults to produce R-style histograms. For scatter plots, R style data-axis buffer in matplotlib and In matplotlib, how do you draw R-style axis ticks that point outward from the axes? show some defaults that can be changed to give a more R-ish look. Building off some of the other answers, the following function does a decent job of mimicking R's histogram style, assuming you've called hist() on your Axes instance with facecolor='none'.

    def Rify(axes):
        '''
        Produce R-style Axes properties
        '''
        xticks = axes.get_xticks() 
        yticks = axes.get_yticks()
    
        #remove right and upper spines
        axes.spines['right'].set_color('none') 
        axes.spines['top'].set_color('none')
    
        #make the background transparent
        axes.set_axis_bgcolor('none')
    
        #allow space between bottom and left spines and Axes
        axes.spines['bottom'].set_position(('axes', -0.05))
        axes.spines['left'].set_position(('axes', -0.05))
    
        #allow plot to extend beyond spines
        axes.spines['bottom'].set_bounds(xticks[0], xticks[-2])
        axes.spines['left'].set_bounds(yticks[0], yticks[-2])
    
        #set tick parameters to be more R-like
        axes.tick_params(direction='out', top=False, right=False, length=10, pad=12, width=1, labelsize='medium')
    
        #set x and y ticks to include all but the last tick
        axes.set_xticks(xticks[:-1])
        axes.set_yticks(yticks[:-1])
    
        return axes
    

提交回复
热议问题