I'm using Python Matplotlib to plot contours. Here's some code I have below as a basis. If you run this, you'll see that the labels are almost at vertical. I'd like to get the labels orientated horizontal, but I have no idea how can achieve this. I've tried with ClabelText
, which the documentation suggests, but don't understand how this is supposed to work. I'd appreciate if someone could suggest a way to orientate the labels, either with or without ClabelText.
import itertools as it
import numpy as np
from matplotlib.ticker import FuncFormatter
from matplotlib.contour import ClabelText
import matplotlib.pyplot as plt
from math import pi, log
def getTime(data):
M = data['weight']
Tei = data['temp']
Twasser = 99.8
Teikl = 86.0 ## max allowed temp
k = 0.262 ## estimate was 0.3 W/(m.K),
Crho = 3.18 # (KJ/kgC)
const = pow(Crho, 1.0/3) / (pi*pi*k*pow(4*pi/3,2.0/3))
Tval = const*pow(M,2.0/3)*log(0.76*(Tei-Twasser)/(Teikl-Twasser))
return Tval # coo time in minutes
def contourFmt(val, posn):
mins = int(val // 1)
secs = int(val % 1 *60)
return '{0:d}mm{1:d}ss'.format(mins, secs)
def labeler(val): #is this any use??
print(val)
return
#weights = np.array(range(40, 80, 5))*1.0
#temps = np.array(range(0, 30, 5))*1.0
weights = np.arange(40.0, 80.0, 5.0)
temps = np.arange(0.0, 25.01, 5.0)
X = temps
Y = weights
Z = np.zeros((len(X), len(Y)))
xx = [{'temp':i} for i in X]
yy = [{'weight':i} for i in Y]
plt.figure()
##zz = it.product(xx,yy)
for i, xdicts in enumerate(xx):
for j, ydicts in enumerate(yy):
zd = {}
zd.update(xdicts)
zd.update(ydicts)
zval = getTime(zd)
Z[i,j] = zval
times = np.arange(4.00, 6.50, 0.25)
CS = plt.contour(Y, X, Z, levels=times, colors='b')
lbl = ClabelText(labeler)
lbl.set_rotation('horizontal')
formatter = FuncFormatter(contourFmt)
#plt.clabel(CS, inline=True, fmt=formatter, fontsize=12)
plt.clabel(CS, inline=True, use_clabeltext=True, fmt=formatter, fontsize=12)
plt.grid(True)
plt.clabel(CS, inline=1, fontsize=12)
plt.show()
In Toolspace, on the Settings tab, under Surface, click Label Styles > Contour (or Slope, Spot Elevation, Watershed alternatively). Select the style you want to modify and right-click.
If True , contour labels will be placed manually using mouse clicks. Click the first button near a contour to add a label, click the second button (or potentially both mouse buttons at once) to finish adding labels.
To add multiple contour labelsClick Annotate tab > Labels & Tables panel > Add Labels menu > Surface > Contour - Multiple. Select the contours to label by specifying the start and end points of the contour label line. Press Enter.
You can set the rotation of the individual labels after they have been created. The label Text
objects are returns by clabel
, so you can store them and iterate over them, using .set_rotation(0)
to orient them horizontally.
Change the last few lines of your script to:
labels1 = plt.clabel(CS, inline=True, use_clabeltext=True, fmt=formatter, fontsize=12)
labels2 = plt.clabel(CS, inline=1, fontsize=12)
for l in labels1+labels2:
l.set_rotation(0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With