roc*_*996 2 python matplotlib python-3.x
我使用 Python 绘制了以下图:
import matplotlib.pyplot as plt
plt.hist([x*100 for x in relativeError], bins = 100)
plt.xlabel("Relative Error [%]")
plt.ylabel("#samples")
plt.axvline(x=0, linestyle='--',linewidth=1, color='grey')
Run Code Online (Sandbox Code Playgroud)
但我真正想要的是根据值是正值还是负值来拥有不同的颜色。
您可以事后为条形着色。
import numpy as np
import matplotlib.pyplot as plt
x = np.random.normal(-20, 15, 5000)
_, _, bars = plt.hist(x, bins = 100, color="C0")
for bar in bars:
if bar.get_x() > 0:
bar.set_facecolor("C1")
plt.xlabel("Relative Error [%]")
plt.ylabel("#samples")
plt.axvline(x=0, linestyle='--',linewidth=1, color='grey')
plt.show()
Run Code Online (Sandbox Code Playgroud)
相反,如果您想绘制直方图值的条形图(就像另一个答案所建议的那样),它看起来更像是
import numpy as np
import matplotlib.pyplot as plt
x = np.random.normal(-20, 15, 5000)
hist, edges = np.histogram(x, bins=100)
colors = np.array(["C0", "C1"])[(edges[:-1] > 0).astype(int)]
plt.bar(edges[:-1], hist, width=np.diff(edges), align="edge", color=colors)
plt.xlabel("Relative Error [%]")
plt.ylabel("#samples")
plt.axvline(x=0, linestyle='--',linewidth=1, color='grey')
plt.show()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3947 次 |
| 最近记录: |