Skip to content

Let twin-axis aligned at the specified position #26109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Genlovy-Hoo
Copy link

PR summary

Let the left-axis and right-axis aligned at the specified position.

In some data that need twin-axis plot, a point at left-axis and another point at right-axis have same meaning, they shoud be aligned.

For example, we plot netvalue curve of a portfolio at twin-left-axis and profit raito curve at twin-right-axis, the point 1.0 at the
left-axis and the point 0.0 at the right-axis both mean the begin state of the portifolio, so they should be aligned.

general twinx plot:
image

twinx aligned plot:
image

PR checklist

[✔] new and changed code is tested

@Genlovy-Hoo Genlovy-Hoo changed the title Hyy Let twin-axis aligned at the specified position Jun 11, 2023
0, 0, 30, 0, 0, 0, 0, 0, -30, 0]
net_gain= [0, -2, -3, 5, 2, 3, 4, 5, 5, -1,
-4, -10, 2, 5, 9, 6, 0, 1, -1, 9]
df = pd.DataFrame({"net_in": net_in, "net_gain": net_gain})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be done in numpy instead of pandas?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I have already done this with numpy.

df["total_in"] = df["net_in"].cumsum()
df["value"] = df["total_in"] + df["net_gain"].cumsum()
df["value_net"] = df["value"] / df["value"].iloc[0]
df["gain_pct"] = df["value"] / df["total_in"] - 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need all these derived variables?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I have omitted the data generation process and provided the final data that needs to be plotted directly.

plt.show()


general_plot()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just in-line these. If you want to show both, I'd use two axes

fig, axs = plt.subplots(2, 1, layout='constrained')
for nn in range(2):
    ax1 = axs[nn]
    ...plotting
    if nn == 1:
       twinxalign(ax1, ax2, 1, 0)
       ax.title('align')
    else:
       ax.title('not aligned')

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have merged the drawing process into one function and used the parameter align to control whether to align or not.

def _inverse(x):
return (x - b_new) / k_new
ax_right.set_ylim([right_min_new, right_max_new])
ax_right.set_yscale("function", functions=(_forward, _inverse))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually not sure why you want a scale here. It's just linear between min/max_new?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My original intention was to confirm the linear map of right_min_new-right_max_new and right_min-right_max is performed.

But I found this step seems be already done in the set_ylim step, so there is no need to do it again. I have removed it in the new code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Needs review
Development

Successfully merging this pull request may close these issues.

3 participants