You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We generated sentiment scores for each tweet using
411
-
[TextBlob](http://textblob.readthedocs.io/en/dev/), which are stored in the
412
-
`polarity` column. We can plot the mean value for each candidate, along with
413
-
the standard deviation. The standard deviation will tell us how wide the
414
-
variation is between all the tweets, whereas the mean will tell us how the
415
-
average tweet is.
416
-
417
-
In order to do this, we can add 2 Axes to a single Figure, and plot the mean
418
-
of `polarity` in one, and the standard deviation in the other. Because there
419
-
are a lot of text labels in these plots, we’ll need to increase the size of
420
-
the generated figure to match. We can do this with the `figsize` option in the
421
-
`plt.subplots` method.
422
-
423
-
The code below will:
424
-
425
-
* Group tweets by candidate, and compute the mean and standard deviation for each numerical column (including `polarity`).
426
-
* Create a Figure that’s `7` inches by `7` inches, with 2 Axes objects, arranged vertically.
427
-
* Create a bar plot of the standard deviation the first Axes object.
428
-
* Set the tick labels using the [set_xticklabels](http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.set_xticklabels) method, and rotate the labels `45` degrees using the `rotation` argument.
429
-
* Set the title.
430
-
* Create a bar plot of the mean on the second Axes object.
To plot the tweet lengths, we’ll first have to categorize the tweets, then
468
-
figure out how many tweets by each candidate fall into each bin.
455
+
要绘制推特长度,我们首先必须对这些推特进行分类,然后找出关于每个候选人的推特落入到每个箱中的个数。
469
456
470
-
In the code below, we’ll:
457
+
下面的代码中,我们将:
471
458
472
-
*Define a function to mark a tweet as `short` if it’s less than `100` characters, `medium` if it’s `100` to `135` characters, and `long` if it’s over `135` characters.
473
-
*Use `apply` to generate a new column `tweet_length`.
474
-
*Figure out how many tweets by each candidate fall into each group.
Now that we have the data we want to plot, we can generate our side by side
496
-
bar plot. We’ll use the `bar` method to plot the tweet lengths for each
497
-
candidate on the same axis. However, we’ll use an offset to shift the bars to
498
-
the right for the second and third candidates we plot. This will give us three
499
-
category areas, `short`, `medium`, and `long`, with one bar for each candidate
500
-
in each area.
501
-
502
-
In the code below, we:
503
-
504
-
* Create a Figure and a single Axes object.
505
-
* Define the `width` for each bar, `.5`.
506
-
* Generate a sequence of values, `x`, that is `0`, `2`, `4`. Each value is the start of a category, such as `short`, `medium`, and `long`. We put a distance of `2` between each category so we have space for multiple bars.
507
-
* Plot `clinton` tweets on the Axes object, with the bars at the positions defined by `x`.
508
-
* Plot `sanders` tweets on the Axes object, but add `width` to `x` to move the bars to the right.
509
-
* Plot `trump` tweets on the Axes object, but add `width * 2` to `x` to move the bars to the far right.
510
-
* Set the axis labels and title.
511
-
* Use `set_xticks` to move the tick labels to the center of each category area.
0 commit comments