Pandas以降序绘制x或index_column [英] Pandas plot x or index_column in descending order

查看:595
本文介绍了Pandas以降序绘制x或index_column的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据化学分析中导入的csv文件做一些绘图。所以我导入如下:

I wanted to do some plots based on imported csv files from a chemical analysis. So I import as follows:

In [91]:

df = pd.read_csv('/file_location/Untitled 1.csv', delimiter = '\;', index_col = 'IR')
df

Out[91]:
Sample 1    Sample 2
IR      
300 1   0
400 5   4
500 6   0
600 0   8
4 rows × 2 columns



 In [98]:

df.plot()

精细看起来很好。

按照惯例,我用x轴以降序绘制这种类型的数据。最右边的数字(不要问我为什么)。所以我重新排序index-col:

By convention this type of data i plotted with the x axis in descending order. Highest number to the right (do not ask me why). So i reorder the index-col:

In [97]:

df2 = df.sort_index(axis=0, ascending=False, kind='quicksort')
df2
Out[97]:
Sample 1    Sample 2
IR      
600 0   8
500 6   0
400 5   4
300 1   0
4 rows × 2 columns

太棒了!

In [96]:

df2.plot()
Out[96]:

但是当我绘制它看起来一样(/ sadpanda)

But when i Plot it looks the same (/sadpanda)

任何想法=)?

推荐答案

另一种方法是是在matplotlib中反转x轴的方向。这里代码的关键位是 plt.gca()。invert_xaxis()。注意:这会将x轴保留为整数轴。示例代码如下:

Another approach would be to invert the direction of the x-axis in matplotlib. The key bit of code here is plt.gca().invert_xaxis(). Note: this leaves the x-axis as an integer axis. Example code follows:

from StringIO import StringIO # for python 2.7; import from io for python 3
import pandas as pd
import matplotlib.pyplot as plt 

# get data
data = """,sample1, sample2
300, 1,   0
400, 5,   4
500, 6,   0
600, 0,   8"""    
df = pd.read_csv(StringIO(data), header=0, index_col=0, skipinitialspace=True)

# and plot
df.plot()
plt.gca().invert_xaxis()
plt.show()

这篇关于Pandas以降序绘制x或index_column的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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