pandas 如何使用 matplotlib 正确绘制数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45613391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to properly plot dataframe with matplotlib
提问by Marcos Santana
I'm trying to plot a dataframe with two columns:
Compound_ID,Averages
0 M0001,0.75
1 M0002,0.87
2 M003,0.45
Instead of showing the 'Compound_ID' on the x axis it is showing the index. When I explicitly try to plot, it raises and error.
它不是在 x 轴上显示“Compound_ID”,而是显示索引。当我明确尝试绘图时,它会引发错误。
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
df = pd.read_csv('teste.csv')
plt.plot(df['Averages'], df['Compound_ID'])
plt.show()
AttributeError: 'Series' object has no attribute 'find'
It is probably something easy to solve, but can someone take a look at my code?
这可能很容易解决,但有人可以看看我的代码吗?
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
df = pd.read_csv('teste.csv')
plt.plot(df['Averages'])
plt.show()
回答by Andrey Lukyanenko
You can do this:
你可以这样做:
plt.plot(df['Averages'])
plt.xticks(range(len(df['Compound_ID'])) , df['Compound_ID'])
This way you plot xticks separately. The first element is numerical indexes, second - names.
这样你就可以分别绘制 xticks 了。第一个元素是数字索引,第二个元素是名称。