官术网_书友最值得收藏!

There's more...

We have seen a few ways to explore data, both statistically and visually. There are quite a few libraries in Python that you can use to visualize your data. One of the most widely used of these is ggplot. Before we look at a few commands, let's learn how ggplot works.

There are seven layers of grammatical elements in ggplot, out of which, first three layers are mandatory:

  • Data
  • Aesthetics
  • Geometrics
  • Facets
  • Statistics
  • Coordinates
  • Theme

You will often start by providing a dataset to ggplot(). Then, you provide an aesthetic mapping with the aes() function to map the variables to the and y axes. With aes(), you can also set the color, size, shape, and position of the charts. You then add the type of geometric shape you want with functions such as geom_point() or geom_histogram(). You can also add various options, such as plotting statistical summaries, faceting, visual themes, and coordinate systems.

The following code is an extension to what we have used already in this chapter, so we will directly delve into the ggplot code here:

f = pd.melt(housepricesdata, id_vars=['SalePrice'],value_vars= numerical_features[0:9])
ggplot(f,aes('value', 'SalePrice')) + geom_point(color='orange') + facet_wrap('variable',scales='free')

The preceding code generates the following chart:

Similarly, in order to view the density plot for the numerical variables, we can execute the following code:

f_1 = pd.melt(housepricesdata, value_vars=numerical_features[0:9])
ggplot(f_1, aes('value')) + geom_density(color="red") + facet_wrap('variable',scales='free')

The plot shows us the univariate density plot for each of our numerical variables. The geom_density() computes and draws a kernel density estimate, which is a smoothed version of the histogram:

主站蜘蛛池模板: 井冈山市| 和静县| 瓦房店市| 北流市| 新密市| 嘉定区| 静乐县| 旅游| 吉安市| 和田市| 万年县| 罗山县| 腾冲县| 托克逊县| 奉贤区| 虎林市| 石狮市| 公安县| 惠东县| 平顶山市| 嵊州市| 石楼县| 晋城| 抚宁县| 基隆市| 永寿县| 梅州市| 青田县| 通渭县| 墨脱县| 上栗县| 梁河县| 重庆市| 徐闻县| 高阳县| 应城市| 平乡县| 湖南省| 天长市| 清原| 徐闻县|