Pandas Dataframe Plotting with uneven index values creates skewed graphs

梦想与她 提交于 2021-01-27 20:03:06

问题


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)

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')

enter image description here

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

bar plots of the problem

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

linearised scatter graph

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')

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

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.

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

Update

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.

dataframe screenshot


回答1:


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.



来源:https://stackoverflow.com/questions/27109798/pandas-dataframe-plotting-with-uneven-index-values-creates-skewed-graphs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!