3

I am a beginner for matplotlab. The expected result for me are the following

  1. two axes share x axis
  2. The two y-axis are at both ends of axes,
  3. when I change the figure size , x-axis and y-axis scaling 1:1
  4. there are legend.

I try do make code for the requirement, try twinx() and set_aspect as 'equal' but it seems impossible. The following is my test code. I don't know how to set the param to get the following result:

  • when change the figure size by mouse, x-axis and y-axis scaling 1:1
  • y2 label is on the right of the left axis

I also try twinx, but it couldn't set the "x-axis and y-axis scaling 1:1**"

import numpy as np
import matplotlib.pyplot as plt

x1 = np.arange(10)
x2 = np.arange(20)
y1 = x1*x1*10000
y2 = np.sin(x2)


#adjust = 'datalim'
adjust = 'box'
#asp = 'equal'
asp = 'auto'
fig = plt.figure()
ax1 = fig.add_subplot(111,aspect=asp)
ax2 = fig.add_subplot(111,aspect=asp,frameon=False,sharex=ax1)
ax1.set_adjustable(adjust)
ax1.set_xlabel("xxxxxxxx")
ax1.set_ylabel("y1")
ax1.plot(x1,y1,label='ax1')
ax2.set_adjustable(adjust)
ax2.get_yaxis().set_ticks_position('right')
ax2.set_ylabel("y2",color='tab:red')
ax2.plot(x2,y2,label='ax2',color='tab:red')
ax1.tick_params(axis='y', labelcolor='tab:blue') 
ax1.tick_params(axis='y', labelcolor='tab:red') 
ax1.legend(loc='upper left', bbox_to_anchor=(1, 0.5))
ax2.legend(loc='upper left', bbox_to_anchor=(1, 0.6))
plt.show()

about "change the figure size by mouse" :

If you move the mouse to the edge of the app window in win10, you can use the mouse to resize the window

when the figure is displayed, we can resize the window by mouse ,I want to lock the ratio of the x-axis and x-axis display length ,No matter how I change the window size, the display length of the x-axis and y-axis is always the same.

the following code with one x and y axis, it is used to show the expected result, but there is just one x and one y in it.

    import numpy as np
    import matplotlib.pyplot as plt
    x1 = np.arange(10)
    x2 = np.arange(20)
    y1 = x1*x1*10000
    y2 = np.sin(x2)

    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax1.set_adjustable('box')
    ax1.set_aspect('equal')
    ax1.set_xlabel("xxxxxxxx")
    ax1.set_ylabel("y1")
    ax1.plot(x2*0.1,y2)
    plt.show()

I hope to get expected result in standard Python IDE (IDLE). then I can move the code to my real project. In my real project, I create a figure and move it to wxpython code as the following:

self.figure = Figure()
self.canvas = FigureCanvasWxAgg(self, -1, self.figure)

Since the wxpython panel need to change his size, the figure will follow it.

One way is to write a function to auto calculate and change the size of the figure, but it is complex, is there another way to do so?

3
  • I'm not sure I understand your question. Are you trying to make equal scale with x-axis running from 0 to 20 and y-axis running either from 0-1 or from 0-1e6? Commented May 20, 2020 at 2:29
  • thanks for your help ,and I am sorry about my describe is not clear. you are right , actually there are two axes in one figure, i hope to make equal scale with x-axis and two y-axis, the x-axis is shared by the two axes, the two y-axis has different major tick and minor tick (e.g 0,1,2,3 and 0,10000,20000...) , x-axis with y-axis has same length on one figure( if the axes is square, when I move the figure from a square to rectangle, the axes is also a square) . do you know how to do it? Commented May 20, 2020 at 3:03
  • Hi @Quang: thanks for wanting to edit questions here. Could I trouble you not to use code formatting for ordered lists? Using the standard <ol> format is preferable, and of course this material is not code. Commented May 20, 2020 at 10:04

1 Answer 1

1

An equal aspect ratio is not what you want, because it would mean that one unit on the x-axis has the same length on the plot as one unit on the y-axis. So e.g. your y2-axis would have to be roughly 40,000 (that's 800,000 / 20) times as long as your x-axis. On top of that, it's impossible to have an equal aspect ratio for two y-axes in the same plot, unless they both cover the same number of units.

If you need a square figure, just set the figure size to two equal numbers, e.g.

fig = plt.figure(figsize=[8, 8])

Regardless of your code, what happens when you "change the figure size by mouse" will depend on the program you're doing that with.

Sign up to request clarification or add additional context in comments.

1 Comment

Arne, thanks since the limitation of comments length , I add comment as the new answer to explain the "change the figure size by mouse" , actually it should be "change the windows size by mouse in win10" , and I add a temp code to show it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.