【问题标题】:scatter plot with single pixel marker in matplotlibmatplotlib 中带有单个像素标记的散点图
【发布时间】:2017-02-06 18:35:46
【问题描述】:

我正在尝试使用散点图绘制大型数据集。 我想使用 matplotlib 用单个像素标记来绘制它。 好像已经解决了。

https://github.com/matplotlib/matplotlib/pull/695

但我找不到有关如何获取单个像素标记的信息。

我的简化数据集(data.csv)

Length,Time
78154393,139.324091
84016477,229.159305
84626159,219.727537
102021548,225.222662
106399706,221.022827
107945741,206.760239
109741689,200.153263
126270147,220.102802
207813132,181.67058
610704756,50.59529
623110004,50.533158
653383018,52.993885
659376270,53.536834
680682368,55.97628
717978082,59.043843

我的代码如下。

import pandas as pd
import os
import numpy
import matplotlib.pyplot as plt

inputfile='data.csv'
iplevel = pd.read_csv(inputfile)
base = os.path.splitext(inputfile)[0]

fig = plt.figure()
plt.yscale('log')
#plt.xscale('log')
plt.title(' My plot:  '+base)
plt.xlabel('x')
plt.ylabel('y')
plt.scatter(iplevel['Time'], iplevel['Length'],color='black',marker=',',lw=0,s=1)
fig.tight_layout()
fig.savefig(base+'_plot.png', dpi=fig.dpi)

您可以在下面看到这些点不是单个像素。

感谢任何帮助

【问题讨论】:

    标签: python matplotlib scatter-plot


    【解决方案1】:

    问题

    我担心您引用的 matplotlib git 存储库中讨论的错误修复仅对 plt.plot() 有效,对 plt.scatter() 无效

    import matplotlib.pyplot as plt
    
    fig = plt.figure(figsize=(4,2))
    ax = fig.add_subplot(121)
    ax2 = fig.add_subplot(122, sharex=ax, sharey=ax)
    ax.plot([1, 2],[0.4,0.4],color='black',marker=',',lw=0, linestyle="")
    ax.set_title("ax.plot")
    ax2.scatter([1,2],[0.4,0.4],color='black',marker=',',lw=0, s=1)
    ax2.set_title("ax.scatter")
    ax.set_xlim(0,8)
    ax.set_ylim(0,1)
    fig.tight_layout()
    print fig.dpi #prints 80 in my case
    fig.savefig('plot.png', dpi=fig.dpi)
    

    解决方案:设置标记大小

    解决方案是使用通常的"o""s" 标记,但将标记大小设置为正好一个像素。由于标记大小以点为单位,因此需要使用图形 dpi 来计算一个像素的大小(以点为单位)。这是72./fig.dpi

    • For aplot`,markersize直接是

      ax.plot(..., marker="o", ms=72./fig.dpi)
      
    • 对于scatter,标记大小通过s 参数给出,单位为方点,

      ax.scatter(..., marker='o', s=(72./fig.dpi)**2)
      

    完整示例:

    import matplotlib.pyplot as plt
    
    fig = plt.figure(figsize=(4,2))
    ax = fig.add_subplot(121)
    ax2 = fig.add_subplot(122, sharex=ax, sharey=ax)
    ax.plot([1, 2],[0.4,0.4], marker='o',ms=72./fig.dpi, mew=0, 
            color='black', linestyle="", lw=0)
    ax.set_title("ax.plot")
    ax2.scatter([1,2],[0.4,0.4],color='black', marker='o', lw=0, s=(72./fig.dpi)**2)
    ax2.set_title("ax.scatter")
    ax.set_xlim(0,8)
    ax.set_ylim(0,1)
    fig.tight_layout()
    fig.savefig('plot.png', dpi=fig.dpi)
    

    【讨论】:

    • 对我来说,设置edgecolor='' 有助于将磅值缩小很多。使用marker=','marker='.' 的工作方式取决于您希望呈现点的方式。
    【解决方案2】:

    对于仍在尝试解决此问题的任何人,我找到的解决方案是在 plt.scatter 中指定 s 参数。

    s 参数是指您正在绘制的点的面积。

    这似乎不是很完美,因为 s=1 似乎覆盖了我屏幕的大约 4 个像素,但这肯定会使它们比我能找到的任何其他东西都要小。

    https://matplotlib.org/devdocs/api/_as_gen/matplotlib.pyplot.scatter.html

    s : 标量或类似数组,形状 (n, ),可选
    大小以点^2。默认为 rcParams['lines.markersize'] ** 2.

    【讨论】:

      【解决方案3】:

      plt.scatter() 参数设置为linewidths=0 并找出参数s 的正确值。

      来源:https://stackoverflow.com/a/45803960/4063622

      【讨论】:

        猜你喜欢
        • 2017-12-13
        • 2021-10-10
        • 1970-01-01
        • 1970-01-01
        • 2019-09-19
        • 2020-09-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多