避免在matplotlib的X轴中排序,并使用多个y轴打印公共X轴

2024-09-29 01:19:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我想澄清这篇文章中的两个问题。你知道吗

我有一张像下图一样的照片。 enter image description here

<强>1。绘图问题:。 当我尝试绘制column 0 with column 1时,值会被排序。你知道吗

示例:在col_0中,我有从112 till 0开始的值。 当我使用下面的代码时,这些值按升序排序,图形显示反转的X轴图。你知道吗

plt.plot(df.col_0, df.col_1)

enter image description here

避免对X轴值排序的最佳方法是什么?你知道吗

<强>2。单个图形中的所有参数 我想在一个绘图中绘制所有参数。除X轴外,所有其他参数值都在0 to 1(相同比例)之间 什么是最好的Python方式。 任何帮助都将不胜感激。你知道吗


Tags: 代码图形绘图示例df参数排序with
2条回答

我不明白你所说的“排序”是什么意思——它不是绘制112,0.90178并连接到110.89899,0.90779,等等吗?你知道吗

若要共享X轴但有2个Y轴(某些集合在其上绘制),则use twinx

fig, ax1 = plt.subplots()
ax1.plot(df.col_0, df.col_1)
ax2 = ax1.twinx()
ax2.plot(df.col_0, df.col_2)

回复:如何按你想要的顺序绘图

我相信你的意图是实际绘制这些值与时间或指数的关系。为此,我建议:

fig, ax1 = plt.subplots()
ax1.plot(df['Time'], df.col_0) # or df.index, df.col_0
ax2 = ax1.twinx()
ax2.plot(df['Time'], df.col_1)

尝试根据索引绘制序列/数据帧:

col_to_draw = [col for col in df.columns if col!='col0']

# if your data frame is indexed as 0,1,2,... ignore this step
tmp_df = df.reset_index()

ax = tmp_df[col_to_draw].plot(figsize=(10,6))
xtick_vals = ax.get_xticks()
ax.set_xticklabels(tmp_df.col0[xtick_vals].tolist())

输出:

enter image description here

相关问题 更多 >