我有以下表格的数据(link):
Y X 0 X 10 X 20
15 4.83 4.91 4.99
20 4.58 4.65 4.73
25 4.43 4.49 4.56
我试图绘制X 标签值的轮廓,在这些情况下为0,10,20(代码中为z),x轴为X值,y轴为Y值。使用代码创建图:
import numpy as np
import re
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
import matplotlib.cm as cm
with open('contour.dat', "r") as data:
while True:
line = data.readline()
if not line.startswith('#'):
break
data_header = [i for i in line.strip().split('\t') if i]
_data_ = np.genfromtxt('contour.dat', skiprows = 2, delimiter = '\t')
x = _data_[:, 0]
y = _data_[:, 1:]
y_n = []
for i in range(len(data_header)):
if data_header[i][0] == 'X':
y_n = np.int_(np.append(y_n, i))
y_index = [data_header[i] for i in y_n]
z = []
for i in range(0, len(data_header)):
z = np.append(z, re.findall(r"[-+]?\d*\.\d+|\d+", data_header[i]))
z = z.reshape(len(z), 1)
xm = np.tile(x, 21)
xm = np.reshape(xm,(21, 10)).T
zm = np.tile(z, 10).T
with PdfPages('./on_tau.pdf') as p_tau:
_p_vs_tau_ = plt.figure(figsize=(5, 5))
_p_vs_tau_.clf()
p_vs_tau = plt.subplot(111)
# x, y = np.meshgrid(x, y)
surf = plt.contourf(y, xm, zm, 22, rstride=1, cstride=1, cmap=cm.gist_heat,
linewidth=0, antialiased=False, alpha = 1.0)
surf1 = plt.contour(y, xm, zm, 22, colors = '#000000',
linewidths=0.5, antialiased=False, alpha = 1.0)
plt.clabel(surf1, inline=1, fontsize=6)
plt.xlim([5, 10])
p_tau.savefig(bbox_inches='tight')
plt.close()
我正在尝试解决以下问题:
1。目前所有的轮廓标签都是垂直的。我怎么能旋转 跟随轮廓的标签?
2。如何更改标签的位置以使它们不重叠(如图所示)?
其他问题
第3。如何确保标签始终显示在图上,即使轴也是如此 限制被改变了吗?
答案 0 :(得分:1)
我在matplotlib 1.4.3
中运行了下面的代码,并根据需要获得了旋转的轮廓标签,所以,我不知道你为什么会遇到这个问题。对于你的第二个问题,如何使用更少的轮廓水平?
import numpy as np
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
x, y = np.pi*np.mgrid[-1:1:101j, -1:1:101j]
z = np.sin(x)*np.sin(y)
with PdfPages('./contour_plot.pdf') as p_tau:
_p_vs_tau_ = plt.figure(figsize=(5, 5))
_p_vs_tau_.clf()
p_vs_tau = plt.subplot(111)
surf = plt.contourf(y, x, z, 21, rstride=1, cstride=1, cmap="RdYlBu",
linewidth=0, antialiased=False, alpha = 1.0)
surf1 = plt.contour(y, x, z, 21, colors='k', linestyles="solid",
linewidths=0.5, antialiased=False, alpha = 1.0)
plt.clabel(surf1, inline=1, fontsize=6)
plt.savefig("contour_plot.png", dpi=600)
p_tau.savefig(bbox_inches='tight')
plt.close()