I have an array A which I have plotted in a contour plot using X and Y as coordinate axes,
plt.contourf(X,Y,A)
Problem is, the values in A vary from 1 to a very large number such that the color scale doesn't show a plot. When I plot log(A), I get the following contour,
which is what I'm looking for. But I want to be able to view the values of the array A, instead of log(A), when I hover my cursor over a certain (X,Y) point. I already got an answer for how to do that, but how would I go about doing it while my colour scale remains log? Basically what I'm trying to do is to make the color scale follow a log pattern, but not the array values themselves.
Thanks a lot!
You can do this:
from matplotlib import colors
plt.contourf(X, Y, A, norm=colors.LogNorm())
plt.colorbar()
plt.show()
or
from matplotlib import ticker
plt.contourf(X, Y, A, locator=ticker.LogLocator())
plt.colorbar()
plt.show()
A similar question was already asked for log-scaling the colors in a scatter plot: A logarithmic colorbar in matplotlib scatter plot
As is it was indicated there, there is an article in matplotlibs documentation that describes norms of colormaps: http://matplotlib.org/devdocs/users/colormapnorms.html
Essentially, you can set the norm of your contourplot by adding the keyword , norm=matplotlib.colors.LogNorm()
Related
I plotted a 2 sigma C.L ellipse using contour plot function in matplotlib. However I am unable to fill it with colours. Only the boundaries are coloured.
plt.contour(x, y, likelihood, [level1,level2])
Where x=[100,], y=[100,] and l=[100,100] dimensional arrays. The plot I get is as shown below : I would like the ellipses to be of solid colour. How to do it ? using cmap doesnt help either as posted in some other threads in this forum.
Thanks to #kwinkunks here is a solution
plt.contourf(x, y, likelihood, [level1,level2],colors=['white', 'grey', 'red'],extend='both')
I am using matplotlib.pyplot and astropy to build a plot in galactic coordinates and my goal is to show the density of stars in the sky.
For that, the only data I have is a two-column table with the coordinates of the stars in Right Ascension (RA) and Declination (Dec).
Right now my code is doing the following:
import astropy.coordinates as coord
import matplotlib.pyplot as plt
import astropy.units as u
coordinates = coord.SkyCoord(ra=RA*u.deg, dec=DEC*u.deg)
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection="aitoff")
ax.plot(coordinates.galactic.l.wrap_at('180d').radian,
coordinates.galactic.b.radian, 'k.', alpha=0.01, ms=1)
ax.grid(True)
So for now I am basically using plt.plot to plot all datapoints (which in the case is half-million datapoints) using a very low alpha and symbol size and the plot looks like this:
However, this isn't the plot I want, as the colour scale quickly saturates.
My question is: Is there a way of making a similar plot but properly reflecting the density of datapoint in the z-axis (color)? For example, I want to be able of controling the color table for a given number-density of sources.
I've seen some answers to similar questions are available.
For example, this question (Plotting a heatmap in galactic coordinates) does a similar thing, but for a specific z-axis described by some data.
I am also aware of this question (How can I make a scatter plot colored by density in matplotlib?) and I tried each solution in this post, but they all failed since I am using a subplot which already has a projection.
Any ideas?
I have a large list of data points of x and y values that I need to put into a histogram with 40 bins but mathlibplot.hist is only letting me enter 1 variable with bins. I've tried hist2d as well but it's not very clean. Any help would be appreciated!
As you have data points x and y, you can simply use hist method to plot histogram.
The following code will help you to create a histogram.
plt.hist([x,y],bins=40, histtype='step',fill=True)
plt.show()
The histogram will look like the following:
If you want to change the style or give it title and labels, you can do it. Here is another histogram with unfilled bars.
If you still face any problem, let me know then.
Maybe you can make use of matplotlib library to solve your purpose:
It will be like imposing 2 histograms on top of each other.
In the below code, I am trying to plot a histograms of y_train and predicted(X_train) in the same space.
You can modify the variables as per your requirement.
import matplotlib.pyplot as plt
plt.hist(y_train, stacked=True,bins=40, label='Actual', alpha=0.5)
plt.hist(regressor.predict(X_train),bins=40, stacked=True, label='Predicted', alpha=0.5)
plt.legend(loc='best')
plt.show()
Hope this helps!
I have a calculated data (x,y) which I would like to put on top of an image from a paper to compare with some of its boundaries. If this image was a 2D x,y plot, I would have extracted easily the points and then plot together with mine. However this image is a 3D color plot, therefore if I want just to read the image, define my coordinates according to it and plot my data on top, so at the end would seem like 2 layers.
I can plot scatter points by following:
import matplotlib.pyplot as plt
im = plt.imread('fig_exp.png')
implot = plt.imshow(im)
# put a blue dot at (10, 20)
plt.scatter([10], [20], c='r', s=2000, marker=u'*')
plt.show()
But how can I define image coordinates so I can plot e.g
x=[-100,-80,-60,-40]
y=[10,15,20,25]
plt.plot(x,y,'-ro')
The problem here is that I dont know how to define the coordinates as in the figure. Beside I dont know how to plot not as scatter but a line.
I appreciate any helpful solution!
You are looking for the extent= keyword argument to imshow() as detailed in the documentation
As for your second question, if you want a line plot, you use the plot() function instead of scatter().
I have considered the matrix of numbers as the mapping of plane points to their values and then draw the isoline. I have practised the contour function from the following module: matplitlib.pylab and I have succeeded in configurating it as I wanted, but there is one exeption. - The contourf set a step by default and it designates the values on the Y-axis and X-axis. I would like to change it. Is it possible? I have read all of the documentation about the contour function but I haven't found the solution. I am a beginner. This is my code:
plt.figure()
cs = plt.contour(m.Z, levels=A, colors=K, linestyles='solid')
plt.clabel(cs, A[1::2], inline=0.5, fmt='%1.0f', fontsize=10)
plt.colorbar(cs, shrink=1)
plt.show()
m.Z is my matrix and plt is matplotlib.pylab I have tried to change the step giving the values of vectors - X and Y in contour ( I know that they aren't needed) but it doesn't work.