{"assignment":{"_schema_version":2,"course_id":37,"date_created":"2022-09-17T16:32:45.482196+00:00","date_modified":"2023-09-10T14:20:24.222988+00:00","extra_instructor_files":"","extra_starting_files":"","forked_id":null,"forked_version":null,"hidden":false,"id":1245,"instructions":"## Visualizations\n\n![A picture of a researcher pointing to a Histogram and saying, \"As you can see, most students do very well!\"](bakery_advanced_plotting_visualization.png)\n\nWhen you have a large amount of data, you can create visualizations to get a more intuitive understanding.\nThese visualizations, known as plots or graphs, take advantage of the fact that humans are good at processing pictures.\nMaking visualizations in Python is surprisingly easy, thanks to its many external libraries.\n\n## MatPlotLib\n\n```python import-matplotlib\nimport matplotlib.pyplot as plt\n```\n\nThe most popular library for plotting in Python is named MatPlotLib.\nAs a very large package, we only need to import only a certain submodule within it.\nBy convention and for convenience, we rename that module to be `plt`.\nYou will see this line of code all the time at the start of programs in Python that make plots, so you should get used to typing it.\n\n## Types of Plots\n\n![Three plots are shown: a histogram, a line plot, and a scatter plot](bakery_advanced_plotting_graph_types.png)\n\nThere are 3 kinds of plots that we'll learn about.\nFirst are histograms, which show a distribution of numbers.\nThen there are line plots, that show a trend over time.\nFinally, there are scatter plots, which show the relationship between two corresponding lists of numbers.\nIn fact, each of the plots shown takes in lists of integers or floats.\nMatPlotLib actually has many other kinds of plots.\nTo learn how to make other kinds of plots, you can refer to the documentation.\n\n## Histograms\n\n![Code on the left constructs a histogram, shown on the right.](bakery_advanced_plotting_histograms.png)\n\nSome people find histograms confusing, but once mastered they are one of the most generally useful and fundamental kinds of graphs.\nHistograms allow us to see the distribution of the data.\nIs the data clustered around a single value, or spread out?\nAre certain values more popular than others?\nHistograms tell us all this and more.\nThey should not be confused with Bar Charts, which are a more generalized kind of graph that tell you the frequency of categories of data.\nInstead, histograms take a list of numbers and group them into bins, or ranges of numbers.\nFor example, if we have the list of numbers shown, we could group them into bins of size 10.\nEach tick on the y-axis means another number in that bin.\n\n## Line Plots\n\n![Code on left constructs a line plot, shown on the right](bakery_advanced_plotting_lines.png)\n\nLine plots are another simple kind of graph, and can be used to show a trend over time or some other factor.\nBe careful when creating line plots, since people often use them when a Histogram would be more appropriate.\nThe major difference is that a line plot requires the data to have some kind of meaningful ordering, while the histogram does not.\nYou have to think critically about your data and what the data represents, before you know whether you need a histogram or a line plot.\nFor instance, you could make a line plot of the grades you've earned over the course of the semester if they are ordered by date.\nBut if the scores were not ordered, then a histogram would be more appropriate.\nWhen in doubt, always start with a histogram.\n\n## Scatter Plots\n\n![Code on the left constructs a line plot, shown on the right](bakery_advanced_plotting_scatter.png)\n\nWhen you have two lists of data that you want to match up, a Scatter Plot is the graph to use.\nFor example, the graph shown here matches students' grade on a first exam with their grade on a second exam.\nA scatter plot is used to find relationships between the two lists of numbers.\nOn its own, a scatter plot is not usually conclusive evidence, so you often have to follow-up with statistical tests.\nHowever, a scatter plot can help you explore the data and learn whether there might be a lurking correlation.\n\n## Labelling\n\n![The code on the left produces the line plot with labels on the right](bakery_advanced_plotting_labels.png)\n\nProfessional data scientists always label their graphs.\nMatPlotLib makes it easy to label the X, Y, and title of the graph.\nThe relevant functions are xlabel, ylabel, and title, each of which consumes a string.\nYou should *always* label your graphs.\n\n## Show\n\n```python use-show\nimport matplotlib.pyplot as plt\n\ndata = [0, 5, 7, 35, 35, 51, 53, 57,\n        58, 61, 64, 82, 84, 84, 88]\n\nplt.hist(data)\nplt.show()\n\nplt.plot(data)\nplt.show()\n```\n\nA trick about creating plots is that you need to call the `show` function afterwards.\nIf you do not call the `show` function, no graph will appear.\nThis can be a common source of confusion, especially since the `show` function takes no arguments.\nRemember, without the parentheses after the `show`, you are not actually calling a function.\nWhen you run this code, notice that each time we call `show`, a new graph appears.\n\n## Multiple Plots on One Graph\n\n![The code shown on the left produces the two line plots on the right with a legend](bakery_advanced_plotting_legend.png)\n\nIf you create multiple plots before calling the `show` function, each plot will be combined on the same canvas.\nGenerally, you do not want to combine graphs of different types, like histograms and line plots.\nBut overlapping line plots with each other is often helpful in comparing trends visually.\nTo make identifying which line is which, you should add a legend to the graph with the `legend` function.\nThis also requires the use of the `label` keyword argument, to give a name to each of the line plots.\n\n\n## Plotting Lists of Dataclasses\n\n```python map-dataclasses\nfrom dataclasses import dataclass\nimport matplotlib.pyplot as plt\n\n@dataclass\nclass Student:\n    name: str\n    score: int\n\nstudents = [Student(\"Ada\", 96), Student(\"Babbage\", 87),\n            Student(\"Capn\", 90), Student(\"Domino\", 50),\n            Student(\"Ellie\", 100)]\nscores = []\nfor student in students:\n    scores.append(student.score)\nplt.hist(scores)\nplt.show()\n```\n\nSo far, all of our examples have used simple list of literal primitive values.\nHowever, in practice, usually you will be plotting data taken from a sophisticated data source.\nOne of the most common situations is a list of dataclasses, which cannot be plotted directly.\nInstead, you must map the list of dataclasses into a list of integers or floats. \n\n\n## Advanced Features\n\n![A series of images demonstrating advanced MatPlotLib features are shown, such as modeling functions, contour graphs, and heat maps.](bakery_advanced_plotting_advanced.png)\n\nMatPlotLib is an extremely sophisticated library with many, many advanced features.\nIt is very easy to make legends, adjust colors, more complicated graphs, and much more.\nRefer to the MatPlotLib documentation and look up examples of how to do more with MatPlotLib.","ip_ranges":"","name":"10B1) Plotting Reading","on_change":"","on_eval":"","on_run":"","owner_id":1,"owner_id__email":"acbart@udel.edu","points":0,"public":true,"reviewed":false,"sample_submissions":[],"settings":"{\n  \"youtube\": {\n    \"Bart\": \"CJ3nMrvf2bo\",\n    \"Amy\": \"H_l4gidEP24\"\n  },\n  \"video\": {\n    \"Bart\": \"https://blockpy.cis.udel.edu/videos/bakery_advanced_plotting-Bart.mp4\",\n    \"Amy\": \"https://blockpy.cis.udel.edu/videos/bakery_advanced_plotting-Amy.mp4\"\n  },\n  \"small_layout\": true,\n  \"header\": \"Plotting\"\n}","starting_code":"","subordinate":true,"tags":[],"type":"reading","url":"bakery_advanced_plotting_read","version":9},"ip":"216.73.216.157","submission":{"_schema_version":3,"assignment_id":1245,"assignment_version":9,"attempts":0,"code":"","correct":false,"course_id":37,"date_created":"2026-05-20T14:01:57.988487+00:00","date_due":"","date_graded":"","date_locked":"","date_modified":"2026-05-20T14:01:57.988487+00:00","date_started":"","date_submitted":"","endpoint":"","extra_files":"","feedback":"","grading_status":"NotReady","id":2036938,"score":0.0,"submission_status":"Started","time_limit":"","url":"submission_url-8c78229e-f2d8-41ab-b8de-2b51c877faa6","user_id":2044668,"user_id__email":"","version":0},"success":true}
