Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python scatter plot. Size and style of the marker

I have a set of data that I want to show as a scatter plot. I want each point to be plotted as a square of size dx.

          x = [0.5,0.1,0.3]           y = [0.2,0.7,0.8]           z = [10.,15.,12.]           dx = [0.05,0.2,0.1]            scatter(x,y,c=z,s=dx,marker='s') 

The problem is that the size s that the scatter function read is in points^2. What I'd like is having each point represented by a square of area dx^2, where this area is in 'real' units, the plot units. I hope you can get this point.

I also have another question. The scatter function plots the markers with a black border, how can I drop this option and have no border at all?

like image 379
Brian Avatar asked Jan 31 '12 14:01

Brian


People also ask

How do you change a marker shape in a scatter plot in python?

Just use the marker argument of the plot() function to custom the shape of the data points. The code below produces a scatter plot with star shaped markers (figure on the left). The figure on the right shows you the possible shapes offered by python.

How do you increase marker size in scatter plot?

Size in points^2 markersize'] ** 2. This can be taken literally. In order to obtain a marker which is x points large, you need to square that number and give it to the s argument. So the relationship between the markersize of a line plot and the scatter size argument is the square.

What is marker size in scatter?

Where, s is a scalar or an array of the same length as x and y , to set the scatter marker size. The default scatter marker size is rcParams['lines. markersize'] ** 2 . According to documentation, s is the marker size in points2.


2 Answers

Translate from user data coordinate system to display coordinate system.

and use edgecolors='none' to plot faces with no outlines.

import numpy as np  fig = figure() ax = fig.add_subplot(111) dx_in_points = np.diff(ax.transData.transform(zip([0]*len(dx), dx)))  scatter(x,y,c=z,s=dx_in_points**2,marker='s', edgecolors='none') 
like image 169
remosu Avatar answered Oct 02 '22 13:10

remosu


If you want markers that resize with the figure size, you can use patches:

from matplotlib import pyplot as plt from matplotlib.patches import Rectangle  x = [0.5, 0.1, 0.3] y = [0.2 ,0.7, 0.8] z = [10, 15, 12] dx = [0.05, 0.2, 0.1]  cmap = plt.cm.hot fig = plt.figure() ax = fig.add_subplot(111, aspect='equal')  for x, y, c, h in zip(x, y, z, dx):     ax.add_artist(Rectangle(xy=(x, y),                   color=cmap(c**2),        # I did c**2 to get nice colors from your numbers                   width=h, height=h))      # Gives a square of area h*h  plt.show() 

enter image description here

Note that:

  1. The squares are not centered at (x,y). x,y are actually the coords of the square lower left. I let it this way to simplify my code. You should use (x + dx/2, y + dx/2).
  2. The color is get from the hot colormap. I used z**2 to give colors. you should also adapt this to your needs

Finally for your second question. You can get the border of the scatter marks out using the keyword arguments edgecolor or edgecolors. These are a matplotlib color argument or a sequence of rgba tuples, respectively. If you set the parameter to 'None', borders are not draw.

like image 21
joaquin Avatar answered Oct 02 '22 13:10

joaquin