【问题标题】:Setting default scatter plot marker in matplotlib在 matplotlib 中设置默认散点图标记
【发布时间】:2016-07-26 15:41:48
【问题描述】:

我正在寻找一种方法来为 plt.scatter() 设置默认标记 - 我尝试在我的 matplotlibrc 文件中设置这一行:

lines.marker    : +

但这似乎并没有改变默认标记,即一个圆圈。

http://matplotlib.org/users/customizing.html 似乎也没有任何其他明显的参数可以设置;有谁知道是否真的可以为散点图设置默认标记?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    您可以通过直接更改site-packages/matplotlib/pyplot.py中的代码来做到这一点:

    # This function was autogenerated by boilerplate.py.  Do not edit as
    # changes will be lost
    @_autogen_docstring(Axes.scatter)
    def scatter(x, y, s=20, c=None, marker='+', cmap=None, norm=None, vmin=None,
                vmax=None, alpha=None, linewidths=None, verts=None, edgecolors=None,
                hold=None, data=None, **kwargs):
        ax = gca()
        # allow callers to override the hold state by passing hold=True|False
        washold = ax.ishold()
    

    请注意评论中的警告,因为我不确定何时调用boilerplate.py。(您只需要尝试一下即可)但是在实际更改源之前,请考虑重叠@ 987654324@您自己的电话:

    import matplotlib.pyplot as plt
    import numpy as np
    
    class scatter():
        def __init__(self,x,y):
            self.marker = '+'
            plt.scatter(x,y,marker=self.marker)
    
    x,y = np.random.randint(0,100,30),np.random.randint(0,100,30)
    scatter(x,y)
    plt.show()
    

    【讨论】:

    • 这似乎工作正常,但我认为我不应该直接更改 matplotlib 代码!如果有人感兴趣,我在这里提出了一个问题:github.com/matplotlib/matplotlib/issues/6272
    • @David Stansby 我同意,因此我建议“重叠”散射函数。从源代码来看,我认为您没有其他方法可以实现这一点,marker 的默认参数非常简单(认为 Python 中可能存在一些我忽略的过程)。
    猜你喜欢
    • 1970-01-01
    • 2017-12-13
    • 2021-10-10
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    相关资源
    最近更新 更多