0

I am drawing a scatter plot using the following code

fig, ax = plt.subplots(figsize=(20, 15))

ax.scatter(found_devices_df['time'], found_devices_df['label'], c=found_devices_df['label'],
           marker='.', s=found_devices_df['count']*100, alpha=.6)
fig.autofmt_xdate()

I want to draw horizontal lines and vertical lines (not equally spaced grids) per each level of x and y axes respectively. I have timestamps in the X-axis and the device type on the Y-axis.

How can I use ax.hline and ax.vline for this?

I tried ax.axvline(found_devices_df['time'], color="red", linestyle="--")to draw vertical line per each x datatime stamp but got an error

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Malintha
  • 4,512
  • 9
  • 48
  • 82

1 Answers1

1

If you want to draw multiple vertical lines, try the following code.

ax.vlines(x, ymin=0.0, ymax=1.0, color=color, linestyle="--")

To draw multiple horizontal lines, try the following code

ax.hlines(y, xmin=0.0, xmax=1.0, ...)

full code:(We have customized the scatterplots in the official reference.)

import numpy as np
np.random.seed(19680801)
import matplotlib.pyplot as plt


fig, ax = plt.subplots()
for color in ['tab:blue', 'tab:orange', 'tab:green']:
    n = 25
    x, y = np.random.rand(2, n)
    scale = 200.0 * np.random.rand(n)
    ax.scatter(x, y, c=color, s=scale, label=color, alpha=0.8, edgecolors='none')
    ax.vlines(x, ymin=0.0, ymax=1.0, color=color, linestyle="--")
    
# ax.legend()
# ax.grid(True)

plt.show()

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32