辅导data程序、Python设计编程调试
- 首页 >> Matlab编程 Using your preferred code editor (e.g. VSCode), in a Python script called "CR2.py", write code to answer the following problem.
Problem
Write a function plot_columns() which takes 4 input arguments:
a string "fig_type", which is one of the two values: "single" or "subplots",
a 2D Numpy array "x" with 𝑟r rows and 𝑐c columns, where 𝑟,𝑐>0r,c>0 and 𝑐≤10c≤10,
a 2D Numpy array "y" with 𝑟r rows and 𝑐c columns, where 𝑟,𝑐>0r,c>0 and 𝑐≤10c≤10,
a list "plot_type" of length 𝑐c, whose elements are strings taking one of the two values: "line" or "scatter".
Your function should create a figure, and in it produce and show a plot of each column of "x" and "y", with the x-coordinates stored in a given column of "x" corresponding to the y-coordinates stored in the same column of "y". This means that you will produce 𝑐c plots in total -- one per pair of columns in "x" and "y".
The input argument "fig_type" determines how to draw the plots:
if "fig_type" is "single", you should produce one set of axes, draw all the plots together in the same axes, and differentiate them e.g. by line or marker colour or style.
if "fig_type" is "subplots", you should produce 𝑐c different sets of axes (in the same figure), so that each plot is drawn in a different subplot. Choose how to set up your subplots so that all plots are sufficiently large and readable.
The 𝑖ith string in the list "plot_type" determines whether the 𝑖ith plot should be a line plot (line connecting the points) or a scatter plot (individual markers only for each point, no lines between points).
Your function shouldn't return anything, just display the plot. All plots should be clearly and appropriately labelled (you can use the label e.g. "Plot i" to label the 𝑖ith plot).
You can assume that the input arguments will always be of the form outlined above, i.e. you don't need to check, for instance, that "plot_type" has the correct length -- you can assume that it always will. (That being said, if you do want to check input arguments, you won't be penalised for doing so.)
Here are a couple of examples:
If x and y are Numpy arrays with 2 columns each, plot_columns("single", x, y, ["line", "scatter"]) should produce 2 overlapping plots, in the same axes; the first plot should be a line plot, the second plot should be a scatter plot. (You can label them "Plot 1" and "Plot 2".)
If x and y are Numpy arrays with 4 columns each, plot_columns("subplots", x, y, ["scatter", "scatter", "line", "scatter"]) should produce 4 plots, each in a different subplot. The first, second, and fourth plots should be scatter plots, the third plot should be a line plot.
Then, after the function definition, write a few tests to check that your function is working. For this task, the expected result should be relatively self-evident. You could use Numpy's "random" module to generate randomised data for your plots, you could evaluate different functions and plot them, etc. Try to think of a few fairly trivial examples which make it very quick to see whether your function works or not.
You should include your test code in the script CR2.py, after the function definition. Comment out all your tests but one, so that the assessors can look at your plots one at a time (and not be bombarded by 10 different figure windows appearing at once!).
Problem
Write a function plot_columns() which takes 4 input arguments:
a string "fig_type", which is one of the two values: "single" or "subplots",
a 2D Numpy array "x" with 𝑟r rows and 𝑐c columns, where 𝑟,𝑐>0r,c>0 and 𝑐≤10c≤10,
a 2D Numpy array "y" with 𝑟r rows and 𝑐c columns, where 𝑟,𝑐>0r,c>0 and 𝑐≤10c≤10,
a list "plot_type" of length 𝑐c, whose elements are strings taking one of the two values: "line" or "scatter".
Your function should create a figure, and in it produce and show a plot of each column of "x" and "y", with the x-coordinates stored in a given column of "x" corresponding to the y-coordinates stored in the same column of "y". This means that you will produce 𝑐c plots in total -- one per pair of columns in "x" and "y".
The input argument "fig_type" determines how to draw the plots:
if "fig_type" is "single", you should produce one set of axes, draw all the plots together in the same axes, and differentiate them e.g. by line or marker colour or style.
if "fig_type" is "subplots", you should produce 𝑐c different sets of axes (in the same figure), so that each plot is drawn in a different subplot. Choose how to set up your subplots so that all plots are sufficiently large and readable.
The 𝑖ith string in the list "plot_type" determines whether the 𝑖ith plot should be a line plot (line connecting the points) or a scatter plot (individual markers only for each point, no lines between points).
Your function shouldn't return anything, just display the plot. All plots should be clearly and appropriately labelled (you can use the label e.g. "Plot i" to label the 𝑖ith plot).
You can assume that the input arguments will always be of the form outlined above, i.e. you don't need to check, for instance, that "plot_type" has the correct length -- you can assume that it always will. (That being said, if you do want to check input arguments, you won't be penalised for doing so.)
Here are a couple of examples:
If x and y are Numpy arrays with 2 columns each, plot_columns("single", x, y, ["line", "scatter"]) should produce 2 overlapping plots, in the same axes; the first plot should be a line plot, the second plot should be a scatter plot. (You can label them "Plot 1" and "Plot 2".)
If x and y are Numpy arrays with 4 columns each, plot_columns("subplots", x, y, ["scatter", "scatter", "line", "scatter"]) should produce 4 plots, each in a different subplot. The first, second, and fourth plots should be scatter plots, the third plot should be a line plot.
Then, after the function definition, write a few tests to check that your function is working. For this task, the expected result should be relatively self-evident. You could use Numpy's "random" module to generate randomised data for your plots, you could evaluate different functions and plot them, etc. Try to think of a few fairly trivial examples which make it very quick to see whether your function works or not.
You should include your test code in the script CR2.py, after the function definition. Comment out all your tests but one, so that the assessors can look at your plots one at a time (and not be bombarded by 10 different figure windows appearing at once!).