Adjusting gridlines on a 3D Matplotlib figure

前端 未结 4 2185
感动是毒
感动是毒 2021-02-15 03:05

I\'m getting ready for a presentation and I have some example figures of 3D matplotlib figures. However, the gridlines are too light to see on the projected images.

相关标签:
4条回答
  • 2021-02-15 03:48

    If you don't mind having all the lines thicker then you could adjust the default rc settings.

    Given a graph such as:

    enter image description here

    We can add:

    import matplotlib as mpl
    mpl.rcParams['lines.linewidth'] = 2
    

    To increase the default line width of all lines, giving a result of:

    enter image description here

    Alternatively, if you feel this looks ugly, you could use:

    ax.w_xaxis.gridlines.set_lw(3.0)
    ax.w_yaxis.gridlines.set_lw(3.0)
    ax.w_zaxis.gridlines.set_lw(3.0)
    

    to adjust the line width of each axis to 3.0, producing:

    enter image description here

    In order to update the colour, so the grid-lines really pop, you could add:

    ax.w_xaxis._axinfo.update({'grid' : {'color': (0, 0, 0, 1)}})
    ax.w_yaxis._axinfo.update({'grid' : {'color': (0, 0, 0, 1)}})
    ax.w_zaxis._axinfo.update({'grid' : {'color': (0, 0, 0, 1)}})
    

    Which produces:

    enter image description here

    The methods are pretty hacky, but as far as I am aware there is no simpler way of achieving these results!! Hope this helps; let me know if you require any further assistance!

    0 讨论(0)
  • 2021-02-15 04:05

    If to lighten the background of grid, can use setting the pane color more light (eg:white) using Axes3DSubplot object as below.

    ax.w_xaxis.pane.set_color('w');
    ax.w_yaxis.pane.set_color('w');
    ax.w_zaxis.pane.set_color('w');
    

    Or else to highlight the grid lines further, can updated grid color parameter of plot.

    plt.rcParams['grid.color'] = "black"
    
    0 讨论(0)
  • 2021-02-15 04:08

    Unfortunately this doesn't seem to be exposed. Looking over the source, the key internal variable is call _AXINFO which we can override by careful subclassing.

    enter image description here

    Add this code after your figure is created, and style it with the dict custom_AXINFO:

    from mpl_toolkits.mplot3d import Axes3D
    import mpl_toolkits.mplot3d.axis3d as axis3d
    
    # New axis settings
    custom_AXINFO = {
        'x': {'i': 0, 'tickdir': 1, 'juggled': (1, 0, 2),
              'color': (0.00, 0.00, 0.25, .75)},
        'y': {'i': 1, 'tickdir': 0, 'juggled': (0, 1, 2),
              'color': (0.20, 0.90, 0.90, 0.25)},
        'z': {'i': 2, 'tickdir': 0, 'juggled': (0, 2, 1),
              'color': (0.925, 0.125, 0.90, 0.25)},}
    
    class custom_XAxis(axis3d.Axis):
        _AXINFO = custom_AXINFO
    
    class custom_YAxis(axis3d.Axis):
        _AXINFO = custom_AXINFO
    
    class custom_ZAxis(axis3d.Axis):
        _AXINFO = custom_AXINFO
    
    class custom_Axes3D(Axes3D):
        def _init_axis(self):
            '''Init 3D axes; overrides creation of regular X/Y axes'''
            self.w_xaxis = custom_XAxis('x', self.xy_viewLim.intervalx,
                                        self.xy_dataLim.intervalx, self)
            self.xaxis = self.w_xaxis
            self.w_yaxis = custom_YAxis('y', self.xy_viewLim.intervaly,
                                self.xy_dataLim.intervaly, self)
            self.yaxis = self.w_yaxis
            self.w_zaxis = custom_ZAxis('z', self.zz_viewLim.intervalx,
                                self.zz_dataLim.intervalx, self)
            self.zaxis = self.w_zaxis
    
            for ax in self.xaxis, self.yaxis, self.zaxis:
                ax.init3d()
    
    # The rest of your code below, note the call to our new custom_Axes3D
    
    points = (5*np.random.randn(3, 50)+np.tile(np.arange(1,51), (3, 1))).transpose()
    fig = plt.figure(figsize = (10,10)) 
    ax = custom_Axes3D(fig)
    

    This is monkey-patching at it's worst, and should not be relied upon to work for later versions.

    Fixing the facecolors was easier than the grid lines, as this requires an override of one of the __init__ methods, though it could be done with more work.

    It does not seem difficult to expose this to the end user, and as such I can imagine that this may be fixed in later versions.

    0 讨论(0)
  • 2021-02-15 04:10

    Just use this to change the linewidth:

    plt.rcParams['grid.linewidth'] = 2
    

    Full scripts to plot the figure:

    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D 
    
    points = (5*np.random.randn(3, 50)+np.tile(np.arange(1,51), (3, 1))).transpose()
    fig = plt.figure(figsize = (10,10))
    ax = fig.add_subplot(111, projection='3d') 
    ax.scatter(points[:,0], points[:,1], points[:,2])
    #ax.view_init(elev=0., azim=0)
    ax.set_ylim([0, 60])
    ax.set_zlim([0, 60])
    ax.set_xlim([0, 60])
    ax.set_zlabel('Cytokine')
    ax.set_ylabel('Parameter')
    plt.rcParams['grid.linewidth'] = 4   # change linwidth
    plt.rcParams['grid.color'] = "black" # change color
    
    0 讨论(0)
提交回复
热议问题