Python:创建一个堆叠的Barplot的麻烦
我被困在试图在Python中创建一个堆叠的Barplot。 我的数据集具有以下柱子: TweetID,文本,主题标签,位置,语言,情感,日期,
我希望能够在堆叠的Barplot中获得每个位置(前20个位置)的情感。
我尝试了以下内容,但没有成功:
import matplotlib.pyplot as plt
#Sentiment per Location
df2 = pd.crosstab(ft_data.location,ft_data.Sentiment)
df3 = df2.reset_index()
# plot data in stack manner of bar type
plt.figure(figsize=(20,5))
plt.bar(df3['location'], df3['negative'], color='r')
plt.bar(df3['location'], df3['neutral'], bottom=df3['negative'], color='y')
plt.bar(df3['location'], df3['positive'], bottom=df3['negative']+df3['neutral'], color='g')
plt.xticks(rotation=90)
plt.xlabel("location")
plt.ylabel("Sentiment")
plt.legend(["Negative", "Neutral", "Positive"])
plt.title("Sentiment by location")
plt.show()
我会遇到多个错误,不确定这是否是正确的方法。 我认为问题是由于不同位置的数量和一些分组丢失。
帮助您非常感谢!
数据集:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 115311 entries, 11718 to 104593
Data columns (total 17 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 tweetid 115311 non-null float64
1 text 115311 non-null object
2 hashtags 115311 non-null object
3 tweetcreatedts 115311 non-null datetime64[ns]
4 location 115311 non-null object
5 language 115311 non-null object
6 Sentiment 115311 non-null object
7 negative 115311 non-null float64
8 neutral 115311 non-null float64
9 positive 115311 non-null float64
10 date 115311 non-null datetime64[ns]
11 year 115311 non-null int64
12 month 115311 non-null int64
13 day 115311 non-null int64
14 day_of_year 115311 non-null int64
15 quarter 115311 non-null int64
16 season 115311 non-null int64
dtypes: datetime64[ns](2), float64(4), int64(6), object(5)
tweetid text \
11718 1.497725e+18 president zelensky reportedly declined offers ...
11915 1.497725e+18 gas pipeline bursting kharkiv ukraine stopruss...
11637 1.497725e+18 9 countries whove banned russian aircraft air...
11780 1.497725e+18 9 countries whove banned russian aircraft air...
10328 1.497725e+18 agitation belarusian anarchists soldier enemy ...
hashtags tweetcreatedts \
11718 [] 2022-02-27 00:07:19
11915 [Kharkiv, ukraine, StopRussia, StopPutin] 2022-02-27 00:07:29
11637 [Ukraine] 2022-02-27 00:07:41
11780 [Ukraine] 2022-02-27 00:07:41
10328 [] 2022-02-27 00:07:43
location language Sentiment negative neutral positive \
11718 London en neutral 0.422550 0.562111 0.015338
11915 広島 en neutral 0.296707 0.676842 0.026451
11637 JAPAN en neutral 0.230520 0.716168 0.053311
11780 JAPAN en neutral 0.230520 0.716168 0.053311
10328 Athens/Agrinio en negative 0.556276 0.432886 0.010838
错误
/opt/conda/lib/python3.7/site-packages/IPython/core/pylabtools.py:151: UserWarning:
Glyph 129499 (\N{VAMPIRE}) missing from current font.
---------------------------------------------------------------------------
ParseException Traceback (most recent call last)
/opt/conda/lib/python3.7/site-packages/matplotlib/_mathtext.py in parse(self, s, fonts_object, fontsize, dpi)
2236 try:
-> 2237 result = self._expression.parseString(s)
2238 except ParseBaseException as err:
/opt/conda/lib/python3.7/site-packages/pyparsing/core.py in parse_string(self, instring, parse_all, parseAll)
1133 # catch and re-raise exception from here, clearing out pyparsing internal stack trace
-> 1134 raise exc.with_traceback(None)
1135 else:
ParseException: Expected end of text, found '$' (at char 11), (line:1, col:12)
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
/opt/conda/lib/python3.7/site-packages/IPython/core/formatters.py in __call__(self, obj)
339 pass
340 else:
--> 341 return printer(obj)
342 # Finally look for special method names
343 method = get_real_method(obj, self.print_method)
/opt/conda/lib/python3.7/site-packages/IPython/core/pylabtools.py in print_figure(fig, fmt, bbox_inches, base64, **kwargs)
149 FigureCanvasBase(fig)
150
--> 151 fig.canvas.print_figure(bytes_io, **kw)
152 data = bytes_io.getvalue()
153 if fmt == 'svg':
/opt/conda/lib/python3.7/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, pad_inches, bbox_extra_artists, backend, **kwargs)
2293 )
2294 with getattr(renderer, "_draw_disabled", nullcontext)():
-> 2295 self.figure.draw(renderer)
2296
2297 if bbox_inches:
/opt/conda/lib/python3.7/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
71 @wraps(draw)
72 def draw_wrapper(artist, renderer, *args, **kwargs):
---> 73 result = draw(artist, renderer, *args, **kwargs)
74 if renderer._rasterizing:
75 renderer.stop_rasterizing()
/opt/conda/lib/python3.7/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer)
48 renderer.start_filter()
49
---> 50 return draw(artist, renderer)
51 finally:
52 if artist.get_agg_filter() is not None:
/opt/conda/lib/python3.7/site-packages/matplotlib/figure.py in draw(self, renderer)
2836 self.patch.draw(renderer)
2837 mimage._draw_list_compositing_images(
-> 2838 renderer, self, artists, self.suppressComposite)
2839
2840 for sfig in self.subfigs:
/opt/conda/lib/python3.7/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
130 if not_composite or not has_images:
131 for a in artists:
--> 132 a.draw(renderer)
133 else:
134 # Composite any adjacent images together
/opt/conda/lib/python3.7/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer)
48 renderer.start_filter()
49
---> 50 return draw(artist, renderer)
51 finally:
52 if artist.get_agg_filter() is not None:
/opt/conda/lib/python3.7/site-packages/matplotlib/axes/_base.py in draw(self, renderer)
3053 artists.remove(spine)
3054
-> 3055 self._update_title_position(renderer)
3056
3057 if not self.axison:
/opt/conda/lib/python3.7/site-packages/matplotlib/axes/_base.py in _update_title_position(self, renderer)
2989 if (ax.xaxis.get_ticks_position() in ['top', 'unknown']
2990 or ax.xaxis.get_label_position() == 'top'):
-> 2991 bb = ax.xaxis.get_tightbbox(renderer)
2992 else:
2993 if 'outline' in ax.spines:
/opt/conda/lib/python3.7/site-packages/matplotlib/axis.py in get_tightbbox(self, renderer, for_layout_only)
1103 ticks_to_draw = self._update_ticks()
1104
-> 1105 self._update_label_position(renderer)
1106
1107 # go back to just this axis's tick labels
/opt/conda/lib/python3.7/site-packages/matplotlib/axis.py in _update_label_position(self, renderer)
2081 # get bounding boxes for this axis and any siblings
2082 # that have been set by `fig.align_xlabels()`
-> 2083 bboxes, bboxes2 = self._get_tick_boxes_siblings(renderer=renderer)
2084
2085 x, y = self.label.get_position()
/opt/conda/lib/python3.7/site-packages/matplotlib/axis.py in _get_tick_boxes_siblings(self, renderer)
1879 axis = getattr(ax, f"{axis_name}axis")
1880 ticks_to_draw = axis._update_ticks()
-> 1881 tlb, tlb2 = axis._get_tick_bboxes(ticks_to_draw, renderer)
1882 bboxes.extend(tlb)
1883 bboxes2.extend(tlb2)
/opt/conda/lib/python3.7/site-packages/matplotlib/axis.py in _get_tick_bboxes(self, ticks, renderer)
1084 """Return lists of bboxes for ticks' label1's and label2's."""
1085 return ([tick.label1.get_window_extent(renderer)
-> 1086 for tick in ticks if tick.label1.get_visible()],
1087 [tick.label2.get_window_extent(renderer)
1088 for tick in ticks if tick.label2.get_visible()])
/opt/conda/lib/python3.7/site-packages/matplotlib/axis.py in <listcomp>(.0)
1084 """Return lists of bboxes for ticks' label1's and label2's."""
1085 return ([tick.label1.get_window_extent(renderer)
-> 1086 for tick in ticks if tick.label1.get_visible()],
1087 [tick.label2.get_window_extent(renderer)
1088 for tick in ticks if tick.label2.get_visible()])
/opt/conda/lib/python3.7/site-packages/matplotlib/text.py in get_window_extent(self, renderer, dpi)
908
909 with cbook._setattr_cm(self.figure, dpi=dpi):
--> 910 bbox, info, descent = self._get_layout(self._renderer)
911 x, y = self.get_unitless_position()
912 x, y = self.get_transform().transform((x, y))
/opt/conda/lib/python3.7/site-packages/matplotlib/text.py in _get_layout(self, renderer)
316 if clean_line:
317 w, h, d = renderer.get_text_width_height_descent(
--> 318 clean_line, self._fontproperties, ismath=ismath)
319 else:
320 w = h = d = 0
/opt/conda/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py in get_text_width_height_descent(self, s, prop, ismath)
263 if ismath:
264 ox, oy, width, height, descent, fonts, used_characters = \
--> 265 self.mathtext_parser.parse(s, self.dpi, prop)
266 return width, height, descent
267
/opt/conda/lib/python3.7/site-packages/matplotlib/mathtext.py in parse(self, s, dpi, prop, _force_standard_ps_fonts)
433 # mathtext.fontset rcParams also affect the parse (e.g. by affecting
434 # the glyph metrics).
--> 435 return self._parse_cached(s, dpi, prop, _force_standard_ps_fonts)
436
437 @functools.lru_cache(50)
/opt/conda/lib/python3.7/site-packages/matplotlib/mathtext.py in _parse_cached(self, s, dpi, prop, force_standard_ps_fonts)
454 self.__class__._parser = _mathtext.Parser()
455
--> 456 box = self._parser.parse(s, font_output, fontsize, dpi)
457 font_output.set_canvas_size(box.width, box.height, box.depth)
458 return font_output.get_results(box)
/opt/conda/lib/python3.7/site-packages/matplotlib/_mathtext.py in parse(self, s, fonts_object, fontsize, dpi)
2240 err.line,
2241 " " * (err.column - 1) + "^",
-> 2242 str(err)])) from err
2243 self._state_stack = None
2244 self._em_width_cache = {}
ValueError:
PSYCHOPATHS$#mercer#devos nra#blood$$#oligarch$#501c#brexit#bot epshteyn#murdoch#anschutz#SCOTUS*bribes FuxNews#blavatnik#sfeinberg#schwarzman#sackler
^
Expected end of text, found '$' (at char 11), (line:1, col:12)
I am stuck trying to create a stacked barplot for my sentiment analysis in python.
My dataset has the following colums:
tweetid,text,hashtags,location,language,Sentiment,date
I would like to be able to get the sentiment per location (top 20 locations) in a stacked barplot.
I have tried the following but am unsuccessful:
import matplotlib.pyplot as plt
#Sentiment per Location
df2 = pd.crosstab(ft_data.location,ft_data.Sentiment)
df3 = df2.reset_index()
# plot data in stack manner of bar type
plt.figure(figsize=(20,5))
plt.bar(df3['location'], df3['negative'], color='r')
plt.bar(df3['location'], df3['neutral'], bottom=df3['negative'], color='y')
plt.bar(df3['location'], df3['positive'], bottom=df3['negative']+df3['neutral'], color='g')
plt.xticks(rotation=90)
plt.xlabel("location")
plt.ylabel("Sentiment")
plt.legend(["Negative", "Neutral", "Positive"])
plt.title("Sentiment by location")
plt.show()
I get multiple errors and am not sure if this is the right method.
I think the problem is due the amount of different location and some grouping missing.
Help is much appreciated!
Dataset:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 115311 entries, 11718 to 104593
Data columns (total 17 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 tweetid 115311 non-null float64
1 text 115311 non-null object
2 hashtags 115311 non-null object
3 tweetcreatedts 115311 non-null datetime64[ns]
4 location 115311 non-null object
5 language 115311 non-null object
6 Sentiment 115311 non-null object
7 negative 115311 non-null float64
8 neutral 115311 non-null float64
9 positive 115311 non-null float64
10 date 115311 non-null datetime64[ns]
11 year 115311 non-null int64
12 month 115311 non-null int64
13 day 115311 non-null int64
14 day_of_year 115311 non-null int64
15 quarter 115311 non-null int64
16 season 115311 non-null int64
dtypes: datetime64[ns](2), float64(4), int64(6), object(5)
tweetid text \
11718 1.497725e+18 president zelensky reportedly declined offers ...
11915 1.497725e+18 gas pipeline bursting kharkiv ukraine stopruss...
11637 1.497725e+18 9 countries whove banned russian aircraft air...
11780 1.497725e+18 9 countries whove banned russian aircraft air...
10328 1.497725e+18 agitation belarusian anarchists soldier enemy ...
hashtags tweetcreatedts \
11718 [] 2022-02-27 00:07:19
11915 [Kharkiv, ukraine, StopRussia, StopPutin] 2022-02-27 00:07:29
11637 [Ukraine] 2022-02-27 00:07:41
11780 [Ukraine] 2022-02-27 00:07:41
10328 [] 2022-02-27 00:07:43
location language Sentiment negative neutral positive \
11718 London en neutral 0.422550 0.562111 0.015338
11915 広島 en neutral 0.296707 0.676842 0.026451
11637 JAPAN en neutral 0.230520 0.716168 0.053311
11780 JAPAN en neutral 0.230520 0.716168 0.053311
10328 Athens/Agrinio en negative 0.556276 0.432886 0.010838
Error
/opt/conda/lib/python3.7/site-packages/IPython/core/pylabtools.py:151: UserWarning:
Glyph 129499 (\N{VAMPIRE}) missing from current font.
---------------------------------------------------------------------------
ParseException Traceback (most recent call last)
/opt/conda/lib/python3.7/site-packages/matplotlib/_mathtext.py in parse(self, s, fonts_object, fontsize, dpi)
2236 try:
-> 2237 result = self._expression.parseString(s)
2238 except ParseBaseException as err:
/opt/conda/lib/python3.7/site-packages/pyparsing/core.py in parse_string(self, instring, parse_all, parseAll)
1133 # catch and re-raise exception from here, clearing out pyparsing internal stack trace
-> 1134 raise exc.with_traceback(None)
1135 else:
ParseException: Expected end of text, found '
(at char 11), (line:1, col:12)
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
/opt/conda/lib/python3.7/site-packages/IPython/core/formatters.py in __call__(self, obj)
339 pass
340 else:
--> 341 return printer(obj)
342 # Finally look for special method names
343 method = get_real_method(obj, self.print_method)
/opt/conda/lib/python3.7/site-packages/IPython/core/pylabtools.py in print_figure(fig, fmt, bbox_inches, base64, **kwargs)
149 FigureCanvasBase(fig)
150
--> 151 fig.canvas.print_figure(bytes_io, **kw)
152 data = bytes_io.getvalue()
153 if fmt == 'svg':
/opt/conda/lib/python3.7/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, pad_inches, bbox_extra_artists, backend, **kwargs)
2293 )
2294 with getattr(renderer, "_draw_disabled", nullcontext)():
-> 2295 self.figure.draw(renderer)
2296
2297 if bbox_inches:
/opt/conda/lib/python3.7/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
71 @wraps(draw)
72 def draw_wrapper(artist, renderer, *args, **kwargs):
---> 73 result = draw(artist, renderer, *args, **kwargs)
74 if renderer._rasterizing:
75 renderer.stop_rasterizing()
/opt/conda/lib/python3.7/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer)
48 renderer.start_filter()
49
---> 50 return draw(artist, renderer)
51 finally:
52 if artist.get_agg_filter() is not None:
/opt/conda/lib/python3.7/site-packages/matplotlib/figure.py in draw(self, renderer)
2836 self.patch.draw(renderer)
2837 mimage._draw_list_compositing_images(
-> 2838 renderer, self, artists, self.suppressComposite)
2839
2840 for sfig in self.subfigs:
/opt/conda/lib/python3.7/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
130 if not_composite or not has_images:
131 for a in artists:
--> 132 a.draw(renderer)
133 else:
134 # Composite any adjacent images together
/opt/conda/lib/python3.7/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer)
48 renderer.start_filter()
49
---> 50 return draw(artist, renderer)
51 finally:
52 if artist.get_agg_filter() is not None:
/opt/conda/lib/python3.7/site-packages/matplotlib/axes/_base.py in draw(self, renderer)
3053 artists.remove(spine)
3054
-> 3055 self._update_title_position(renderer)
3056
3057 if not self.axison:
/opt/conda/lib/python3.7/site-packages/matplotlib/axes/_base.py in _update_title_position(self, renderer)
2989 if (ax.xaxis.get_ticks_position() in ['top', 'unknown']
2990 or ax.xaxis.get_label_position() == 'top'):
-> 2991 bb = ax.xaxis.get_tightbbox(renderer)
2992 else:
2993 if 'outline' in ax.spines:
/opt/conda/lib/python3.7/site-packages/matplotlib/axis.py in get_tightbbox(self, renderer, for_layout_only)
1103 ticks_to_draw = self._update_ticks()
1104
-> 1105 self._update_label_position(renderer)
1106
1107 # go back to just this axis's tick labels
/opt/conda/lib/python3.7/site-packages/matplotlib/axis.py in _update_label_position(self, renderer)
2081 # get bounding boxes for this axis and any siblings
2082 # that have been set by `fig.align_xlabels()`
-> 2083 bboxes, bboxes2 = self._get_tick_boxes_siblings(renderer=renderer)
2084
2085 x, y = self.label.get_position()
/opt/conda/lib/python3.7/site-packages/matplotlib/axis.py in _get_tick_boxes_siblings(self, renderer)
1879 axis = getattr(ax, f"{axis_name}axis")
1880 ticks_to_draw = axis._update_ticks()
-> 1881 tlb, tlb2 = axis._get_tick_bboxes(ticks_to_draw, renderer)
1882 bboxes.extend(tlb)
1883 bboxes2.extend(tlb2)
/opt/conda/lib/python3.7/site-packages/matplotlib/axis.py in _get_tick_bboxes(self, ticks, renderer)
1084 """Return lists of bboxes for ticks' label1's and label2's."""
1085 return ([tick.label1.get_window_extent(renderer)
-> 1086 for tick in ticks if tick.label1.get_visible()],
1087 [tick.label2.get_window_extent(renderer)
1088 for tick in ticks if tick.label2.get_visible()])
/opt/conda/lib/python3.7/site-packages/matplotlib/axis.py in <listcomp>(.0)
1084 """Return lists of bboxes for ticks' label1's and label2's."""
1085 return ([tick.label1.get_window_extent(renderer)
-> 1086 for tick in ticks if tick.label1.get_visible()],
1087 [tick.label2.get_window_extent(renderer)
1088 for tick in ticks if tick.label2.get_visible()])
/opt/conda/lib/python3.7/site-packages/matplotlib/text.py in get_window_extent(self, renderer, dpi)
908
909 with cbook._setattr_cm(self.figure, dpi=dpi):
--> 910 bbox, info, descent = self._get_layout(self._renderer)
911 x, y = self.get_unitless_position()
912 x, y = self.get_transform().transform((x, y))
/opt/conda/lib/python3.7/site-packages/matplotlib/text.py in _get_layout(self, renderer)
316 if clean_line:
317 w, h, d = renderer.get_text_width_height_descent(
--> 318 clean_line, self._fontproperties, ismath=ismath)
319 else:
320 w = h = d = 0
/opt/conda/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py in get_text_width_height_descent(self, s, prop, ismath)
263 if ismath:
264 ox, oy, width, height, descent, fonts, used_characters = \
--> 265 self.mathtext_parser.parse(s, self.dpi, prop)
266 return width, height, descent
267
/opt/conda/lib/python3.7/site-packages/matplotlib/mathtext.py in parse(self, s, dpi, prop, _force_standard_ps_fonts)
433 # mathtext.fontset rcParams also affect the parse (e.g. by affecting
434 # the glyph metrics).
--> 435 return self._parse_cached(s, dpi, prop, _force_standard_ps_fonts)
436
437 @functools.lru_cache(50)
/opt/conda/lib/python3.7/site-packages/matplotlib/mathtext.py in _parse_cached(self, s, dpi, prop, force_standard_ps_fonts)
454 self.__class__._parser = _mathtext.Parser()
455
--> 456 box = self._parser.parse(s, font_output, fontsize, dpi)
457 font_output.set_canvas_size(box.width, box.height, box.depth)
458 return font_output.get_results(box)
/opt/conda/lib/python3.7/site-packages/matplotlib/_mathtext.py in parse(self, s, fonts_object, fontsize, dpi)
2240 err.line,
2241 " " * (err.column - 1) + "^",
-> 2242 str(err)])) from err
2243 self._state_stack = None
2244 self._em_width_cache = {}
ValueError:
PSYCHOPATHS$#mercer#devos nra#blood$#oligarch$#501c#brexit#bot epshteyn#murdoch#anschutz#SCOTUS*bribes FuxNews#blavatnik#sfeinberg#schwarzman#sackler
^
Expected end of text, found '
(at char 11), (line:1, col:12)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论