python绘制坐标系_使用Python画一个带坐标轴的圆

Download Microsoft Visual Studio

Microsoft Visual Studio enables you develop your python Application, to use Microsoft Visual Studio developing your application, first thing is to install Visual Studio.

Download Microsoft Visual Studio online, download Visual Studio .

Choose Python when installing Microsoft Visual Studio

itemContent?repositoryId=343a5e4f-3d73-4494-9abd-5dc9100abc70&path=%2FModules%2FLinked_Image_Files%2Finstallation-python-workload.png&version=GBmaster&contentOnly=true&__v=5

After installation complete, launch the Visual Studio and create a project.

Select File > New > Project

In the New Project window, expand Installed, expand Python.

In the template, select Python Application.

Choose your Name and Location, then click OK.

Install matplotlib and numpy package

Select View > Ohter Windows > Python Environments.

In the right side of window, switch to Python Environment (In same window to the Solution Explorer).

Select one version, for example, Python 3.6. Select Packages.

In the search box, type matplotlib and select “pip install matplotlib” from PyPI.

Wait for the installation complete.

itemContent?repositoryId=343a5e4f-3d73-4494-9abd-5dc9100abc70&path=%2FModules%2FLinked_Image_Files%2Finstall-matplotlib.PNG&version=GBmaster&contentOnly=true&__v=5

Repeat above two steps for numpy.

Open python file, write code to plot a circle step by step

The equation for a circle is x^2 + y^2 = 1, therefore, y = +sqrt(1-x^2) and y = -sqrt(1-x^2).

Select Soltuion Explorer, double click on the python file to open it, in my side, the file name is PythonApplication1.py.

Import pyplot and numpy libraries.

importmatplotlib.pyplot as pltimport numpy as np

Define a Figure window with name Figure1 and size as width=5, height=5.

plt.figure(num=1,figsize=(5,5))

Use numpy.linspace to define x with some points, starting from -1 to 1, randomly generate 500 points.

Use numpy.sqrt to define the corresponding y1 and y2.

x = np.linspace(-1, 1, 500)

y1= np.sqrt(1-x**2)

y2= -np.sqrt(1-x**2)

Plot the figure and show the figure

l1, = plt.plot(x, y1, color='blue')

l2,= plt.plot(x, y2, color='blue')

...

plt.show()

Here is what you get so far:

itemContent?repositoryId=343a5e4f-3d73-4494-9abd-5dc9100abc70&path=%2FModules%2FLinked_Image_Files%2F1.1_plotFigure.png&version=GBmaster&contentOnly=true&__v=5

Customize your Figure

(NOTE: all the code need be added before the line of plt.show())

Then, you can customize the steps and points in the x axis and y axis, the functions are pyplot.xticks and pyplot.yticks.

In following code, I defined some numbers by using numpy.arange(), the numbers are -1, -0.5, 0, 0.5, 1. Use these numbers both for x axis and y axis.

new_ticks = np.arange(-1,1,0.5)

plt.xticks(new_ticks)

plt.yticks(new_ticks)

If you want to customize the border and axis location, you can use plt.gca(), the following code did three things:

Set the position of y axis to -1.

Set the postion of x axis to -1.

Remove the border of right edge.

Remove the border of top edge.

ax =plt.gca()

ax.spines['left'].set_position(('data',-1))

ax.spines['bottom'].set_position(('data',-1))

ax.spines['right'].set_color('none')

ax.spines['top'].set_color('none')

Now here is what you get:

itemContent?repositoryId=343a5e4f-3d73-4494-9abd-5dc9100abc70&path=%2FModules%2FLinked_Image_Files%2F1.2_CustomizeTicks.png&version=GBmaster&contentOnly=true&__v=5

If you want to move the axis to 0, then just change the value from -1 to 0 in above code.

ax.spines['left'].set_position(('data',0))

ax.spines['bottom'].set_position(('data',0))

Add a legend to the upper right corner, the string inside the ' should sround with $, and it will render better acording to what support best by the system.

plt.legend(handles=[l1,l2,], labels=[r'$x^2+y^2=1$'], loc='upper right')

Add axes at the end of the axis by using plt.annotate().

plt.annotate('$x$', xy=(0.98,0.5), ha='left', va='top', xycoords='axes fraction', fontsize=20)

plt.annotate('$y$', xy=(0.5,1), ha='left', va='top', xycoords='axes fraction', textcoords='offset points',fontsize=20)

This is what you get finall, pretty cool.

itemContent?repositoryId=343a5e4f-3d73-4494-9abd-5dc9100abc70&path=%2FModules%2FLinked_Image_Files%2F1.3_withlegendandAxis.png&version=GBmaster&contentOnly=true&__v=5

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值