Question

I am having trouble making a scatter plot with a log scale for the values. The arrays I have are numpy ndarray's. x.size = 19, y.size = 18, and data is 19x18. It is telling me "ValueError: x and y must be the same size". But they shouldn't have to be?! I tried botching it and just making each one 18, but then it wasn't converting my data values to the 0-1 range and it was giving me an rbg color error. Any help would be greatly appreciated!

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.cm import get_cmap
from matplotlib.colors import (LogNorm, Normalize,SymLogNorm)
from matplotlib.ticker import (LogLocator, LogFormatter, LogFormatterMathtext, 
                           MultipleLocator)
x= np.arange(19)
y = np.arange(18)
data = np.random.random_sample((19, 18))

sc =ax2.scatter(x,y,c=data,norm=LogNorm(),
                cmap=get_cmap('Spectral_r'),edgecolors='none')
cbar1 = fig.colorbar(sc,ax=ax2,ticks=LogLocator(),format=LogFormatterMathtext())
Was it helpful?

Solution

x, y and c must all be the same size as they are mapped by position (zip(x, y, c) should return the position and color tuples you want).

Try something like

X, Y = np.meshgrid(x, y)   # makes 2D X, Y arrays for every grid point

sc = plt.scatter(X.ravel(), Y.ravel(), c=c.ravel(), ...)

OTHER TIPS

http://en.wikipedia.org/wiki/Scatter_plot

The data is displayed as a collection of points, each having the value of one variable determining the position on the horizontal axis and the value of the other variable determining the position on the vertical axis.

You are not plotting two separate arrays when you make a scatter plot. You are plotting N two-dimensional points. In the the function you are using, you supply the x and y coordinates separately. This is why they must be the same length.

Maybe a scatter plot is not what you are after in this case?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top