Vinod Kumar
Vinod Kumar

Reputation: 1622

Way to change only the width of marker in scatterplot but not height?

I want to make a scatterplot with marker type as rectange (not square), such that width is more than height. With the "s" I can control the overall size of the marker but it increases in both dimension.

I can not directly pass height and width as these are unknown properties of scatter.

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.scatter(np.arange(1,6), np.random.normal(size=5), marker='s', s=16)

Upvotes: 0

Views: 584

Answers (1)

Achintha Ihalage
Achintha Ihalage

Reputation: 2440

Try the following snippet.

import numpy as np
import matplotlib.pyplot as plt
width = 60
height = 30
verts = list(zip([-width,width,width,-width],[-height,-height,height,height]))
fig, ax = plt.subplots()
ax.scatter(np.arange(1,6), np.random.normal(size=5), marker=(verts,0),s=40)

Here, the argument s changes the size of the scatter. The drawn rectangle keeps the ratio width/height.

Output:

enter image description here

update

Since matplotlib 3.2x, use of (verts, 0) is depreciated. The working code should be changed to

fig, ax = plt.subplots()
ax.scatter(np.arange(1,6), np.random.normal(size=5), marker=verts, s=40)

Upvotes: 1

Related Questions