Home > OS >  Plotting Scatter plot with different lines
Plotting Scatter plot with different lines

Time:12-04

Please I am trying to plot a scatter plot as shown in the attached image.

I have tried the below code but it is not working. This is in python by the way.

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]

Attached image is how the output should look. Thank you. [1]: https://i.stack.imgur.com/5KQiz.png

CodePudding user response:

To plot a scatter plot with the data you provided, you can use the scatter method instead of the plot method. Here is an example of how you could do this:

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

This code will create a scatter plot with the hours and fish_counts data, using the x marker to represent the data points. The x-axis will be labeled "Hours since low tide" and the y-axis will be labeled "Jellyfish entering bay over 15 minutes".

In this example, the scatter method takes the hours and fish_counts arrays as the first and second arguments, respectively. The marker argument is set to "x" to use the x marker for the data points.

You can also customize the appearance of the scatter plot by setting additional arguments to the scatter method. For example, you can use the color argument to set the color of the data points, or the s argument to set the size of the markers. Here is an example of how you could use these arguments:

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

CodePudding user response:

To create a scatter plot in Python with the data and format shown in the image, you can use the following code:

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()

The key difference between this code and the code you provided is that it uses the scatter() method to create the scatter plot, instead of the plot() method. The scatter() method allows you to specify the marker style and color for the data points, which is necessary to match the format of the scatter plot in the image.

By using this code, you should be able to create a scatter plot that matches the format shown in the image.

CodePudding user response:

To add lines to your scatter plot, you can use the ax.plot() method. The first argument to this method should be the x-coordinates of the points on the line, and the second argument should be the y-coordinates of the points on the line. Here is an example:

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

In this code, we use ax.plot() to add three lines to the plot, each with different color and y-coordinate values. You can adjust the x-coordinates of the lines to position them as desired on the plot. You can also adjust the colors of the lines by passing a different color value to the color argument of ax.plot(). You can specify colors by their name (e.g. "green") or by their hex code (e.g. "#00ff00").

  • Related