【问题标题】:Problems to plot all axes in a parallel coordinates plot在平行坐标图中绘制所有轴的问题
【发布时间】:2021-08-15 11:35:08
【问题描述】:

我无法在平行坐标图中绘制所有轴。我在代码中找不到这个问题的原因。我正在提供代码和电子表格 xlsx。

This is the problem in the graph

https://drive.google.com/drive/folders/1HwkQclW2oXy8C1AbNVIQNYb2LheFH8oo?usp=sharing

【问题讨论】:

  • 请详细说明想要的结果是什么。
  • 我需要图表包含所有列。在随附的照片中,您可以看到它仅正确绘制了 6 列并删除了最后 3 列。
  • 请阅读how to write a good question,以帮助人们在未来做出回应。

标签: python pandas matplotlib


【解决方案1】:

根据提供的代码,行

verts = list(zip([x for x in np.linspace(0, len(ys) - 1, len(ys) * 3 - 2, endpoint=True)],
                     np.repeat(zs[j, :], 3)[1:-1]))

迭代len(ys),但应该迭代ys.shape[1]

time = 30


df = pd.read_excel('dados_PPLOT2.xlsx', sheet_name = str(time))
df2 = df[["yearly_rainfall", "rainfall_events", "mean_rainfall_depth", "mean_rainfall_duration", 
          "mean_rainfall_intensity", "mean_dry_time", "max_rainfall_depth", "max_rainfall_duration", "max_rainfall_intensity"]]
data1 = df2.to_numpy()

dd = ["Y_rainfall (mm)", "Nº events", "Depth (mm)", "Duration (h)", 
      "Intensity (mm/h)", "Dry time (h)", "max_depth", "max_duration", "max_intensity"]
dd2 = ['0', '1', '2', '3', '4', '5']

#ynames = iris.feature_names
ys = data1
ymins = ys.min(axis=0)
ymaxs = ys.max(axis=0)
dys = ymaxs - ymins
ymins -= dys * 0.05  # add 5% padding below and above
ymaxs += dys * 0.05
dys = ymaxs - ymins

# transform all data to be compatible with the main axis
zs = np.zeros_like(ys)
zs[:, 0] = ys[:, 0]
zs[:, 1:] = (ys[:, 1:] - ymins[1:]) / dys[1:] * dys[0] + ymins[0]

fig, host = plt.subplots(figsize=(14,5))

plt.style.use('seaborn-paper') #fast

axes = [host] + [host.twinx() for i in range(ys.shape[1] - 1)]
for i, ax in enumerate(axes):
    ax.tick_params(labelsize=10)
    ax.set_ylim(ymins[i], ymaxs[i])
    ax.spines['top'].set_visible(False)
    ax.spines['bottom'].set_visible(False)

    if ax != host:
        ax.spines['left'].set_visible(False)
        ax.yaxis.set_ticks_position('right')
        ax.spines["right"].set_position(("axes", i / (ys.shape[1] - 1)))
coress = ["#126618",'#5de8ac',"#d32000", "#661e72",'#146ea3','#ffbb00']
host.set_xlim(0, ys.shape[1] - 1)
host.set_xticks(range(ys.shape[1]))
host.set_xticklabels(dd, fontsize=14)
host.tick_params(axis='x', which='major', pad=7)
host.spines['right'].set_visible(False)
host.xaxis.tick_top()
host.set_title('Mean precipitation property (MIT '+ str(time) +' min)', fontsize=18, pad=12)
colors = plt.cm.Set2.colors
legend_handles = [None for _ in dd]
for j in range(ys.shape[0]):
    # create bezier curves
    verts = list(zip([x for x in np.linspace(0, ys.shape[1] - 1, ys.shape[1] * 3 - 2, endpoint=True)],
                     np.repeat(zs[j, :], 3)[1:-1]))
    codes = [Path.MOVETO] + [Path.CURVE4 for _ in range(len(verts) - 1)]
    path = Path(verts, codes)
    patch = patches.PathPatch(path, facecolor='none', lw=2, alpha=0.5, edgecolor=coress[j], linestyle=(1, (5, 0.7)))
    host.add_patch(patch)

host.legend(dd2,
            loc='lower center', bbox_to_anchor=(0.5, -0.18),
            ncol=len(dd), fancybox=True, shadow=False, prop={'size': 12})

#plt.savefig('Metodologia2_parallel coordinates plot_Mean_BR_'+ str(time) +'min.png', format='png', dpi=300, bbox_inches='tight')   
plt.tight_layout()
plt.show()

【讨论】:

    猜你喜欢
    • 2014-09-09
    • 2015-07-25
    • 2018-11-29
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多