我正在尝试用散点图绘制一个大型数据集。我想使用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)你可以在下面看到这些点不是单像素的。

任何帮助我们都将不胜感激
发布于 2016-09-30 03:27:19
问题所在
我担心您引用的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)

解决方案:设置markersize
解决方案是使用常用的"o"或"s"标记,但将标记大小设置为恰好一个像素。由于markersize是以点为单位给出的,因此需要使用数字dpi来计算一个像素的点的大小。这是72./fig.dpi。
For aplot`,标记大小是直接ax.plot(...,marker="o",ms=72./fig.dpi)
s参数给出的,该参数以平方点为单位,标记(...,ax.scatter=‘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)

发布于 2017-10-20 18:24:01
对于任何还在尝试解决这个问题的人来说,我找到的解决方案是在plt.scatter中指定s参数。
S参数表示要打印的点的面积。
它似乎不是很完美,因为s=1似乎覆盖了我屏幕的4个像素,但这肯定比我能找到的任何其他东西都要小。
https://matplotlib.org/devdocs/api/_as_gen/matplotlib.pyplot.scatter.html
形状s:标量或array_like,
(n,),可选
以点^2为单位的大小。默认值为rcParams'lines.markersize‘** 2。
发布于 2019-06-24 00:59:51
将plt.scatter()参数设置为linewidths=0,并计算出参数s的正确值。
https://stackoverflow.com/questions/39753282
复制相似问题