请给我画一个散点图,如附图所示。
我已经尝试了下面的代码,但它不起作用。顺便说一下,这是在蟒蛇里。
hours = [n / 3600 for n in seconds]
fig, ax = plt.subplots(figsize=(8, 6))
## Your code here
ax.plot(hours, fish_counts, marker="x")
ax.set_xlabel("Hours since low tide")
ax.set_ylabel("Jellyfish entering bay over 15 minutes")
ax.legend()[![enter image description here][1]][1]附加图像是输出的外观。谢谢。1:https://i.stack.imgur.com/5KQiz.png
发布于 2022-12-03 16:54:25
若要用所提供的数据绘制散点图,可以使用分散方法而不是绘图方法。下面是一个如何做到这一点的例子:
# import the necessary packages
import matplotlib.pyplot as plt
# define the data
hours = [n / 3600 for n in seconds]
fish_counts = [10, 12, 8, 11, 9, 15, 20, 22, 19, 25]
# create a figure and an axes
fig, ax = plt.subplots(figsize=(8, 6))
# plot the data as a scatter plot
ax.scatter(hours, fish_counts, marker="x")
# set the x-axis label
ax.set_xlabel("Hours since low tide")
# set the y-axis label
ax.set_ylabel("Jellyfish entering bay over 15 minutes")
# show the legend
ax.legend()
# show the plot
plt.show()这段代码将用小时和fish_counts数据创建一个散点图,使用x标记来表示数据点。X轴将标记为“退潮后数小时”,y轴将标记为“海蜇进入海湾超过15分钟”。
在本例中,分散方法将小时数组和fish_counts数组分别作为第一个参数和第二个参数。标记参数设置为"x“,用于数据点的x标记。
还可以通过为分散方法设置附加参数来自定义散点图的外观。例如,您可以使用颜色参数来设置数据点的颜色,或者使用s参数来设置标记的大小。下面是如何使用这些参数的示例:
# create a figure and an axes
fig, ax = plt.subplots(figsize=(8, 6))
# plot the data as a scatter plot with customized colors and marker sizes
ax.scatter(hours, fish_counts, marker="x", color="green", s=100)
# set the x-axis label
ax.set_xlabel("Hours since low tide")
# set the y-axis label
ax.set_ylabel("Jellyfish entering bay over 15 minutes")
# show the legend
ax.legend()
# show the plot
plt.show()发布于 2022-12-03 16:55:20
要在Python中使用图像中显示的数据和格式创建散点图,可以使用以下代码:
hours = [n / 3600 for n in seconds]
fig, ax = plt.subplots(figsize=(8, 6))
ax.scatter(hours, fish_counts, marker="x", color="red")
ax.set_xlabel("Hours since low tide")
ax.set_ylabel("Jellyfish entering bay over 15 minutes")
ax.legend()这段代码与您提供的代码之间的关键区别在于,它使用scatter()方法来创建散点图,而不是使用plot()方法。scatter()方法允许您为数据点指定标记样式和颜色,这对于匹配图像中的散点图格式是必要的。
通过使用此代码,您应该能够创建一个与图像中显示的格式相匹配的散点图。
发布于 2022-12-03 18:38:31
要将行添加到散点图中,可以使用ax.plot()方法。这个方法的第一个参数应该是线上点的x坐标,第二个参数应该是线上点的y坐标。下面是一个示例:
# Set up the plot
fig, ax = plt.subplots(figsize=(8, 6))
# Add the scatter plot
ax.scatter(hours, fish_counts, marker="x")
# Add the lines
ax.plot([0, 24], [200, 200], color="green")
ax.plot([0, 24], [300, 300], color="orange")
ax.plot([0, 24], [400, 400], color="green")
# Add the axes labels
ax.set_xlabel("Hours since low tide")
ax.set_ylabel("Jellyfish entering bay over 15 minutes")
# Show the plot
plt.show()在这段代码中,我们使用ax.plot()向图中添加三行,每一行都有不同的颜色和y坐标值。你可以调整线的x坐标,使它们在图中的位置与所需位置一致。还可以通过向ax.plot()的颜色参数传递不同的颜色值来调整线条的颜色。您可以通过它们的名称(例如“绿色”)或它们的十六进制代码(例如"#00ff00")来指定颜色。
https://stackoverflow.com/questions/74668688
复制相似问题