python - matplotlib 数据点之间的间距相等

标签 python matplotlib plot

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

代码:

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

因此每两个相邻点之间的距离相等。

最佳答案

所以,看来你的图表只是为了表示目的。 X 轴上的数字不需要在刻度上。要绘制此图,您必须创建一个实际上按比例缩放的 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()

关于python - matplotlib 数据点之间的间距相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47240638/

相关文章:

python/matplotlib/seaborn- x 轴上带有数据点的箱线图

python - 如何在Python中找到循环中图的所有交点?

r - 是否有交互式输出设备可以在 R 中查看 3D 图形?

python - 使用 Keras 进行整数序列预测

c++ - Python解释器在加载DLL时退出

python - 使用 numpy 和 matplotlib 进行 gnuplot 风格索引绘图

r - 需要在网格中的图形中有序地排列 GAM 系数

python - 什么是 REST API 响应的正确方法?

python - map 是如何工作的

python - 在 matplotlib 中扩展线段