1

I have data x = [1,2,..., 341] (x are serial no) , y = [527,533,535,537,... ,1547] I wanted to plot it in python such that the x and y axis should have same range of values and also have same interval between values. I have plotted it using excel. Here you can observe both x and y have range (0,1800) and interval between them is 200 and I have got exact square shape plot.can anybody suggest code to do same in python? While plotting in excel I added 1600 to both list and hence I got expected result. I wanted to get same result with python code without adding any value. X and Y axis should have same range of values and same interval and plot should appear as square. While I was trying to plot using matplotlib functionality it automatically chooses range and interval. Please help me with this code, Thank you!

EDIT 1 :

I wanted the aspect ratio of xticks and yticks should 1:1. That means the grid formed due to ticks should be square shaped (In figure now it forming rectangular shape). In answer, please see fig. plotted by @Redox It has a square shape grid. I would be thankful if you could answer my question.

1
  • You can set the aspect ratio using the ax.set_aspect function. Commented Dec 27, 2023 at 8:27

1 Answer 1

2

you can achieve it this way...

x=np.linspace(1,341,341)
y=np.linspace(527,1547,341)

fig, ax = plt.subplots(figsize=(8,8))
ax.plot(x, y)
ax.set_xlim(0, 1800)
ax.set_xticks(range(0,2000, 200))

ax.set_ylim(0, 1800)
ax.set_yticks(range(0,2000, 200))

ax.grid(True)

Plot

enter image description here

EDIT

The first thing to make sure you continue getting square shaped plot is to ensure that the figsize is a square, like in this case it is 8x8. Next, you need to change the x_lim and y_lim so that the number of ticks are the same. In the comment below you wanted x to be between 0 and 600 (600 units) and y to be between 600 and 2200 (1600 units). So, I took 75 as the tick units (8 blocks) for the X-axis and 200 as the tick units (8 units again) for the Y-axis. This will give me the required squares like below...

x=np.linspace(1,341,341)
y=np.linspace(527,1547,341)

fig, ax = plt.subplots(figsize=(8,8))
ax.plot(x, y)
ax.set_xlim(0, 600)  ## X is from 0 to 600
ax.set_xticks(range(0,600, 75))  ## 8 ticks in all at 75 units spread
#ax.set_xticklabels(range(0,1800, 200))

ax.set_ylim(600,2200)  ## Y is from 600 to 2200
ax.set_yticks(range(600,2200, 200)) ## 8 ticks in all at 200 units spread
#ax.set_yticks(range(10))
ax.grid(True)

enter image description here

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

4 Comments

If I started the y-axis from 527 itself, then how can I maintain the aspect ratio to be the same (1:1)? ax.set_xlim(0, 600) ax.set_ylim(600,2200) In python it is not maintained automatically.
I am not understanding the question. The size and shape of the figure is controlled by the figsize(). The xlim and ylim are used to set the start and end points for the x and y axes.
I wanted the grid that forms due to xticks and yticks should have 1:1 ratio. Means it should form a square-shaped grid as it forms in the figure that I mentioned in the question. I will edit question to get more Idea.
Check EDIT section...

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.