2017-03-16 69 views
0

我正在为我的Python项目使用Python 3和Jupyter Notebook。如何使用Matplotlib在Python中实现这种特别的图表类型?

我碰到下面的链接中提到的图表:

Matplotlib Chart

我要创造我的数据类似的东西,但我有一个很难找到的代码这样做。这张图表是一个散点图,即使它在x轴上有分类值吗?或者我应该使用另一种图表类型来实现我所追求的目标?

我现在的散点图代码如下所示:

x = mySelectedData['Leisure Per GN'] 
y = mySelectedData['Spa Per GN'] 
fig, ax = plt.subplots(figsize=(8, 4)) 
colors = 'blue' 
size = 250 #(10 + np.random.rand(num_points) * 10) ** 2 
ax.scatter(x, y, s=size, c=colors, alpha=0.5) 
fig.suptitle('CY 2015 - CY 2016:Leisure Per Gn versus Spa Per Gn') 

上面的代码工作正常,既是X的y是数值。我已经改变了代码如下:

x = mySelectedData['Market'] 
y = mySelectedData['Leisure Per GN'] 

fig, ax = plt.subplots(figsize=(8, 4)) 
colors = 'green' 
size = 250 #(10 + np.random.rand(num_points) * 10) ** 2 
ax.scatter(x, y, s=size, c=colors, alpha=0.5) 
fig.suptitle('Leisure Per Gn: UK v/s Germany (CY 2016 -CY 2017)') 

在运行这些代码行,巨蟒引发以下错误消息:

ValueError: First argument must be a sequence 

我想这是与事实做“市场”是一个分类数据。

关于如何进行的任何想法?

回答

0

我会尝试你的分类数据分配任意数值,只是为了得到间距,你想要的方式,然后使用这样的设置您的标签:

import matplotlib.ticker as politicker 

#Add x ticks at specified intervals 
x_labels = ["foo", "bar"] 
label_locations = [0, 1, 2, 3] #chosen x locations 

loc = plticker.FixedLocator() 
ax1.xaxis.set_major_locator(loc) 
plt.xticks(x_labels, label_locations) 

这样,你”仍然绘制你的分类数据,但Matplotlib认为你正在绘制数字数据。

+0

谢谢。我会试试你的解决方案。但是,我对X_labels = [“foo”,“bar”]这一行有点困惑。如果我把它放在我的上下文中,我想它会如下所示:X_labels = mySelectedData ['Market'];那是对的吗? 另外,由于您稍后使用“loc = plticker.FixedLocator()”,因此不应将我们执行“import matplotlib.ticker as politicker”的行读为“import matplotlib.ticker as plticker”? – user3115933

相关问题