![](/img/trans.png)
[英]What is the difference between (matplotlib) and (SymPy Plotting Module)?
[英]plotting with matplotlib from a module
這是我第一次使用python進行繪圖,我想我不太了解matplotlib中對象之間的交互。 我有以下模塊:
import numpy as np
import matplotlib.pyplot as plt
def plotSomething(x,y):
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xscale("log", nonposx='clip')
ax.set_yscale("log", nonposy='clip')
ax.scatter(x,y) #(1)
plt.scatter(x,y) #(2)
當函數被調用時(給定x和y),它的繪制就很好。
a)如果我注釋掉(1)或(2),則只會繪制軸的坐標,而不會繪制散點圖。
b)但是,如果(1)和(2)都未注釋,並且我將var s = 5,marker ='+'添加到(1)XOR(2)中,則該圖將同時顯示兩個標記(一個標記位於其他)-默認的“ o”和“ +”,這意味着我實際上將散點圖繪制了兩次。
但是-如果同時取消注釋(1)和(2),我要繪制兩次,為什么實際上我需要同時擁有(1)和(2)才能看到散點圖? 為什么在(a)我根本沒有散點圖的情況下?
我很困惑 誰能指導我?
發生的事情很可能與Python的垃圾回收有關。 我無法確切告訴您發生了什么,因為提供的代碼示例從不渲染圖。 我猜想您是在函數外部渲染它,在這種情況下,您實際上是在渲染(繪制)之前進行了del fig
繪制。
這應該工作:
def plotSomething(x,y):
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xscale("log", nonposx='clip')
ax.set_yscale("log", nonposy='clip')
ax.scatter(x,y)
fig.savefig('test.png')
如果要延遲渲染/繪制,請傳遞參考:
def plotSomething(x,y):
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xscale("log", nonposx='clip')
ax.set_yscale("log", nonposy='clip')
ax.scatter(x,y)
return fig
(我不是不同對象之間如何交互的專家)
您應該添加plt.show(),然后可以使用(1)或(2)。 例如:
#!/usr/bin/python
import numpy as np
import matplotlib.pyplot as plt
def plotSomething(x,y):
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xscale("log", nonposx='clip')
ax.set_yscale("log", nonposy='clip')
#ax.scatter(x,y) #(1)
plt.scatter(x,y) #(2)
plt.show()
x=[1,2,3]
y=[5,6,7]
plotSomething(x,y)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.