2017-11-11 279 views
1

我正在尝试绘制一些数据,其中x轴显示数据点之间的等距间距。matplotlib数据点之间的相等间距

代码:

#!/usr/bin/env python 
from matplotlib import pyplot as plt 

sizes = [1400, 1600, 1700, 1875, 1100, 1550, 2350, 2450, 1425, 1700] 
prices = [245, 312, 279, 308, 199, 219, 405, 324, 319, 255] 

plt.xlim([1000, 2500]) 
plt.ylim([0, 500]) 
plt.xlabel("sizes") 
plt.ylabel("prices") 

plt.scatter(sizes, prices) 
plt.show() 

如何看起来: enter image description here 我多么希望它看起来:

https://www.kdnuggets.com/2017/04/simple-understand-gradient-descent-algorithm.html

所以每两个相邻之间的距离分相等。

+2

这似乎没有多大意义。如果x轴是一个带有单位的轴,并且你确实有相应的值(这里是“尺寸”),那么完整的情节就会按照你的要求进行伪造。 – ImportanceOfBeingErnest

回答

2

所以,看起来你的图只是为了表示的目的。 x轴上的数字不需要按比例。为了绘制这个图表,您必须创建一个实际按比例缩放的轴列表,并用您的sizes列表中的元素替换其标签。下面的代码显示了如何做到这一点

#!/usr/bin/env python 
from matplotlib import pyplot as plt 

sizes = [1400, 1600, 1700, 1875, 1100, 1550, 2350, 2450, 1425, 1700] 
prices = [245, 312, 279, 308, 199, 219, 405, 324, 319, 255] 

plt.ylim([0, 500]) 
plt.xlabel("sizes") 
plt.ylabel("prices") 

x = [a for a in range(len(sizes))] 
plt.scatter(x,prices) 
plt.xticks(x, sizes) 
plt.show() 
+0

原来,这并没有太大的意义,但你的答案是正确的。谢谢! – cie