绘制等高线图,其中数据框的行 names/indices 作为 y 值
Plotting a contour map with row names/indices of dataframe as y-values
以下数据框:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.random((5,5)))
df.index = [1000, 200, 50, 1, 0.1]
其中指数是指示值高度的压力水平。
制作简单等高线图时:
plt.contourf(df)
y 值与索引值不对应。我希望将对应于索引值的压力水平用作 y 轴的值。
我尝试使用 extent 参数,但这会拉伸 min/max 之间的值。压力水平分布不均。
如 matplotlib.pyplot.contourf
的文档中所述:
X, Y : array-like, optional
The coordinates of the values in Z.
X and Y must both be 2D with the same shape as Z (e.g. created via numpy.meshgrid), or they must both be 1-D such that len(X) == N
is the number of columns in Z and len(Y) == M
is the number of rows in Z.
X and Y must both be ordered monotonically.
If not given, they are assumed to be integer indices, i.e. X = range(N)
, Y = range(M)
.
所以你可以通过:
range(len(df.columns))
作为 X
- 压力水平列表,
[1000, 200, 50, 1, 0.1]
,如 Y
df
作为 Z
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.random((5, 5)))
plt.contourf(range(len(df.columns)), [1000, 200, 50, 1, 0.1], df)
plt.show()
昨天我自己也发现了:
使用 list(df.index) 从索引中检索了 y-values。
如果您使用 np.linspace 有任何 min/max 值,然后创建一个均匀间隔的 X 值,并从压力水平的总大小中减去 2:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.random((5,5)))
df.index = [1000, 200, 50, 1, 0.1]
x_vals = np.linspace(min, max, size(df.index) -2))
plt.contourf(lat_vals, press_lvl, clim)
以下数据框:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.random((5,5)))
df.index = [1000, 200, 50, 1, 0.1]
其中指数是指示值高度的压力水平。
制作简单等高线图时:
plt.contourf(df)
y 值与索引值不对应。我希望将对应于索引值的压力水平用作 y 轴的值。
我尝试使用 extent 参数,但这会拉伸 min/max 之间的值。压力水平分布不均。
如 matplotlib.pyplot.contourf
的文档中所述:
X, Y : array-like, optional
The coordinates of the values in Z.
X and Y must both be 2D with the same shape as Z (e.g. created via numpy.meshgrid), or they must both be 1-D such thatlen(X) == N
is the number of columns in Z andlen(Y) == M
is the number of rows in Z.
X and Y must both be ordered monotonically.
If not given, they are assumed to be integer indices, i.e.X = range(N)
,Y = range(M)
.
所以你可以通过:
range(len(df.columns))
作为X
- 压力水平列表,
[1000, 200, 50, 1, 0.1]
,如Y
df
作为Z
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.random((5, 5)))
plt.contourf(range(len(df.columns)), [1000, 200, 50, 1, 0.1], df)
plt.show()
昨天我自己也发现了:
使用 list(df.index) 从索引中检索了 y-values。
如果您使用 np.linspace 有任何 min/max 值,然后创建一个均匀间隔的 X 值,并从压力水平的总大小中减去 2:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.random((5,5)))
df.index = [1000, 200, 50, 1, 0.1]
x_vals = np.linspace(min, max, size(df.index) -2))
plt.contourf(lat_vals, press_lvl, clim)