yuzie007 / mpltern

Ternary plots as projections of Matplotlib
https://yuzie007.github.io/mpltern
MIT License
39 stars 2 forks source link

Tribin is not working properly #14

Closed andieis closed 5 months ago

andieis commented 8 months ago

The tribin is calculating weird things. The output of the hexbin is correct. The main errors in the tribin are the following:

As it is working with the hexbin, I assume that it is an error with the tribin and not with my data.

To reproduce it, you find attached the code I'm running, as well as the data and a screenshot of my output. Thanks for any help in advance!

Kind regards, Andreas

grafik

import numpy as np
import matplotlib.pyplot as plt
import mpltern
import csv

with open("data.csv", "r") as file:
    csv_dict = np.array(list(csv.reader(file)))

t = csv_dict[:,0]
t = np.array([int(i) for i in t]) / 4500
l = csv_dict[:,2]
l = np.array([float(i) for i in l]) / 360
r = csv_dict[:,1]
r = np.array([float(i) for i in r]) / 70

fig = plt.figure(figsize=(10.8, 4.8))
fig.subplots_adjust(left=0.075, right=0.85, wspace=0.3)

ax = fig.add_subplot(1, 2, 1, projection="ternary")
# ax.scatter(t, l, r, color="C3", marker="x")
pc = ax.hexbin(t, l, r, gridsize=10, edgecolors="k", alpha=0.5, zorder=0.0)
ax.set_title("hexbin")
ax.taxis.set_ticks(np.array([0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500]) / 4500, labels=(0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500))
ax.laxis.set_ticks(np.array([0, 45, 90, 135, 180, 225, 270, 315, 360]) / 360, labels=(0, 45, 90, 135, 180, 225, 270, 315, 360))
ax.raxis.set_ticks(np.array([0, 10, 20, 30, 40, 50, 60, 70]) / 70, labels=(0, 10, 20, 30, 40, 50, 60, 70))

cax = ax.inset_axes([1.05, 0.1, 0.05, 0.9], transform=ax.transAxes)
colorbar = fig.colorbar(pc, cax=cax)
colorbar.set_label("Count", rotation=270, va="baseline")

ax = fig.add_subplot(1, 2, 2, projection="ternary")
# ax.scatter(t, l, r, color="C3", marker="x")
pc = ax.tribin(t, l, r, gridsize=10, edgecolors="k", alpha=0.5, zorder=0.0)
ax.set_title("tribin")
ax.taxis.set_ticks(np.array([0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500]) / 4500, labels=(0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500))
ax.laxis.set_ticks(np.array([0, 45, 90, 135, 180, 225, 270, 315, 360]) / 360, labels=(0, 45, 90, 135, 180, 225, 270, 315, 360))
ax.raxis.set_ticks(np.array([0, 10, 20, 30, 40, 50, 60, 70]) / 70, labels=(0, 10, 20, 30, 40, 50, 60, 70))

cax = ax.inset_axes([1.05, 0.1, 0.05, 0.9], transform=ax.transAxes)
colorbar = fig.colorbar(pc, cax=cax)
colorbar.set_label("Count", rotation=270, va="baseline")

plt.show()

data.csv

yuzie007 commented 8 months ago

Thank you @andieis for your report. While I also reproduced your plots, can I have one question before further survey? In your data.csv, the sums of three columns are not a constant but different for rows. To my best knowledge it is not very common for ternary plots. Can I ask how is your use-case of ternary plots?

andieis commented 8 months ago

Well, you are right - they do not sum up to a constant value for each row.

I just would like to show 4-dimensional data in a easily understandable way.

As I studied now a bit more into detail, I realized that my goal cannot be achieved using a ternary plot, as it does not sum up to a constant value.

Thanks anyways for your answer and your work!

yuzie007 commented 5 months ago

While it was not directly your concern, with this issue, I noticed that the normalization was not properly done as expected in hexbin and tribin. In the latest version 1.0.3, this is fixed. With this, I would like to close this issue. In case you find some issue, please feel free to re-open this issue or open a new issue. Thank you @andieis once again for your report and trying mpltern!