Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
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
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
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}) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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')
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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:

twinx aligned plot:

PR checklist
[✔] new and changed code istested