我正在尝试使用补丁生成图表。从下面的代码中,有谁知道为什么这些最终会被压扁而不是循环,就像我想要的那样?不幸的是我没有足够的代表发布图片......示例代码是自包含的,即您应该能够运行它来生成图形。当调整绘图大小时,补丁似乎会改变形状。我希望他们保持固定。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.transforms as transforms
from matplotlib.font_manager import FontProperties
from matplotlib.pyplot import *
mypath = ['1,0.25','2,0.5','3,0.35','4,0.40']
fig = plt.figure()
ax = fig.add_subplot(111)
Distances = []
Confidence_Intervals = []
for line in mypath:
Distances.append(float(line.split(',')[0].strip()))
Confidence_Intervals.append(float(line.split(',')[1].strip()))
ind = np.arange(len(Distances))
data = np.array(Distances)
y_error = np.array(Confidence_Intervals)
circles = []
for a in range(len(ind)):
ax.scatter(0, data[a], s=60, color='Black')
trans = transforms.blended_transform_factory(ax.transData, ax.transData)
circles.append(patches.Circle((0,data[a]),y_error[a], transform=trans,facecolor='yellow', alpha=0.5))
fig.set_size_inches(24,12)
for circle in circles:
ax.add_patch(circle)
plt.grid(True)
plt.legend(loc=0, scatterpoints = 1)
plt.ylabel('Pairwise distance (FastTree)')
plt.xlabel('Clade pairing')
plt.tick_params(axis='both', which='minor', labelsize=8)
plt.title(title)
plt.xlim(-0.6,0.6)
plt.show()
答案 0 :(得分:1)
感谢Joe Kington的建议。你是对的。宽高比没有设定。
我添加了以下行:
axes()。set_aspect('equal','datalim')
现在,补丁的宽度与图表大小无关。最终代码如下。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.transforms as transforms
from matplotlib.font_manager import FontProperties
from matplotlib.pyplot import *
mypath = ['1,0.25','2,0.5','3,0.35','4,0.40']
fig = plt.figure()
ax = fig.add_subplot(111)
Distances = []
Confidence_Intervals = []
for line in mypath:
Distances.append(float(line.split(',')[0].strip()))
Confidence_Intervals.append(float(line.split(',')[1].strip()))
ind = np.arange(len(Distances))
data = np.array(Distances)
y_error = np.array(Confidence_Intervals)
circles = []
print y_error
plt.xlim(-0.6,0.6)
for a in range(len(ind)):
ax.scatter(0, data[a], s=60, color='Black')
trans = transforms.blended_transform_factory(ax.transData, ax.transData)
circles.append(patches.Circle((0,data[a]),y_error[a], transform=trans, facecolor='yellow', alpha=0.5))
fig.set_size_inches(24,12)
for circle in circles:
ax.add_patch(circle)
plt.grid(True)
plt.legend(loc=0, scatterpoints = 1)
plt.ylabel('Pairwise distance (FastTree)')
plt.xlabel('Clade pairing')
plt.tick_params(axis='both', which='minor', labelsize=8)
plt.title(title)
axes().set_aspect('equal', 'datalim')
plt.show()