问题描述
我试图绘制一个动态大小的泡泡(散点图)。当我尝试绘制随机数据时,我可以很好地绘制。但是,当我试图解析我的输入文件,我不能绘制。输入:
编号,地点,方式,名称,00 :00:00,12:00:00
123,London,Air,Apollo,342,972
123,London,Rail,Beta,2352,342 $ b $ 123,Paris,Bus,Beta, 545,353 $ b $ 345,巴黎,巴士,拉瓦,652,974 $ b $ 345,罗马,巴士,拉瓦,2325,56 $ b $ 345,伦敦,Air,Rava,2532,9853
567,巴黎,空中,阿波罗,545,544
567,罗马,铁路,阿波罗,5454,5 $ b $ 876,日本,铁路,阿波罗,644,54 $ b $ 876,日本,巴士,贝塔,45 ,57
程式:
从pandas导入pandas作为pd
导入DataFrame
导入pandas.io.data
导入matplotlib.pyplot作为plt
导入numpy作为np
将seaborn导入为sns
df = pd.read_csv('text_2.csv')
#泡泡变化的大小
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(df ['Place'], df ['Name'],s = df ['00:00:00'])#增加第三个可变收益作为泡沫的大小
plt.show()
我想把放置为
x轴
和名称
作为 y轴
和大小
取自计数(00:00)
。巨大的泡沫,我找不到很多的例子。任何有价值的建议是适当的。提前致谢。
为什么我会在(00:00)列出错,如何传递该列的值?
$ b
Traceback(最近一次调用的最后一个):
在< module>文件中的Bubble_plot.py
ax.scatter(df ['Place'],df ['Name'],s = df ['00:00:00'])#增加第三个可变收益作为泡泡的大小
File /usr/lib/pymodules/python2.7/matplotlib/axes.py,第6266行,分散
x,y,s,c = cbook.delete_masked_points(x,y,s,c)
文件/usr/lib/pymodules/python2.7/matplotlib/cbook.py,第1774行,在delete_masked_points
中增加ValueError(第一个参数必须是一个序列)
ValueError:第一个参数必须是一个序列
我希望这可以工作只要将名称和地点更改为分类,但没有任何运气(无论是情节或seaborn)。如果将它们转换为整数,它基本上可以工作,但是随后丢失了字符串或分类的标签。 FWIW:
pre $ df2 = df.copy()
for ['Place','Name']
df2 [c] = df2 [c] .astype('category')。cat.codes
fig = plt.figure()
ax = fig.add_subplot(1 ,1,1)
ax.scatter(df2 ['Place'],df2 ['Name'],s = df2 ['00:00:00'])
或者,也许热图更好?它似乎接受类别,所以你可以免费标签。
df3 = df.copy()
c'[Place','Name']:
df3 [c] = df3 [c] .astype('category')
sns.heatmap(df3.pivot_table(index = 'Place',columns ='Name',values = '00:00:00'))
I am trying to plot a dynamically size able bubble (scatter map). When I try to plot with random data I can very well plot. But when I am trying to parse my input file I am not able to plot.
Input:
Nos,Place,Way,Name,00:00:00,12:00:00
123,London,Air,Apollo,342,972
123,London,Rail,Beta,2352,342
123,Paris,Bus,Beta,545,353
345,Paris,Bus,Rava,652,974
345,Rome,Bus,Rava,2325,56
345,London,Air,Rava,2532,9853
567,Paris,Air,Apollo,545,544
567,Rome,Rail,Apollo,5454,5
876,Japan,Rail,Apollo,644,54
876,Japan,Bus,Beta,45,57
Program:
import pandas as pd
from pandas import DataFrame
import pandas.io.data
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
df=pd.read_csv('text_2.csv')
#SIZE OF BUBBLES CHANGES
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(df['Place'],df['Name'], s=df['00:00:00']) # Added third variable income as size of the bubble
plt.show()
I am trying to put Place
as x axis
and Name
as y axis
and Size
to be taken from the count(00:00)
. Sizable bubble I could not find much of examples around. Any valuable suggestions is appropriated. Thanks in Advance.Why do I get error at (00:00) column and how do I pass the values of that column ?
Error:
Traceback (most recent call last):
File "Bubble_plot.py", line 18, in <module>
ax.scatter(df['Place'],df['Name'], s=df['00:00:00']) # Added third variable income as size of the bubble
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 6266, in scatter
x, y, s, c = cbook.delete_masked_points(x, y, s, c)
File "/usr/lib/pymodules/python2.7/matplotlib/cbook.py", line 1774, in delete_masked_points
raise ValueError("First argument must be a sequence")
ValueError: First argument must be a sequence
I was hoping this might work by just changing 'Name' and 'Place' to categoricals, but no luck there (with either plot or seaborn). It will basically work if you convert them to integers but then you lose the labels that you'd have with strings or categoricals. FWIW:
df2 = df.copy()
for c in ['Place','Name']:
df2[c] = df2[c].astype('category').cat.codes
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(df2['Place'],df2['Name'], s=df2['00:00:00'])
Or maybe a heatmap would work better? It seems to accept categoricals, so you get the labeling for free.
df3 = df.copy()
for c in ['Place','Name']:
df3[c] = df3[c].astype('category')
sns.heatmap( df3.pivot_table( index='Place', columns='Name', values='00:00:00' ) )
这篇关于泡泡图或matplotlib中的热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!