Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Genlovy-Hoo wants to merge6 commits intomatplotlib:main
base:main
Choose a base branch
Loading
fromGenlovy-Hoo:hyy

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 istested

@Genlovy-HooGenlovy-Hoo changed the titleHyyLet twin-axis aligned at the specified positionJun 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 parameteralign 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 ofright_min_new-right_max_new andright_min-right_max is performed.

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

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@jklymakjklymakAwaiting requested review from jklymak

At least 1 approving review is required to merge this pull request.

Assignees
No one assigned
Projects
Status: Needs review
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@Genlovy-Hoo@jklymak@melissawm

[8]ページ先頭

©2009-2025 Movatter.jp