pandas matplotlib 中的气泡图或热图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31069153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bubble plot or Heatmap in matplotlib
提问by JohnE
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 Placeas x axisand Nameas y axisand Sizeto 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 ?
我试图把Placeasx axis和Nameasy axis和Size从count(00:00). 相当大的泡沫我找不到太多的例子。任何有价值的建议都被挪用。提前致谢。为什么在 (00:00) 列出现错误以及如何传递该列的值?
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
回答by JohnE
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:
我希望这可以通过将“名称”和“地点”更改为分类来实现,但没有运气(无论是情节还是seaborn)。如果您将它们转换为整数,它基本上会起作用,但是您会丢失字符串或分类的标签。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' ) )



