marcharper / python-ternary

:small_red_triangle: Ternary plotting library for python with matplotlib
MIT License
729 stars 157 forks source link

Heatmap polygon triangles and gridlines don't meet exactly #196

Open RihuiAn opened 2 years ago

RihuiAn commented 2 years ago

1 2

marcharper commented 2 years ago

Yes, this is known (#44).

RihuiAn commented 2 years ago

So, do you have any good solutions?

I don't know how to fix it by slightly moving the lines outward by the line-width.

marcharper commented 2 years ago

Can you post the code that generated the plot?

RihuiAn commented 2 years ago

def Color_point(x, z, y, scale): w = 255 x_color = x w / float(scale) y_color = y w / float(scale) z_color = z * w / float(scale) r = math.fabs(w - y_color) / w g = math.fabs(w - x_color) / w b = math.fabs(w - z_color) / w return (r, g, b, 1.)

def Generate_heatmap_data(scale): from ternary.helpers import simplex_iterator d = dict() for (i, j, k) in simplex_iterator(scale): d[(i, j, k)] = Color_point(i, j, k, scale) return d

offset, r, s = 0.3, 0.02, 100 x, y = (np.sqrt(3) + 1) r, -2 r ct, cd, cq, c = 'r', 'y', 'b', '#f0f0f0' fig, tax = ternary.figure(scale=s) data = Generate_heatmap_data(scale=100) tax.heatmap(data, style='dual-triangular', use_rgba=True, colorbar=False) tax.boundary(linewidth=2, zorder=3) tax.set_background_color(color=c, alpha=0.2) tax.gridlines(multiple=20, linewidth=1, alpha=1, horizontal_kwargs={'color': cd}, left_kwargs={'color': ct}, right_kwargs={'color': cq}) tax.top_corner_label('Dependence', fontweight='bold', position=(0 - 0.06 - x, 1 + 0.12 + 2 * x, 0), rotation=0, color=cd) tax.left_corner_label('Low streamflow', fontweight='bold', position=(0 - 0.05 - x, 0 + 0.05 + y, 0), rotation=-45, color=cq) tax.right_corner_label('High temperature', fontweight='bold', position=(1 + x, 0 + 0.05 + y, 0), rotation=45, color=ct) tax.right_axis_label('Relative $R_D$', offset=offset, color=cd) tax.left_axislabel('Relative $R{LQ}$', offset=offset, color=cq) tax.bottom_axislabel('Relative $R{HT}$', offset=offset, color=ct) tax.ticks(axis='lbr', multiple=20, linewidth=1.5, offset=0.025) tax.get_axes().axis('off') tax.clear_matplotlib_ticks() fig.set_facecolor('#f0f0f0') tax.show()