具有不均匀索引值的 Pandas Dataframe Plotting 创建偏斜图 [英] Pandas Dataframe Plotting with uneven index values creates skewed graphs

查看:40
本文介绍了具有不均匀索引值的 Pandas Dataframe Plotting 创建偏斜图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,它由多个实验运行组成,这些运行具有不同的x 尺度"到特定建模行为的零输入,即

I have a dataframe that consists of several experimental runs with different 'x-scales' to zero-in on a particular modelled behaviour, i.e.

  • Exp1: xs = np.linspace(0.005,0.75,10)
  • Exp2:xs = np.linspace(0.015,0.035,20)

将这些组合成单个数据帧进行处理就像 pd.concat 一样简单,但我的困难在于绘制结果.

Combining these into a single dataframe for processing is as simple as a pd.concat but my difficulty is in plotting results.

ax=v.plot(
figsize=(10,13),kind='line',
secondary_y='average_rx_delay',
logy=True,
title="Performance Comparison of Varying Packet Period Rates \n(counts on left, seconds on right)"
)
#ax.set_xlabel('Packet Emmission rate (per second)')
ax.set_ylabel('Packet Count')

正如您所看到的,数据框索引被用作您可以说的系列标题",但它没有进行数字评估,导致线条不均匀和倾斜.

As you can see, the data frame index is being used as the 'series title' you could say, but it's not being numerically assessed, leading to uneven and skewed lines.

如果您将其绘制为条形图,则会更容易看到为什么发生这种情况

It's slightly easier to see why this is happening if you plot it bar-wise

我正在寻找的是类似于下面的内容,但作为线条.

What I'm looking for is something like the below but as lines.

这是很懒惰的,很长一段路要走

Which was generated lazily going the long way

f, ax1 = plt.subplots()
ax1.scatter(list(v.index),
     v.collisions, c='r')
ax1.scatter(list(v.index),
     v.tx_counts, c='b')
ax1.scatter(list(v.index),
     v.rx_counts, c='g')
ax1.scatter(list(v.index),
     v.enqueued, c='y')
ax2=ax1.twinx()
ax2.scatter(list(v.index),
     v.average_rx_delay, c='c')

基本上,我希望线图将 v.index 作为 x 轴值,但坚持是实际数字!

Basically, I want line plots to take the v.index as the x-axis value but stick to being actual numbers!

我尝试将 x = v.index 添加到 plot 调用中,以及将索引添加为另一列,并尝试在同一列中使用新列方式,但这并不快乐.

I've tried adding x=v.index to the plot call, as well as adding the index as another column and tried using the new column in the same manner but that's been no joy.

有什么神奇的想法,还是我应该开始长期不整洁的 DIY 方式?

Any magical ideas or should I just start going the long untidy DIY way?

更新

按照@ajean的问题,这就是选择的数据的样子.请注意,PER 是上面提到的 x=v.PER 尝试的再次添加"索引列,但它被主 .plot<正确丢弃了/code> 反正.

As per @ajean's question, this is what a selection of the data looks like. Note that PER is the 'added in again' index column for the x=v.PER attempt mentioned above, but it's correctly discarded by the main .plot anyway.

推荐答案

看起来您的索引被用作分类输入.你可以试试 df.column_name = df.column_name.astype(float).我的答案基于将字符串转换为DataFrame中的浮点数.如果要用线代替点,则应使用图而不是散点图.

It looks like your index is used as a categorial input. You can try df.column_name = df.column_name.astype(float). I have based this answer on Converting strings to floats in a DataFrame. If you want lines instead of points, then you should use plot instead of scatter.

这篇关于具有不均匀索引值的 Pandas Dataframe Plotting 创建偏斜图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆