首页 > 解决方案 > 如何在熊猫中正确添加图形

问题描述

当我在熊猫中绘制不同的数据集时,它没有正确显示所需的图形,而是显示了一个不正确的线性图。我应该如何解决这个问题?

import pandas as pd
import matplotlib.pyplot as plt

#cleaning the csv file
fh=pd.read_csv('Documents/ads/2019 T1_12.csv',skiprows=[0,1,3,9,15,21,27,30,31,32,33,34,35])
fh=fh.drop('Total',axis=1)
fh=fh.drop('Others', axis=1)

#plotting
plt.plot(fh['Year'],fh['Germany'])
plt.plot(fh['Year'], fh['USA'])
plt.ylabel('Number of Emigrants', fontsize=17)
plt.xlabel('Years', fontsize=17)


fh.index
fh.head()

在此处输入图像描述

标签: pythonpandasmatplotlib

解决方案


看起来USA的 dtype 是对象,导致线性图和混乱的 ylabels。

您可能需要添加thousands=','read_csv通话中。

之后,请参阅 Quang Hong 的答案以获得更好的可视化提示。


推荐阅读