I am a beginner for matplotlab. The expected result for me are the following
- two axes share x axis
- The two y-axis are at both ends of axes,
- when I change the figure size , x-axis and y-axis scaling 1:1
- 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?
x-axisrunning from 0 to 20 andy-axisrunning either from0-1or from0-1e6?<ol>format is preferable, and of course this material is not code.