[英]How to plot using matplotlib and pandas?
I'm currently doing this tutorial here: http://nbviewer.ipython.org/urls/bitbucket.org/hrojas/learn-pandas/raw/master/lessons/01%20-%20Lesson.ipynb 我目前正在这里进行本教程: http : //nbviewer.ipython.org/urls/bitbucket.org/hrojas/learn-pandas/raw/master/lessons/01%20-%20Lesson.ipynb
I am currently on the last section where you have to plot a graph. 我目前在最后一节中,您必须绘制图形。 I am running the code in my own idle and not iPython. 我在自己的空闲而不是iPython中运行代码。 Here is my code: 这是我的代码:
q=df['Births'].plot()
MaxValue = df['Births'].max()
MaxName = df['Names'][df['Births'] == df['Births'].max()].values
Text = str(MaxValue) + " - " + MaxName
plt.annotate(Text, xy=(1, MaxValue), xytext=(8,0),xycoords=('axes fraction', 'data'), textcoords ='offset points')
print("The most popular name")
df[df['Births'] == df['Births'].max()]
print(q)
The output I am getting is: 我得到的输出是:
The most popular name
Axes(0.125,0.1;0.775x0.8)
How do I get it to actually show the graph? 如何获得它以实际显示图形?
Adding plt.show()
should do the trick, although if I may make a recomendation..this data would be better represented by a bar chart. 添加plt.show()
应该可以解决问题,尽管如果我可以建议的话。用条形图可以更好地表示此数据。 Something like the following should do the trick: 如下所示的方法可以解决问题:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
df = pd.DataFrame(data = BabyDataSet, columns=['Names', 'Births'])
matplotlib.style.use('ggplot')
ax = df['Births'].plot(kind = 'bar')
ax.set_xticklabels(df.Names)
ax.annotate('Most Popular Name: {}: {} births'.format(df.max().Names, df.max().Births),
xy=(1, 1),
xytext=(1, 800))
ax.set_title("Number of Births by Name")
ax.set_ylabel("No. Births")
plt.show()
如果你不是在交互模式下,你可能需要添加plt.figure()
代码之前,和plt.show()
之后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.