试图绘制与x,y,z对应的3个不同的数组

时间:2019-09-17 11:19:51

标签: python matplotlib jupyter-notebook jupyter

我试图绘制一个从2012年到2018年在30个不同国家/地区的幸福度图,有些年份缺少幸福度值。 这些数组是“幸福”,“年份”和“国家/地区”。

我希望y轴表示幸福度,x轴表示年份,Y表示国家/地区(每个国家/地区用1到30的数字标记),这样就可以有一个颜色连接各个国家的不同程度。

每个数组的形状为(210,)。这是我的代码:

import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.tri as tri

sns.set(style="white")

x=Year
y=Hapiness
z=country

fig = plt.figure(figsize=(50, 50))
ax = fig.add_subplot(111)

nptsx, nptsy = 100, 100
xg, yg = np.meshgrid(np.linspace(x.min(), x.max(), nptsx),
                 np.linspace(y.min(), y.max(), nptsy))

triangles = tri.Triangulation(x, y)
tri_interp = tri.CubicTriInterpolator(triangles, z)
zg = tri_interp(xg, yg)

# change levels here according to your data
levels = np.linspace(1, 210, 30)
colormap = ax.contourf(xg, yg, zg, levels,
                   cmap=plt.cm.Blues,
                   norm=plt.Normalize(vmax=z.max(), vmin=z.min()))
# plot data points
ax.plot(x, y, color="#444444", marker="o", linestyle="", markersize=15)

# add a colorbar
fig.colorbar(colormap,
         orientation='vertical',  # horizontal colour bar
         shrink=0.85)

# graph extras: look at xlim and ylim
ax.set_xlim((2012, 2018))
ax.set_ylim((0, 10))
ax.set_aspect("equal", "box")

plt.show()

这是我运行代码时遇到的错误:

RuntimeError                              Traceback (most recent call 
last)
<ipython-input-65-2779759126bf> in <module>
     16                      np.linspace(y.min(), y.max(), nptsy))
     17 
---> 18 triangles = tri.Triangulation(x, y)
     19 tri_interp = tri.CubicTriInterpolator(triangles, z)
     20 zg = tri_interp(xg, yg)

 C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\tri\triangulation.py in __init__(self, x, y, 
    triangles, mask)
     52             # No triangulation specified, so use 
matplotlib._qhull to obtain
     53             # Delaunay triangulation.
---> 54             self.triangles, self._neighbors =_qhull.delaunay(x,y)
     55             self.is_delaunay = True
     56         else:

RuntimeError: Error in qhull Delaunay triangulation calculation: input inconsistency (exitcode=1); use python verbose option (-v) to see original qhull error.

我的数据示例(.CSV文件):

Entity     Code Year    World Happiness Report(Cantril Ladder(0=worst; 10=best))
Argentina   1   2012    6.4
Argentina   1   2013    6.5
Argentina   1   2014    6.6
Argentina   1   2015    6.6
Argentina   1   2016    6.4
Argentina   1   2017    6.0
Argentina   1   2018    5.7
Australia   2   2012    7.1
Australia   2   2013    7.3
Australia   2   2014    7.2
Australia   2   2015    7.3
Australia   2   2016    7.2
Australia   2   2017    7.2
Australia   2   2018    7.1
Brazil      3   2012    6.6
Brazil      3   2013    7.1
Brazil      3   2014    6.9
Brazil      3   2015    6.5
Brazil      3   2016    6.3
Brazil      3   2017    6.3
Brazil      3   2018    6.1

1 个答案:

答案 0 :(得分:0)

您的错误是您读取csv文件的方式,而没有为数据分配类型,它将被视为字符串。

尝试一下:

import pandas as pd
import numpy as np
df = pd.read_csv("untitled.csv", delimiter=',')

x=np.array(df['Year'].values, dtype='float')
y=np.array(df['Happiness'].values, dtype='float')
z=np.array(df['Code'].values, dtype='int')