如何为 matplotlib.pyplot.scatter() 函数设置 X 和 Y 轴间隔?

3

我有以下用于绘制两个变量'field_size'和'field_mean_LAI'的代码:

plt.figure(figsize=(20,10))
plt.scatter(df.field_size, df.field_lai_mean)
plt.title("Field Size and LAI Plot")
plt.xlabel("Field Size")
plt.ylabel("Mean LAI")
plt.show()

结果是散点图: "enter image description here 如何配置x和y轴的间隔,并改变绘图的颜色?我是Python绘图的新手。

3
你能更准确地描述需要什么吗?“配置x和y间隔并更改绘图的颜色”是什么意思? - JohanC
1
也许这个 Seaborn 教程中的一些二维图表会很有趣?链接 - JohanC
1
嗨,能够提供有关预期输出的清晰信息会很好。您可以通过在plt.scatter中提供“c”或“color”来更改点的颜色。此外,以前已经问过与格式相关的问题。这是一个例子。https://dev59.com/x2cs5IYBdhLWcg3wu2jS - Grayrigel
感谢您的建议。已经找到解决方案并发布为答案。 - tahmid0945
2个回答

3

所以我修改了我的代码如下:

plt.figure(figsize=(20,10))
plt.scatter(df.field_size, df.field_lai_mean)
plt.title("Field Size and LAI Plot")
plt.xlabel("Field Size")
plt.ylabel("Mean LAI")

plt.xticks(np.arange(0.00000,0.00013,step=0.000008))
plt.yticks(np.arange(0,8.5,step=0.5))

plt.show()

现在我有一个像这样的情节:

enter image description here

刚刚定义了xticks和yticks函数,顺利完成!:)


1

要更改绘图点的颜色,可以使用color属性(见下文)。

在创建子图时,可以通过传递xlimylim参数来设置两个轴的限制。

还要注意,我在这里传递了rot参数,以设置x标签的旋转。

要配置xy间隔,您可以使用例如MultipleLocator,用于majorminor刻度线。还有其他定位器,请搜索Web获取详细信息。

您还可以设置网格(就像我所做的那样)。

因此,您可以将代码更改为:

import matplotlib.pyplot as plt
import matplotlib.ticker as tck

fig = plt.figure(figsize=(10,5))
ax = plt.subplot(xlim=(-0.000003, 0.000123), ylim=(-0.5, 9))
df.plot.scatter(x='field_size', y='field_lai_mean', rot=30, color='r', ax=ax)
plt.title("Field Size and LAI Plot")
plt.xlabel("Field Size")
plt.ylabel("Mean LAI")
ax = plt.gca()
ax.yaxis.set_major_locator(tck.MultipleLocator(2))
ax.yaxis.set_minor_locator(tck.MultipleLocator(0.4))
ax.xaxis.set_major_locator(tck.MultipleLocator(0.00001))
ax.xaxis.set_minor_locator(tck.MultipleLocator(0.0000025))
ax.grid()
plt.show()

当然,根据您的需求调整传递的值。
对于一组非常有限的点,我得到了以下图片:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接