简体   繁体   English

如何使用matplotlib和pandas进行绘图?

[英]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.

相关问题 如何 plot a matplotlib (pandas) bar plot using colors and hatches? - How to plot a matplotlib (pandas) bar plot using colors and hatches? 如何使用 matplotlib 嵌套字典 plot (不使用熊猫)? - How to plot nested dictionary using matplotlib (without using pandas)? 如何使用pandas和matplotlib生成离散数据以传递到等高线图中? - How to generate discrete data to pass into a contour plot using pandas and matplotlib? 如何使用matplotlib从熊猫对象创建箱形图? - How to create box plot from pandas object using matplotlib? 如何使用 matplotlib plot Pandas 中的两个元组列表? - How to plot two list of tuples in Pandas using matplotlib? 如何叠加熊猫图,matplotlib图和轴 - how to overlay a pandas plot, matplotlib plot, and axis 如何使用matplotlib绘制熊猫数据框行图? - How to plot a graph of pandas dataframe rows using matplotlib? 如何在 python 中使用 matplotlib 和 pandas 绘制 CSV 数据 - How to plot CSV data using matplotlib and pandas in python 如何在不使用matplotlib(仅使用pandas)的情况下使用不同类型的图(条形图和线图)绘制不同的列 - How to plot different columns with different kind of plot (bar & line) without using matplotlib (only using pandas) 使用Pandas和Matplotlib在一个图中的5个数据帧? - 5 Dataframes in one plot using Pandas & Matplotlib?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM