参考源代码:
RuntimeWarning: divide by zero encountered in log
Z = np.log(df['gpu_trans_count'].values)
from mpl_toolkits.mplot3d.axes3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
df = pd.read_csv('transcount.csv')
df = df.groupby('year').aggregate(np.mean)
gpu = pd.read_csv('gpu_transcount.csv')
gpu = gpu.groupby('year').aggregate(np.mean)
df = pd.merge(df, gpu, how='outer', left_index=True, right_index=True)
df = df.replace(np.nan, 0)
fig = plt.figure()
ax = Axes3D(fig)
X = df.index.values
Y = np.log(df['trans_count'].values)
X, Y = np.meshgrid(X, Y)
Z = np.log(df['gpu_trans_count'].values)
ax.plot_surface(X, Y, Z)
ax.set_xlabel('Year')
ax.set_ylabel('Log CPU transistor counts')
ax.set_zlabel('Log GPU transistor counts')
ax.set_title("Moore's Law & Transistor Counts")
plt.show()
运行报错:
报错1:
Traceback (most recent call last):
File "F:/Python_code/Temp_plot/temp_plot.py", line 32, in <module>
ax.plot_surface(X, Y, Z)
File "C:\Program Files\Python38\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 1556, in plot_surface
raise ValueError("Argument Z must be 2-dimensional.")
ValueError: Argument Z must be 2-dimensional.
解决办法:
添加
Z = (X*Y*Z)/(X*Y)
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2020/9/5 21:41
# @Author: lg6
# @File : temp_plot.py
from mpl_toolkits.mplot3d.axes3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.seterr(divide='ignore', invalid='ignore')
df = pd.read_csv('transcount.csv')
df = df.groupby('year').aggregate(np.mean)
gpu = pd.read_csv('gpu_transcount.csv')
gpu = gpu.groupby('year').aggregate(np.mean)
df = pd.merge(df, gpu, how='outer', left_index=True, right_index=True)
df = df.replace(np.nan, 0)
print(df)
fig = plt.figure()
ax = Axes3D(fig)
X = df.index.values
Y = np.log(df['trans_count'].values)
X, Y = np.meshgrid(X, Y)
Z = np.log(df['gpu_trans_count'].values)
Z = (X*Y*Z)/(X*Y)
ax.plot_surface(X, Y, Z)
ax.set_xlabel('Year')
ax.set_ylabel('Log CPU transistor counts')
ax.set_zlabel('Log GPU transistor counts')
ax.set_title("Moore's Law & Transistor Counts")
plt.show()
报错2:
"C:\Program Files\Python38\python3.exe" F:/Python_code/Temp_plot/temp_plot.py
F:/Python_code/Temp_plot/temp_plot.py:30: RuntimeWarning: divide by zero encountered in log
Z = np.log(df['gpu_trans_count'].values)
C:\Program Files\Python38\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py:1703: RuntimeWarning: invalid value encountered in subtract
v1 = polygons[..., i1, :] - polygons[..., i2, :]
C:\Program Files\Python38\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py:1704: RuntimeWarning: invalid value encountered in subtract
v2 = polygons[..., i2, :] - polygons[..., i3, :]
C:\Program Files\Python38\lib\site-packages\numpy\core\numeric.py:1630: RuntimeWarning: invalid value encountered in multiply
multiply(a2, b0, out=cp1)
C:\Program Files\JetBrains\PyCharm 2020.2.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:68: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
self.figure.tight_layout()
C:\Program Files\Python38\lib\site-packages\mpl_toolkits\mplot3d\proj3d.py:109: RuntimeWarning: invalid value encountered in true_divide
txs, tys, tzs = vecw[0]/w, vecw[1]/w, vecw[2]/w
Process finished with exit code 0
出错原因是数据Z中出现0,导致后面的结果运算除数为0
print(df)
trans_count gpu_trans_count
year
1971 2.300000e+03 0.000000e+00
1972 3.500000e+03 0.000000e+00
1974 4.533333e+03 0.000000e+00
1975 3.510000e+03 0.000000e+00
1976 7.500000e+03 0.000000e+00
1978 1.900000e+04 0.000000e+00
1979 4.850000e+04 0.000000e+00
1982 9.450000e+04 0.000000e+00
1983 8.500000e+03 0.000000e+00
1984 2.000000e+05 0.000000e+00
1985 1.053333e+05 0.000000e+00
1986 2.500000e+04 0.000000e+00
1988 2.500000e+05 0.000000e+00
1989 7.401175e+05 0.000000e+00
1991 6.900000e+05 0.000000e+00
1993 3.100000e+06 0.000000e+00
1994 5.789770e+05 0.000000e+00
1995 5.500000e+06 0.000000e+00
1996 4.300000e+06 0.000000e+00
1997 8.150000e+06 3.500000e+06
1998 7.500000e+06 0.000000e+00
1999 1.760000e+07 1.350000e+07
2000 3.150000e+07 2.500000e+07
2001 4.500000e+07 5.850000e+07
2002 1.375000e+08 8.500000e+07
2003 1.900667e+08 1.260000e+08
2004 3.520000e+08 1.910000e+08
2005 1.690000e+08 3.120000e+08
2006 6.040000e+08 5.325000e+08
2007 3.716000e+08 7.270000e+08
2008 9.032000e+08 1.179500e+09
2009 3.450000e+09 2.154000e+09
2010 1.511667e+09 2.946667e+09
2011 1.733500e+09 4.312712e+09
2012 2.014826e+09 5.310000e+09
2013 5.000000e+09 6.300000e+09
2014 4.310000e+09 0.000000e+00
解决办法:
方法一:
注释掉
df = df.replace(np.nan, 0)
UserWarning: Z contains NaN values. This may result in rendering artifacts.
ax.plot_surface(X, Y, Z)
C:\Program Files\JetBrains\PyCharm 2020.2.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:68: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
self.figure.tight_layout()
方法二:
在开头添加语句
np.seterr(divide='ignore', invalid='ignore')
UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
self.figure.tight_layout()
参考资料:
python :invalid value encountered in true_divide。(除法遇到无效值)
https://blog.csdn.net/wsdn782368398/article/details/89893465