如何在 PyPlot 中显示范围图
How to show a range graph in PyPlot
我不确定如何准确调用它,但我想在 PyPlot 中创建一个类似于上图的图形。只关注一种颜色,我有 10 个独立的数据集,其中包含 (x, y)
个点,我可以绘制成图表。但是我想把它们放在一起展示:一个数据集是正常展示的,每个点周围都有一个“范围”阴影,显示某个 x
.[=14= 的最小值和最大值 y
]
我想你有这样的数据框的数据存储:
N = 20
df = pd.DataFrame({'x': np.linspace(0, 150000, N),
'y': np.random.random(N)})
df['y_max'] = df['y'] + np.random.random(N)
df['y_min'] = df['y'] - np.random.random(N)
x y y_max y_min
0 0.000000 0.856374 1.685085 0.181898
1 7894.736842 0.471733 0.713564 0.128606
2 15789.473684 0.817586 1.453245 0.520492
3 23684.210526 0.352486 0.464310 0.093795
4 31578.947368 0.188503 0.427685 -0.203351
5 39473.684211 0.192593 1.018089 -0.586906
6 47368.421053 0.143718 0.375640 -0.833777
7 55263.157895 0.288232 0.764800 0.035718
8 63157.894737 0.047860 0.802160 -0.776364
9 71052.631579 0.647542 1.389724 0.290451
...
其中 'y'
是实际值,'y_min'
和 'y_max'
是从您拥有的其他数据集获得的最小值和最大值。
然后你可以用 matplotlib.axes.Axes.plot
and the shaded area with matplotlib.axes.Axes.fill_between
:
绘制 'y'
fig, ax = plt.subplots()
ax.plot(df['x'], df['y'], color = 'blue')
ax.fill_between(df['x'], df['y_max'], df['y_min'], color = 'blue', alpha = 0.5)
plt.show()
完整代码
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
N = 20
df = pd.DataFrame({'x': np.linspace(0, 150000, N),
'y': np.random.random(N)})
df['y_max'] = df['y'] + np.random.random(N)
df['y_min'] = df['y'] - np.random.random(N)
fig, ax = plt.subplots()
ax.plot(df['x'], df['y'], color = 'blue')
ax.fill_between(df['x'], df['y_max'], df['y_min'], color = 'blue', alpha = 0.5)
plt.show()
我不确定如何准确调用它,但我想在 PyPlot 中创建一个类似于上图的图形。只关注一种颜色,我有 10 个独立的数据集,其中包含 (x, y)
个点,我可以绘制成图表。但是我想把它们放在一起展示:一个数据集是正常展示的,每个点周围都有一个“范围”阴影,显示某个 x
.[=14= 的最小值和最大值 y
]
我想你有这样的数据框的数据存储:
N = 20
df = pd.DataFrame({'x': np.linspace(0, 150000, N),
'y': np.random.random(N)})
df['y_max'] = df['y'] + np.random.random(N)
df['y_min'] = df['y'] - np.random.random(N)
x y y_max y_min
0 0.000000 0.856374 1.685085 0.181898
1 7894.736842 0.471733 0.713564 0.128606
2 15789.473684 0.817586 1.453245 0.520492
3 23684.210526 0.352486 0.464310 0.093795
4 31578.947368 0.188503 0.427685 -0.203351
5 39473.684211 0.192593 1.018089 -0.586906
6 47368.421053 0.143718 0.375640 -0.833777
7 55263.157895 0.288232 0.764800 0.035718
8 63157.894737 0.047860 0.802160 -0.776364
9 71052.631579 0.647542 1.389724 0.290451
...
其中 'y'
是实际值,'y_min'
和 'y_max'
是从您拥有的其他数据集获得的最小值和最大值。
然后你可以用 matplotlib.axes.Axes.plot
and the shaded area with matplotlib.axes.Axes.fill_between
:
'y'
fig, ax = plt.subplots()
ax.plot(df['x'], df['y'], color = 'blue')
ax.fill_between(df['x'], df['y_max'], df['y_min'], color = 'blue', alpha = 0.5)
plt.show()
完整代码
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
N = 20
df = pd.DataFrame({'x': np.linspace(0, 150000, N),
'y': np.random.random(N)})
df['y_max'] = df['y'] + np.random.random(N)
df['y_min'] = df['y'] - np.random.random(N)
fig, ax = plt.subplots()
ax.plot(df['x'], df['y'], color = 'blue')
ax.fill_between(df['x'], df['y_max'], df['y_min'], color = 'blue', alpha = 0.5)
plt.show()