{
  "source_file": "0930",
  "source_platform": "Hashnode",
  "author": "Wesley Kambale",
  "title_listed": "Data Visualization with Matplotlib and Seaborn",
  "keywords": "",
  "domain": "数据处理",
  "content_type": "深度技术解析",
  "is_paid_flag": "否",
  "url": "https://kambale.dev/matplotlib-and-seaborn",
  "publish_date": "2023-05-19",
  "corpus_stage": "",
  "notes": "",
  "id": "04a094a8e9ec",
  "collected_at": "2026-10-02T14:11:37",
  "title": "Python for Data Visualization: Matplotlib and Seaborn",
  "body_markdown": "Data Visualization with Matplotlib and Seaborn\nA beginner guide to data visualization in Python using Matplotlib and Seaborn libraries\nUpdated\nJuly 18, 2023\n•\n7\nmin read\n•\nView as Markdown\nW\nWesley Kambale\nI'm a Machine Learning Engineer passionate about building production-ready ML systems for the African market.\nWith experience in TensorFlow, Keras, and Python-based workflows, I help teams bridge the gap between machine learning research and real-world deployment—especially on resource-constrained devices.\nI'm also a Google Developer Expert in AI. I regularly speak at tech conferences including PyCon Africa, DevFest Kampala, DevFest Nairobi and more and also write technical articles on AI/ML here.\nPart of series\nMachine Learning\nIntroduction\nData visualization is a crucial step in the data analysis process. It allows us to visually explore and communicate data patterns, trends, and relationships effectively. Matplotlib and Seaborn are two popular Python libraries that provide powerful tools for creating a wide range of static, animated, and interactive visualizations.\nMatplotlib\nMatplotlib is a versatile plotting library that offers a high degree of control over plot customization. It provides a wide variety of plot types, including line plots, scatter plots, bar plots, histograms, and more. Matplotlib can be used in interactive environments like Jupyter or Google Colab notebooks.\nSeaborn\nSeaborn is a higher-level data visualization library built on top of Matplotlib. It simplifies the process of creating attractive statistical graphics by providing high-level functions for common plot types. Seaborn also offers themes and color palettes that make plots visually appealing with minimal customization.\nInstallation\nBefore we start, let's make sure Matplotlib and Seaborn are installed. You can install them using pip, the Python package installer, by running the following commands in your terminal:\npip install matplotlib\npip install seaborn\nMake sure you have an up-to-date version of both libraries. Now that we have everything set up, let's dive into the tutorial!\nVisualization with Matplotlib\nLine Plot\nA line plot is a basic plot type that displays data points connected by lines. It is useful for visualizing trends and changes over time or any continuous variable. Here's an example of creating a simple line plot using Matplotlib:\nimport\nmatplotlib.pyplot\nas\nplt\n# Sample data\nlistOne = [\n1\n,\n2\n,\n3\n,\n4\n,\n5\n]\nlistTwo = [\n2\n,\n4\n,\n6\n,\n8\n,\n10\n]\n# Create a line plot\nplt.plot(listOne, listTwo)\n# Add labels and title\nplt.xlabel(\n'X-axis'\n)\nplt.ylabel(\n'Y-axis'\n)\nplt.title(\n'Line Plot of Two Lists'\n)\n# Show the plot\nplt.show()\nWe import\nmatplotlib.pyplot\nas\nplt\n, create lists\nlistOne\nand\nlistTwo\nrepresenting the data points, and then use the\nplot()\nfunction to create the line plot. We add labels to the x-axis and y-axis and provide a title for the plot. Finally, we use\nshow()\nto display the plot.\nScatter Plot\nA scatter plot displays individual data points as markers on a two-dimensional plane. It is useful for examining the relationship between two continuous variables. Let's create a scatter plot using Matplotlib:\nimport\nmatplotlib.pyplot\nas\nplt\n# Sample data\nlistOne = [\n1\n,\n2\n,\n3\n,\n4\n,\n5\n]\nlistTwo = [\n2\n,\n4\n,\n6\n,\n8\n,\n10\n]\n# Create a scatter plot\nplt.scatter(listOne, listTwo)\n# Add labels and title\nplt.xlabel(\n'X-axis'\n)\nplt.ylabel(\n'Y-axis'\n)\nplt.title(\n'Scatter Plot of Two Lists'\n)\n# Show the plot\nplt.show()\nWe use the\nscatter()\nfunction to create a scatter plot. The rest of the code is similar to the line plot example.\nBar Plot\nA bar plot represents data as rectangular bars, with the length of each bar proportional to the value it represents. Bar plots are commonly used to compare categorical data or to show the distribution of a continuous variable across categories. Here's an example of creating a bar plot using Matplotlib:\nimport\nmatplotlib.pyplot\nas\nplt\n# Sample data\ncategories = [\n'A'\n,\n'B'\n,\n'C'\n,\n'D'\n]\nvalues = [\n10\n,\n15\n,\n7\n,\n12\n]\n# Create a bar plot\nplt.bar(categories, values)\n# Add labels and title\nplt.xlabel(\n'Categories'\n)\nplt.ylabel(\n'Values'\n)\nplt.title(\n'Bar Plot of Categories and Values'\n)\n# Show the plot\nplt.show()\nWe use the\nbar()\nfunction to create a bar plot. The\ncategories\nlist represents the x-axis categories, and the\nvalues\nlist represents the height of each bar. We add labels to the x-axis and y-axis and provide a title for the plot. Finally, we use\nshow()\nto display the plot.\nHistogram\nA histogram is used to visualize the distribution of a single continuous variable. It divides the range of values into intervals called bins and displays the frequency or proportion of values falling into each bin. Here's an example of creating a histogram using Matplotlib:\nimport\nmatplotlib.pyplot\nas\nplt\nimport\nnumpy\nas\nnp\n# Generate random data\nnp.random.seed(\n42\n)\ndata = np.random.normal(\n0\n,\n1\n,\n1000\n)\n# Create a histogram\nplt.hist(data, bins=\n30\n)\n# Add labels and title\nplt.xlabel(\n'Values'\n)\nplt.ylabel(\n'Frequency'\n)\nplt.title(\n'Histogram Showing Values against Frequency'\n)\n# Show the plot\nplt.show()\nHere we use the\nhist()\nfunction to create a histogram. The\ndata\nvariable contains random values generated using NumPy's\nrandom.normal()\nfunction. We specify the number of bins using the\nbins\nparameter. We add labels to the x-axis and y-axis and provide a title for the plot. Finally, we use\nshow()\nto display the plot.\nVisualization with Seaborn\nBox Plot\nA box plot, also known as a box-and-whisker plot, is used to display the distribution of a continuous variable across different categories or groups. It shows the median, quartiles, and any potential outliers in the data. Let's create a box plot using Seaborn:\nimport\nseaborn\nas\nsns\nimport\nnumpy\nas\nnp\nimport\npandas\nas\npd\n# Generate random data\nnp.random.seed(\n42\n)\ndataOne = np.random.normal(\n0\n,\n1\n,\n100\n)\ndataTwo = np.random.normal(\n2\n,\n1\n,\n100\n)\ndataThree = np.random.normal(\n1\n,\n2\n,\n100\n)\n# Combine the data into a DataFrame\ndata = np.concatenate([dataOne, dataTwo, dataThree])\ncategories = np.repeat([\n'A'\n,\n'B'\n,\n'C'\n],\n100\n)\ndf = pd.DataFrame({\n'Category'\n: categories,\n'Data'\n: data})\n# Create a box plot\nsns.boxplot(x=\n'Category'\n, y=\n'Data'\n, data=df)\n# Add title\nplt.title(\n'Box Plot of Data against Category'\n)\n# Show the plot\nplt.show()\nWe use the\nboxplot()\nfunction from Seaborn to create a box plot. We create three sets of random data,\ndataOne\n,\ndataTwo\n, and\ndataThree\n, representing different categories. We then combine the data into a DataFrame,\ndf\n, with the 'Category' and 'Data' columns. Finally, we use\nboxplot()\nby specifying the x-axis as 'Category', the y-axis as 'Data', and the DataFrame\ndf\n. We add a title to the plot and display it using\nshow()\n.\nHeatmap\nA heatmap is a graphical representation of data where the values in a matrix are represented as colors. It is useful for visualizing the relationships or patterns in large datasets. Let's create a heatmap using Seaborn:\nimport\nseaborn\nas\nsns\nimport\nnumpy\nas\nnp\n# Generate random correlation data\nnp.random.seed(\n42\n)\ndata = np.random.rand(\n10\n,\n10\n)\ncorr = np.corrcoef(data)\n# Create a heatmap\nsns.heatmap(corr, annot=\nTrue\n, cmap=\n'coolwarm'\n)\n# Add title\nplt.title(\n'Heatmap of the Data and the Correlation'\n)\n# Show the plot\nplt.show()\nWe use the\nheatmap()\nfunction from Seaborn to create a heatmap. We generate random data\ndata\nand calculate the correlation matrix\ncorr\nusing NumPy's\ncorrcoef()\nfunction. We then pass\ncorr\nto\nheatmap()\n, set\nannot=True\nto display the correlation values on the heatmap, and specify the color map as\n'coolwarm'\n. We add a title to the plot and display it using\nshow()\n.\nAdditional Customizations\nBoth Matplotlib and Seaborn offer a wide range of customization options to enhance your plots. Here are a few additional customization techniques:\nAxis Limits and Ticks\nYou can set custom axis limits using\nxlim()\nand\nylim()\nfunctions in Matplotlib:\nplt.xlim(\n0\n,\n10\n)\nplt.ylim(\n0\n,\n20\n)\nYou can also customize the ticks on the axis using\nxticks()\nand\nyticks()\n:\nplt.xticks([\n0\n,\n1\n,\n2\n,\n3\n,\n4\n,\n5\n])\nplt.yticks([\n0\n,\n5\n,\n10\n,\n15\n,\n20\n])\nLegends\nYou can add legends to your plots to provide additional information about the data using\nlegend()\n:\nplt.plot(x, y, label=\n'Line 1'\n)\nplt.plot(x, z, label=\n'Line 2'\n)\nplt.legend()\nColor Maps\nBoth Matplotlib and Seaborn provide a variety of color maps for different purposes. You can specify the color map using the\ncmap\nparameter. For example, in a scatter plot:\nplt.scatter(x, y, cmap=\n'viridis'\n)\nStyling with Seaborn\nSeaborn provides additional styling options using its built-in themes. You can set a different theme using\nset_theme()\n. For example:\nsns.set_theme(style=\n'whitegrid'\n)\nSeaborn also provides various color palettes that you can use to customize the colors in your plots. You can set a different color palette using\nset_palette()\n. For example:\nsns.set_palette(\n'Set2'\n)\nConclusion\nWe have covered the basics of data visualization using Matplotlib and Seaborn. We explored various plot types, including line plots, scatter plots, bar plots, histograms, box plots, and heatmaps. We also discussed additional customization techniques to enhance your plots. With these tools and techniques, you can create visually appealing and informative visualizations to explore and communicate your data effectively.\nResources\nGoogle Colab Notebook\n#\nmatplotlib\n#\nseaborn\n#\ndata-visualisation-1\n#\ndata-analysis",
  "metadata": {
    "title": "Python for Data Visualization: Matplotlib and Seaborn",
    "description": "A beginner guide to data visualization in Python using Matplotlib and Seaborn libraries",
    "keywords": null,
    "author": null,
    "og:title": "Python for Data Visualization: Matplotlib and Seaborn",
    "og:description": "A beginner guide to data visualization in Python using Matplotlib and Seaborn libraries",
    "og:url": "https://kambale.dev/matplotlib-and-seaborn",
    "og:site_name": "Wesley Kambale",
    "og:image": "https://cdn.hashnode.com/res/hashnode/image/upload/v1689691031785/a5f385d1-d0d4-40e2-9c4c-76af2171c028.png",
    "og:image:width": "1200",
    "og:image:height": "630",
    "og:type": "article",
    "twitter:card": "summary_large_image",
    "twitter:creator": "https://twitter.com/WesKambale",
    "twitter:title": "Python for Data Visualization: Matplotlib and Seaborn",
    "twitter:description": "A beginner guide to data visualization in Python using Matplotlib and Seaborn libraries",
    "twitter:image": "https://cdn.hashnode.com/res/hashnode/image/upload/v1689691031785/a5f385d1-d0d4-40e2-9c4c-76af2171c028.png",
    "article:published_time": "2023-05-19T22:08:32.725Z",
    "article:modified_time": "2023-07-18T16:45:11.164Z",
    "article:author": "https://kambale.dev/@kambale"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/04a094a8e9ec/code/body-000.txt",
      "chars": 43,
      "language": "text"
    },
    {
      "file": "attachments/04a094a8e9ec/code/body-001.txt",
      "chars": 279,
      "language": "text"
    },
    {
      "file": "attachments/04a094a8e9ec/code/body-002.txt",
      "chars": 288,
      "language": "text"
    },
    {
      "file": "attachments/04a094a8e9ec/code/body-003.txt",
      "chars": 300,
      "language": "text"
    },
    {
      "file": "attachments/04a094a8e9ec/code/body-004.txt",
      "chars": 329,
      "language": "text"
    },
    {
      "file": "attachments/04a094a8e9ec/code/body-005.txt",
      "chars": 567,
      "language": "text"
    },
    {
      "file": "attachments/04a094a8e9ec/code/body-006.txt",
      "chars": 312,
      "language": "text"
    },
    {
      "file": "attachments/04a094a8e9ec/code/body-007.txt",
      "chars": 32,
      "language": "text"
    },
    {
      "file": "attachments/04a094a8e9ec/code/body-008.txt",
      "chars": 62,
      "language": "text"
    },
    {
      "file": "attachments/04a094a8e9ec/code/body-009.txt",
      "chars": 75,
      "language": "text"
    },
    {
      "file": "attachments/04a094a8e9ec/code/body-010.txt",
      "chars": 34,
      "language": "text"
    },
    {
      "file": "attachments/04a094a8e9ec/code/body-011.txt",
      "chars": 33,
      "language": "text"
    },
    {
      "file": "attachments/04a094a8e9ec/code/body-012.txt",
      "chars": 24,
      "language": "text"
    }
  ],
  "images": [
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1689691031785/a5f385d1-d0d4-40e2-9c4c-76af2171c028.png",
      "bytes": 98212,
      "reused": true,
      "local_path": "attachments/04a094a8e9ec/images/000.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1689691031785/a5f385d1-d0d4-40e2-9c4c-76af2171c028.png",
      "alt": "Data Visualization with Matplotlib and Seaborn",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1684531547411/fb8d6156-f8a6-47d8-8751-303e09a18d7a.png",
      "bytes": 14676,
      "reused": true,
      "local_path": "attachments/04a094a8e9ec/images/002.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1684531547411/fb8d6156-f8a6-47d8-8751-303e09a18d7a.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1684531647041/de7141ad-484d-4c64-9024-7fe6adbc4a31.png",
      "bytes": 9694,
      "reused": true,
      "local_path": "attachments/04a094a8e9ec/images/003.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1684531647041/de7141ad-484d-4c64-9024-7fe6adbc4a31.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1684531830948/752f03ef-d0e2-43c6-a48c-2cc58cac273a.png",
      "bytes": 9905,
      "reused": true,
      "local_path": "attachments/04a094a8e9ec/images/004.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1684531830948/752f03ef-d0e2-43c6-a48c-2cc58cac273a.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1684531975383/8b18e056-09e8-46c2-80ff-8514c88ace96.png",
      "bytes": 11967,
      "reused": true,
      "local_path": "attachments/04a094a8e9ec/images/005.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1684531975383/8b18e056-09e8-46c2-80ff-8514c88ace96.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1684532463657/5c9bde17-abfe-478e-95fb-9fe775acbdd2.png",
      "bytes": 11049,
      "reused": true,
      "local_path": "attachments/04a094a8e9ec/images/006.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1684532463657/5c9bde17-abfe-478e-95fb-9fe775acbdd2.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1684533165976/a19ff9a3-81f0-4d62-ba97-5e6c7dbcfe2b.png",
      "bytes": 24034,
      "reused": true,
      "local_path": "attachments/04a094a8e9ec/images/007.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1684533165976/a19ff9a3-81f0-4d62-ba97-5e6c7dbcfe2b.png",
      "alt": "",
      "downloaded": true
    }
  ],
  "audio": [],
  "videos": [],
  "links": [
    {
      "href": "https://kambale.dev/matplotlib-and-seaborn.md",
      "label": "View as Markdown",
      "type": "a"
    },
    {
      "href": "https://hashnode.com/@kambale",
      "label": "W",
      "type": "a"
    },
    {
      "href": "https://twitter.com/WesKambale",
      "label": "",
      "type": "a"
    },
    {
      "href": "https://linkedin.com/in/WesKambale",
      "label": "",
      "type": "a"
    },
    {
      "href": "https://kambale.dev/series/ml-series",
      "label": "Part of series Machine Learning",
      "type": "a"
    },
    {
      "href": "https://colab.research.google.com/drive/1sn6sL3O8ocor8axNIWdGALnKyd2sIeCS?usp=sharing",
      "label": "Google Colab Notebook",
      "type": "a"
    },
    {
      "href": "https://kambale.dev/tag/matplotlib",
      "label": "# matplotlib",
      "type": "a"
    },
    {
      "href": "https://kambale.dev/tag/seaborn",
      "label": "# seaborn",
      "type": "a"
    },
    {
      "href": "https://kambale.dev/tag/data-visualisation-1",
      "label": "# data-visualisation-1",
      "type": "a"
    },
    {
      "href": "https://kambale.dev/tag/data-analysis",
      "label": "# data-analysis",
      "type": "a"
    }
  ],
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:50:52Z",
  "source_url": "https://kambale.dev/matplotlib-and-seaborn",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 9433,
  "expected_body_images": 7,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/04a094a8e9ec/pages/source.html",
  "local_html": "attachments/04a094a8e9ec/pages/article.html",
  "body_base_path": "attachments/04a094a8e9ec/pages/",
  "body_markdown_path": "attachments/04a094a8e9ec/pages/article.md"
}