绘制 DataFrame x,y,z (Pivot-Table) - 切换轴
Plotting DataFrame x,y,z (Pivot-Table) - Switch axis
我想将以下 DataFrame df 绘制为线条。为了不必上传文件,我在此处创建了一个示例数组。零仅被替换为 NaN,因此它们不会出现在绘图中(图 1 - 绘图)。不幸的是,用注释掉的命令更改 table 没有任何作用。剧情还是一样。
剧情:
我想交换 x 轴和 y 轴以及交换列的顺序。最后它应该看起来像图 2.
结果:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = np.array([[1.984527906, 2.954823982, 3.929672499, 4.959261398, 5.99837202],
[1.990381781, 2.929568097, 3.95951958, 5.033245035, 5.989265035],
[1.957703504, 2.903082068, 3.940650489, 5.03420226, 6.099707906],
[2.000831255, 2.938274126, 4.000250298, 5.05252714, 6.057832594],
[1.967406661, 2.904963886, 4.101361781, 5.210597762, 6.073879772],
[1.974263791, 2.971977283, 4.159008097, 5.22840781, 6.074537953],
[2.028137225,3.163653014, 5.037366853, 0 ,0],
[2.034031388,3.242129474, 5.031475083, 0 ,0],
[0,0, 5.031525226, 0 ,0],
[0,0, 5.033628193, 0 ,0]])
df = pd.DataFrame(df, index=None).replace(0, np.NaN)
## Reshaping of the df wasn't successful.
# df = df[::-1][df.columns.tolist()[::-1]]
# df.columns = np.arange(0,df.shape[1],1)
# print(df)
fig, ax = plt.subplots(1,1)
df.plot(ax=ax)
在 Matplotlib 中交换 X 和 Y 变量
您似乎想在此处更改因变量和自变量。这是绘制数据的非典型方式。通常,像这样的事情将通过数据透视表 table 或数据帧转置来完成。但是由于您为 x_axis 使用了连续变量,这使得它变得有点困难。我认为对于这个用例,直接使用 matplotlib 绘图会更容易。以下是如何实施此策略的示例。
我也按要求颠倒了出图顺序
fig, ax = plt.subplots(figsize=(10,6))
for col in df.columns[::-1]:
ax.plot(df.iloc[:, col], df.index, label=col)
#style chart
[ax.spines[s].set_visible(False) for s in ['top', 'right']]
ax.grid(axis='y', dashes=(8,3), color='gray', alpha = 0.7)
ax.tick_params(axis='both', bottom=False, left=False)
ax.set_ylim(0,9)
ax.legend(ncol=5, loc=(0.25,1.025), edgecolor='w')
我想将以下 DataFrame df 绘制为线条。为了不必上传文件,我在此处创建了一个示例数组。零仅被替换为 NaN,因此它们不会出现在绘图中(图 1 - 绘图)。不幸的是,用注释掉的命令更改 table 没有任何作用。剧情还是一样。
剧情:
我想交换 x 轴和 y 轴以及交换列的顺序。最后它应该看起来像图 2.
结果:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = np.array([[1.984527906, 2.954823982, 3.929672499, 4.959261398, 5.99837202],
[1.990381781, 2.929568097, 3.95951958, 5.033245035, 5.989265035],
[1.957703504, 2.903082068, 3.940650489, 5.03420226, 6.099707906],
[2.000831255, 2.938274126, 4.000250298, 5.05252714, 6.057832594],
[1.967406661, 2.904963886, 4.101361781, 5.210597762, 6.073879772],
[1.974263791, 2.971977283, 4.159008097, 5.22840781, 6.074537953],
[2.028137225,3.163653014, 5.037366853, 0 ,0],
[2.034031388,3.242129474, 5.031475083, 0 ,0],
[0,0, 5.031525226, 0 ,0],
[0,0, 5.033628193, 0 ,0]])
df = pd.DataFrame(df, index=None).replace(0, np.NaN)
## Reshaping of the df wasn't successful.
# df = df[::-1][df.columns.tolist()[::-1]]
# df.columns = np.arange(0,df.shape[1],1)
# print(df)
fig, ax = plt.subplots(1,1)
df.plot(ax=ax)
在 Matplotlib 中交换 X 和 Y 变量
您似乎想在此处更改因变量和自变量。这是绘制数据的非典型方式。通常,像这样的事情将通过数据透视表 table 或数据帧转置来完成。但是由于您为 x_axis 使用了连续变量,这使得它变得有点困难。我认为对于这个用例,直接使用 matplotlib 绘图会更容易。以下是如何实施此策略的示例。
我也按要求颠倒了出图顺序
fig, ax = plt.subplots(figsize=(10,6))
for col in df.columns[::-1]:
ax.plot(df.iloc[:, col], df.index, label=col)
#style chart
[ax.spines[s].set_visible(False) for s in ['top', 'right']]
ax.grid(axis='y', dashes=(8,3), color='gray', alpha = 0.7)
ax.tick_params(axis='both', bottom=False, left=False)
ax.set_ylim(0,9)
ax.legend(ncol=5, loc=(0.25,1.025), edgecolor='w')