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?
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.
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.
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.
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')
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()
Note that:
(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)
.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With