【问题标题】:trying to plot 3 different arrays corresponding to x,y,z试图绘制对应于 x,y,z 的 3 个不同的数组
【发布时间】:2019-09-17 11:19:51
【问题描述】:

我正在尝试制作 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

【问题讨论】:

  • 能否请您提供一些 x,y,z 的示例数据,以便重现您的代码
  • 我会把它包括在我的问题上面
  • 您可以尝试按照错误消息的建议使用-v 运行python,看看您是否收到有关导致qhull 崩溃的更多信息的错误?
  • 我在运行您的代码时没有出现错误。你还有这个样品吗?如果不是,请尝试找出导致错误的数据部分,然后发布此部分。
  • 是的,当我删除一些缺失值的数据时它确实运行了,不过我现在有点困惑,不确定它是否真的符合我的想法

标签: python matplotlib jupyter-notebook jupyter


【解决方案1】:

您的错误在于您读取 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')

【讨论】:

  • 如果关闭问题:)
猜你喜欢
  • 1970-01-01
  • 2013-02-26
  • 1970-01-01
  • 2014-09-30
  • 2015-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多