ItGo.me Focus on IT Recommend

Home > python - Matplotlib Contour Clabel Location

python - Matplotlib Contour Clabel Location

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1074

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

I would like to control the location of matplotlib clabels on a contour plot, but without utilizing the manual=True flag in clabel. For example, I would like to specify an x-coordinate, and have labels created at the points that pass through this line. I see that you can get the location of the individual labels using get_position(), but I am stuck at that. Any help would be greatly appreciated. Thanks!

python matplotlib contour
|
  this question
edited Dec 2 '15 at 21:06 asked May 7 '10 at 20:21 Matthew Ryan Dillon 95 2 8

 | 

2 Answers
2

---Accepted---Accepted---Accepted---

No, there is no way built into matplotlib to do that. You are supposed to either live with the default locations or go fully interactive with manual and using the mouse.

You might want to file this as a bug report upstream so they can
  their algorithms.

There are multiple options to work around this. The first one is to programmatically place text on the contour figure. You will not be able to reliably remove the lines underneath the text this way. Assuming you have a contour c you can find the contour lines in c.collections. For every contour line invoke get_paths and place your text on that path.

The other option would be to replace the code for manual placement (in matplotlib.contour.BlockingContourLabeler) or tweak the code that finds the label positions (in matplotlib.contour.locate_label), but both functions are pretty dense. If you can come up with a working replacement for locate_label just overwrite the old method in your plotting macro

def your_locate_label(self, linecontour, labelwidth):
    # some magic
    pass

ar = np.array([[1,0], [0,1]]
c = matplotlib.contour(ar)
c.locate_label = your_locate_label

c.clabel()

Btw, if you use ipython you can easily view the function source from your interactive session with

Recommend:python - Matplotlib contour plot with intersecting contour lines

data is of this form - # x y height 77.23 22.34 56 77.53 22.87 63 77.37 22.54 72 77.29 22.44 88 The data actually consists of nearly 10,000 points, which I am reading from an input file. However the s

%psource c.clabel

or directly invoke your $EDITOR on the file were it is defined with

%edit c.clabel

|
  this answer
answered May 10 '10 at 3:16 Benjamin Bannier 23.6k 7 37 72      I didn't find this issue posted, so I made a new one: github.com/matplotlib/matplotlib/issues/613 –  keflavich Dec 7 '11 at 18:42

 | 

Yes, there now is a way to control label locations! https://github.com/matplotlib/matplotlib/pull/642

plt.figure()
CS = plt.contour(X, Y, Z)
manual_locations = [(-1, -1.4), (-0.62, -0.7), (-2, 0.5), (1.7, 1.2), (2.0, 1.4), (2.4, 1.7)]
plt.clabel(CS, inline=1, fontsize=10, manual=manual_locations)

|
  this answer
answered Oct 22 '12 at 15:59 keflavich 5,664 4 44 74      manual can be an iterable object of x,y tuples. Contour labels will be created as if mouse is clicked at each x,y positions. –  Faber Dec 2 '14 at 21:40 1   Is there a way to allow automated labelling, and then loop through the labels to remove (and maybe
 ) any that intersect the axes? –  Richard Law Feb 24 '16 at 21:13

 | 

Recommend:matplotlib - Contour plot with python

grid. exactly half instead of a full plot. I mean my grid is 20 units in x direction however, my resulting plot is 10 units in x direction with replica plot from 10 to 20 units. Can you please look whats wrong v is a numpy array of 20x10

------splitte line----------------------------