I'm making a simple contour plot and I want to highlight the zero line by making it thicker and changing the color.
cs = ax1.contour(x,y,obscc)
ax1.clabel(cs,inline=1,fontsize=8,fmt='%3.1f')
How do I achieve this?
Thanks :-)
HTH -- this is basically the contour example taken from the matplotlib docs, just with modified level lines
The object that is returned from the contour-method holds a reference to the contour lines in its collections attribute.
The contour lines are just common LineCollections.
In the following code snippet the reference to the contour plot is in CS (that is cs in your question):
CS.collections[0].set_linewidth(4) # the dark blue line
CS.collections[2].set_linewidth(5) # the cyan line, zero level
CS.collections[2].set_linestyle('dashed')
CS.collections[3].set_linewidth(7) # the red line
CS.collections[3].set_color('red')
CS.collections[3].set_linestyle('dotted')
type(CS.collections[0])
# matplotlib.collections.LineCollection
Here's how to find out about the levels, if you didn't explicitly specify them:
CS.levels
array([-1. , -0.5, 0. , 0.5, 1. , 1.5])
There is also a lot of functionality to format individual labels:
CS.labelCValueList CS.labelIndiceList CS.labelTextsList
CS.labelCValues CS.labelLevelList CS.labelXYs
CS.labelFmt CS.labelManual CS.labels
CS.labelFontProps CS.labelMappable CS.layers
CS.labelFontSizeList CS.labelTexts
Related
I am trying to draw a diagonal line on my figure to demonstrate how my data compares to someone else's, so I want a line representing 1:1 relationship. I'm trying to use plt.plot to do the line between two points but there is no line on my plot. This is my code + the figure. Can anyone tell me why it is not working?
plot23 = sns.regplot(x = Combined['log10(L/L_solar)'], y = Combined['logLum'],
fit_reg=False).set_title('Figure 23: Comparing luminosities')
plt.xlabel('logL from my data', fontsize=13)
plt.ylabel('logL from Drout', fontsize=13)
plt.axis((4, 6, 4, 6))
plt.plot([0,0], [6,6], 'k-')
plt.show()
plot23.figure.savefig("figure23.png")
You made a mistake in using plt.plot. The syntax is
plt.plot(xarray,yarray, ...)
.
This should be :
plt.plot([0,6], [0,6], 'k-')
To draw infinite lines under a specified angle, e.g. 45deg through the origin at (0,0) you can use
plt.axline( (0,0),slope=-1,linestyle='--',color='red')
This line will span the full axes box regardless the data extremes and you don't have to first determine the axis limits or data limits.
This is a generalization of plt.axhline() and plt.axvline(), which are used to draw infinite length horizontal and vertical lines.
Ref: https://matplotlib.org/stable/gallery/pyplots/axline.html
I am working on a project and we decided to use matplotlib.
For a polar chart we have a bunch of colored vectors which need to be recognizable from each other.
Now I have been able to get this:
Quiver add is simply:
Q.append(sub.quiver(0,y_min_max[0], real_coords[i], imag_coords[i], color=color, scale=y_min_max[1]*2, zorder=5)
But is it possible to have dashed colored vectors?
The closest answer I found is this one Plotting dashed 2D vectors with matplotlib?
Which is close but I've only managed to get colored vectors surrounded with dashed lines instead of dashed vectors
Q.append(sub.quiver(0,y_min_max[0], real_coords[i], imag_coords[i], color=color, scale=y_min_max[1]*2, zorder=5, linewidth=0.5, linestyle = '--'))
I've been experimenting with various combinations but no luck for now, any ideas?
Thanks in advance
From all example code/demos I have seen in the VisPy library, I only see one way that people plot many lines, for example:
for i in range(N):
pos = pos.copy()
pos[:, 1] = np.random.normal(scale=5, loc=(i+1)*30, size=N)
line = scene.visuals.Line(pos=pos, color=color, parent=canvas.scene)
lines.append(line)
canvas.show()
My issue is that I have many lines to plot (each several hundred thousand points). Matplotlib proved too slow because of the total number of points plotted was in the millions, hence I switched to VisPy. But VisPy is even slower when you plot thousands of lines each with thousands of points (the speed-up comes when you have millions of points).
The root cause is in the way lines are drawn. When you create a plot widget and then plot a line, each line is rendered to the canvas. In matplotlib you can explicitly state to not show the canvas until all lines are drawn in memory, but there doesn't appear to be the same functionality in VisPy, making it useless.
Is there any way around this? I need to plot multiple lines so that I can change properties interactively, so flattening all the data points into one plot call won't work.
(I am using a PyQt4 to embed the plot in a GUI. I have also considered pyqtgraph.)
You should pass an array to the "connect" parameter of the Line() function.
xy = np.random.rand(5,2) # 2D positions
# Create an array of point connections :
toconnect = np.array([[0,1], [0,2], [1,4], [2,3], [2,4]])
# Point 0 in your xy will be connected with 1 and 2, point
# 1 with 4 and point 2 with 3 and 4.
line = scene.visuals.Line(pos=xy, connect=toconnect)
You only add one object to your canvas but the control pear line is more limited.
How can I create a scatter plot legend without two symbols showing up each time? I can understand why you'd want this when you're joining symbols by lines, but for a pure scatter plot, all I want in the legend is one example of the symbol. This plot from a previous stackoverflow post shows the kind of thing I mean:
In the legend command you can use the scatterpoints option:
ax.legend(loc=0, scatterpoints = 1)
For a normal plot, it is the option numpoints.
Here you can find more information about the keyword arguments for the legend: http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.legend
I have a couple of lines and I want to show a legend. The problem is, I can't use different styles (--, :, -.) because there are too few of them, and I can't use markers (+, *, etc.) because I need them to show some points on the lines.
So the best idea I've come up with is to use numbers. But I can't figure how I can create legends with numbers. I can even draw numbers near lines myself (to place them in the best position), but how can I then draw a legend with the numbers?
I.e. instead of:
-- H
-.- Li
I'd like something like:
1 H
2 Li
Perhaps a little Latex thrown into the mix?
#In which we make a legend; not with lines, but numbers!
import pylab as pl
pl.rc('text', usetex=True)
pl.figure(1)
pl.clf()
ax = pl.subplot(111)
pl.plot(range(0,10), 'k', label = r'\makebox[25]{1\hfill}Bla')
pl.plot(range(1,11), 'k', label = r'\makebox[25]{12\hfill}Bla12')
lgd = pl.legend(handlelength = -0.4)
for k in lgd.get_lines():
k.set_linewidth(0)
pl.draw()
pl.show()
The numbers/labels are aligned by using \makebox with specific width and \hfill to take up the space not used by your labels. Numbers are not automatic, but if you use a loop to draw your lines then you could add a counter to keep track of the numbers.
Don't know if this is part of your requirement, but the lines are removed by setting their linewidth to 0 and making the space reserved in the legend negative. Couldn't find a neater way of doing this as I believe a legend is always meant to show a line (e.g. you can't set numpoints to 0).
You could of course also just add some text in the right spot in your plot and not use a legend at all.