I have 2 sets of coordinates that I want to plot on the same matrix.
This is my code:
self.ax.imshow(arr,cmap=plt.cm.Greys_r, interpolation='none')
self.ax.imshow(arr.T, cmap=plt.cm.Greys_r, interpolation = 'none')
However this does not work. It only seems to plot the one that is called last.
What am I doing wrong?
I'm not sure I entirely understand the question, but if you just want the sum of both matrices plotted, then try,
self.ax.imshow(arr+are.T,cmap=plt.cm.Greys_r, interpolation='none')
though note it only works if arr and are.T have equal shape.
Related
I am wondering if it is maybe possible to plot the output of numpy.histogram2d() using matplotlib.pyplot.hist2d? In the one-dimensional case, this can be done by using:
counts, bins = np.histogram(something, bins=no_bins, range=(range_min, range_max))
plt.hist(bins[:-1], bins, weights=counts)
Is there a similar solution for the two-dimensional case? I do not want to plot the 2d histo with the methods suggested on https://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram2d.html
The idea behind this is that I would like to apply some corrections to the inital histogram (i.e. bin-by-bin background subtraction using data from another 2d histogram) and then plot the corrected histogram.
Many thanks in advance!
Plotting 2d histograms is typically done with imshow(). If you're used to ROOT or some other plotting libraries, be especially careful what argument you give for origin and extent.
The following solution works as expected:
counts_bkg, bins_x_bkg, bins_y_bkg = np.histogram2d(x_bkg, y_bkg, bins=(x_bins, ybins))
counts, bins_x, bins_y = np.histogram2d(x, y, bins=(x_bins, y_bins))
diff = counts - counts_bkg
diffT = diff.T
fig, ax = plt.subplots(1)
pc = ax.pcolorfast(bins_x, bins_y, diffT)
plt.show()
In the docs, you can find three examples of how to plot the output from np.histogram2d() using the matplotlib functions imshow, pcolormesh and NonUniformImage.
I googled my way here. I am going to write here what worked for me, just in case, some else finds themselves in similar situtation.
I was looking at the source code of pyplot, where I found reference to axes class. The hist2d funtion is actually defined in 'matplotlib/axes/_axes.py'.
There I found hist2d calls np.histogram2s and then uses the xedges, yedges and bins in plt.pcolormesh as follow:
pc = self.pcolormesh(xedges, yedges, h.T, **kwargs)
Remember there is no option to use keywords 'Range', 'density' and 'bins' in pcolormesh but these are taken into account by the np.histogram2d function.
TL;DR: Using pcolormesh is the simplest way for plotting 2D histogram from the output of np.histogram2d.
hoping this question is an easy one to solve!
I am trying to create an intensity map of a set of values. Each value has a corresponding (x,y) coordinate. Currently, my data is in the form of 3 separate arrays all of the same length. One array has x-coordinates, another has y-coordinates, and the third has numerical values that are the intensities i wish to colorize. Really I'm thinking this is just a scatter plot but integrating the third array's value into a color. If anyone knows which plotting method I should use that would be great. I've tried pcolor from pyplot, but it is giving me errors about the dimensions of my x and y data. Maybe this is an issue with the x and y arrays being 1-d?
Thanks in advance for any help!
Figured it out!
Just need to set the value array to a color variable. Like this:
plt.scatter(x, y, c=z, cmap='jet',vmin=0, vmax=250)
plt.colorbar()
plt.show()
"z" is the values array!
I have the code below:
fig, ax = pyplot.subplots()
graph = ax.pcolorfast(data, cmap='viridis', vmin = min, vmax = max)
pyplot.colorbar(graph)
pyplot.show()
and it plots what I wanted, however it is sideways. Is there a good way of rotating it -90 or 270 degrees? I have tried a.T, which returns the original plot. I have also tried ndimage.rotate(graph, -90|270), ndimage.interpolation.rotate(graph, -90|270), and numpy.rot90(data,3). The first two return errors for invalid rotation planes and the second appears to shove the graph off the edge, losing a majority of my data points.
If anyone has some thoughts, I would be very grateful. Even if it's that I put in the wrong arguments. I am at a loss here.
Is a supposed to be equal to data in your example? Tried your code with a random 2D array, and np.transpose(a) as well as a.T seem to properly rotate the figure, as opposed to what you indicate here ('returns the original plot'). If this is not the case for you, I think we need more information.
For some reason this question is tough for me to explain, but here goes. I have a set of data say (x,y1,y2) that I would like to plot. I need it to be one plot, not two plot one being (x,y1) and the other being (x,y2). Instead I want it so i can plot it at once and then have the left axis be for (y1) and the right axis be for (y2).
Currently I am stuck at plotting two separate plots and having to scale the axis to match each other, yet it still isn't working too well. Below I put my code that plots, the set of data is mesaBV=x...mesaMv=y1...mesaAge=y2
Is anyone familiar with answering this question? Thanks!!
Edit:
host = host_subplot(111, axes_class=AA.Axes)
par1 = host.twinx()
par1.set_yscale('log')
host.grid(True)
host.set_xlabel("B-V")
host.set_ylabel("Mv")
par1.set_ylabel("Age")
p1, = host.plot(mesaBV, mesaMv, label="Mv",marker="o")
p2, = par1.plot(mesaBV, mesaAge, label="Age",marker="o")
host.set_yticks(np.linspace(host.get_ybound()[0], host.get_ybound()[1], 10))
par1.set_yticks(np.linspace(par1.get_ybound()[0], par1.get_ybound()[1], 10))
host.axis["left"].label.set_color(p1.get_color())
par1.axis["right"].label.set_color(p2.get_color())
plt.draw()
plt.show()
By having two y values for the same point, you are creating a vertical line. Unless you can scale the graph perfectly, it won't work, and even if you scale it perfectly, there would be no point (no pun intended) because y1 would be essentially in exactly the same spot as y2. If you truly need it like that, than just plot (x1,y1) and figure out a different scale for the right y-axis without having it affect the original points. This way you can easily emulate (x1,y1,y2).
EDIT: Now that I can see your code, I recommend using the twinx() function to create the second axis.
I am having difficulty understanding how matplotlib.pyplot.xlim() works.
I am plotting a simple plot of x values vs y values. The y values are numerical points in the range of 100-600. The x values are of magnitude e-09 to e-13. So, I plot x against y. This is my plot, with generic pseudocode
import matplotlib.pyplot as plt
x = np.array
y = np.array
plt.plot(x,y)
plt.ylim(0,400)
plt.show()
As you can tell, there's plenty of structure between 0 and 0.5. I would like to look at that.
So, I try
plt.plot(x,y)
plt.xlim(0,0.5)
plt.ylim(0,400)
plt.show()
The output plot is completely blank. I see nothing.
So, I try, xlim= -1 to +1
plt.plot(x,y)
plt.xlim(-1,1)
plt.ylim(0,400)
plt.show()
This is the output plot.
Using the origninal plot, how can I set the x-axis to see the actual data?
As you clearly mentioned
The x values are of magnitude e-09 to e-13.
So if you want to see the values that lie within 1e-8 and 0.5e-9 you should do:
plt.xlim(1e-8,0.5e-9)
instead of
plt.xlim(0,0.5)
where you have no values to show as the values of x are within e-09 to e-13.
If your x-values are of magnitude of 1e-9 till 1e-13 you have completely different length scales. In this case a logarithmic axis may be appropriate. Note that this works only if all x-values are strictly positive.
plt.xscale('log')