简体   繁体   中英

matplotlib equal spacing between datapoints

I'm trying do plot some data with the x axis showing equidistant spacing between the data points.

The code:

#!/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()

How it looks: 在此处输入图片说明 How I want it to look:

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

So the distance between each two neighboring points is equal.

So, it seems your graph is just for representation purpose. The numbers on the x-axis do not need to be on a scale. To plot this, you must create a x axis list which is actually to scale and just replace its labels by the elements of your sizes list. The following code shows how to do this

#!/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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM