0
我希望能創建一個函數來爲繪圖設置x軸限制。這裏是什麼,是工作目前:將繪圖傳遞給函數Matplotlib Python
import matplotlib.pyplot as plt
def scatter_plot(df, user_conditions):
data_to_plot = user_conditions['data_to_plot']
title = data_to_plot.replace("_", " ").title()
df1 = df[['time',data_to_plot]]
df1 = index_dataframe_time(df1)
plt.scatter(df1.index.to_pydatetime(), df1[data_to_plot])
min = df1.index.min()
max = df1.index.max()
plt.xlim(min, max)
plt.title('Hour of Day vs '+title, fontsize=14)
plt.show()
這是我所期待的:
def scatter_plot(df, user_conditions):
data_to_plot = user_conditions['data_to_plot']
title = data_to_plot.replace("_", " ").title()
print title
df1 = df[['time',data_to_plot]]
df1 = index_dataframe_time(df1)
plot = plt.scatter(df1.index.to_pydatetime(), df1[data_to_plot])
plot = set_limits(df1, plot)
plot.title('Hour of Day vs '+title, fontsize=14)
plot.show()
def set_limits(df, plot):
min = df.index.min()
max = df.index.max()
plot.xlim(min, max)
return plot
然而,在set_limits
與plot.xlim(min,max)
問題,
> Traceback (most recent call last):
File
> "C:/Users/Application/main.py", line 115, in <module>
>
main()
>
> File "C:/Users/Application/main.py", line
> 106,
> in main
> plot_configuration(df, user_conditions) File "C:/Users/Application/main.py", line 111,
> in plot_configuration
> scatter_plot(df, user_conditions)
File "C:/Users/Application/main.py", line 76,
> in scatter_plot
> plot = set_limits(df1, plot) File "C:/Users/Application/main.py", line 83,
> in set_limits
> plot.xlim(min, max) AttributeError: 'PathCollection' object has no attribute 'xlim'
如何可以修改set_limits來解決這個問題嗎?
你是如何導入'matplotlib'的?例如'plt'從哪裏來? – mgilson
@mgilson對不起,這個問題現在已經修復,以反映 – Withoutahold
問題是'pyplot'是一個有狀態的模塊,它對於習慣於matlab的人來說是很方便的。特別的,有狀態是爲什麼'plt.set_xlim'工作的原因 - 'pyplot'對你當前使用的圖形(和座標軸)有一個引用。如果你想自己處理這些圖,這不起作用(好),你需要使用'matplotlib.figure'提供的面向對象的接口。 [參考](http://stackoverflow.com/a/5450333/748858)。我想你可以使用'pyplot.gcf()'來獲得當前的數字...... – mgilson