Python Forum

Full Version: Exercise about Matplotlib
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is just an Exercise and I did it but I have some errors. I attached my file. Could you help me, please?

Write a Python program through the following steps:
1. Create the variables x and y like this:

import numpy as np

# Create 100 evenly-spaced points between -4 and 10
x = np.linspace(-4,10,100)
y = -0.5*x**2 + 2*x - 3
2. Draw the curve of y versus x . The curve should be in red color, with a line width of 2, and in dashed-line style.
3. Create two array of random numbers using np.random package:
data_rnd_1 : shape=100-by-2

data_rnd_2 : shape=120-by-2
4. Create a figure of size 6x9 with two subplots, top and bottom.
5. Consider the rows of data_rnd_1 and data_rnd_2 as two-dimensional points, and daw the scatter plot of them (first column versus the second column) in the first and the second subplot, respectively. The markers of the subplots should be "*" and ".", respectively. The colors of the data points in the subplots should be green and red, respectively.
6. Add labels for both x and y axes of the subplots as "dimension 1" and "dimension 2", respectively.
7. Show the plot on the screen.

import numpy as np
x = np.linspace(-5, 5, 100)
y = 4 * (x**3) + 2 * (x**2) + 5 * x

import matplotlib.pyplot as plt

plt.plot(x, y, color='red', linestyle='--', linewidth=2)
plt.show()
Output:
import numpy as np

rand_int_1 = np.random.randn(100,2)  
print("First array", rand_int_1)

rand_int_2 = np.random.randn(120,2)  
print("Second array", rand_int_2)
Output:
First array [[ 1.49640185 -0.66136227] [ 1.60592569 -1.00050129] [ 0.3692072 -0.65355689] [ 1.17201832 -0.64600418] [-0.0502125 -0.52215024] [-2.00133328 0.37088889] [-1.35206998 -0.82313078] [-0.80944694 0.77050384] [-1.45875423 0.88411917] [ 0.41081153 -0.37732382] [ 1.43853755 0.25879336] [-0.16880245 0.65282008] [ 0.83117115 1.45340928] [ 0.748778 0.45377505] [ 0.58926665 0.25232723] [ 0.53539355 0.41047299] [ 0.56356845 -0.07005123] [-1.49576535 -0.21530849] [ 0.11826929 0.04476837] [-0.99748351 0.08577806] [ 1.74663997 -0.91651593] [-0.08709894 -0.54249125] [ 1.51059814 -0.7595974 ] [ 0.44316279 -0.25325591] [ 0.59857887 0.76817626] [ 0.82461033 1.49741473] [ 0.84859489 -0.34372826] [ 0.49569524 0.23631009] [ 0.63617553 0.37383413] [-0.70796829 -1.35339274] [ 0.4191391 1.08997109] [-1.52265186 -0.69733364] [-1.43614787 -1.28475393] [-1.7990606 -0.96242155] [-1.23909522 -0.85365844] [ 0.97573238 -0.25087961] [ 0.07665907 1.59519797] [ 0.48247256 0.33283678] [-1.97240187 1.60911192] [ 0.11598151 -0.66755312] [-0.42564187 0.94075559] [ 0.28498309 0.43170389] [-1.27016193 1.61724424] [-0.85347036 0.66329169] [-0.17967041 0.52705415] [-1.24546743 -0.4993504 ] [-2.27932601 -0.5873414 ] [ 2.05634685 0.73010786] [ 0.26655388 -0.57670283] [ 1.99989822 0.03111458] [ 0.24318088 -0.84504788] [-2.30511512 -1.4931346 ] [ 0.29199263 -1.08535811] [-2.53109714 0.43907014] [ 0.16005339 1.08404753] [-0.6402196 0.52055492] [-0.65396027 -0.00411722] [-0.67409229 0.76800164] [ 1.33194302 -0.38805457] [ 0.6037552 0.33065125] [-0.44111077 -1.17897769] [-1.3827073 -0.5541841 ] [-0.66629365 0.8548909 ] [ 1.0789364 -0.43969718] [-1.34002559 -0.76229719] [ 1.62977397 0.36395259] [ 0.87560937 -1.37508597] [-0.91214153 0.5192275 ] [-0.1313885 -1.99834874] [-0.80740125 0.33509786] [ 0.90573347 -1.15800689] [ 0.73091749 -0.11494194] [-0.95207499 1.70390183] [-0.7807826 1.18661921] [-0.08465479 -0.80179412] [ 0.9801296 -0.08572493] [-1.75606103 1.86116148] [ 0.76398521 -1.5602368 ] [ 1.62196017 -1.43663579] [ 0.48218422 1.98186682] [-0.59215991 -0.68954956] [-1.62845565 0.87886794] [-0.59446473 -0.53331269] [ 0.18880219 0.63195542] [ 0.56976069 -0.25107048] [ 0.54849514 1.30694834] [-0.52600658 1.91312649] [ 0.25563035 2.56027139] [ 0.33194719 0.57458993] [-1.94070096 -0.31473919] [ 0.26501133 -0.33709175] [ 0.12532544 -0.20618611] [-0.48312128 -1.25242019] [ 1.30900889 -1.59669083] [-0.01568461 -0.60254162] [-0.04270708 2.54644392] [ 1.38063447 -1.76322973] [ 0.09442477 0.51040657] [ 0.59917315 -0.21247956] [ 0.1541758 -0.11325782]] Second array [[ 7.48099918e-01 1.18264847e+00] [-2.23133766e+00 -1.55762057e+00] [-1.04462058e+00 -5.49467922e-01] [-1.53862283e+00 -3.64846910e-01] [-7.01715860e-01 -1.42459432e+00] [-8.19690224e-01 -2.91201820e-01] [-5.03466555e-01 -2.58572249e-01] [ 7.62518615e-01 -3.56013289e-01] [ 6.10867429e-01 -1.07932615e+00] [-6.21378241e-01 2.03929061e+00] [-1.15158641e+00 1.29730015e+00] [-2.29112373e-01 1.28870394e+00] [-1.77705956e+00 6.85668134e-01] [ 1.69873754e+00 -8.06340949e-02] [ 8.47773387e-01 8.19588115e-01] [-7.90721724e-01 -1.22740598e+00] [ 1.43057966e+00 1.03234732e+00] [-2.65905077e-01 2.04453714e+00] [ 1.29153883e+00 1.23231405e+00] [ 1.11357029e+00 3.85680576e-02] [-1.28873166e+00 7.99790974e-01] [ 1.10954953e-01 -7.59981814e-02] [-2.08343205e-01 7.76529485e-01] [ 1.05367293e+00 1.30709057e+00] [-1.67357727e+00 -4.38357319e-01] [-5.99401237e-01 -1.20915798e+00] [-1.45236570e+00 -2.41843817e-01] [ 1.52202091e+00 1.20424557e+00] [-2.03248245e-01 9.22083018e-01] [ 7.49849753e-01 7.70107253e-01] [-3.62262805e-01 -4.93539605e-02] [ 5.92774845e-01 1.71538503e-01] [-2.66668020e+00 -7.07788807e-01] [ 6.48773495e-01 5.08520679e-01] [-1.49729586e+00 1.51709189e-02] [ 1.37079326e+00 2.04452656e+00] [-1.50460691e+00 -5.03481521e-01] [ 3.21198281e-02 -5.62401088e-01] [-7.33982834e-02 -1.42651619e+00] [ 1.51023944e-01 7.19347571e-01] [ 1.07714394e+00 1.21259936e+00] [ 1.11437253e+00 -1.04650749e+00] [ 1.60128628e-02 -7.42315320e-02] [ 8.87893818e-01 -9.15525007e-01] [-2.02196405e+00 7.76996294e-01] [-7.56922975e-01 -1.79488795e+00] [-3.89765080e-01 8.43555054e-01] [ 8.56078990e-01 -2.51927290e+00] [-3.57355256e-01 2.24793154e-01] [-1.33983095e+00 7.72415983e-01] [-7.69958902e-01 2.43881809e-02] [ 1.23317128e+00 2.66524847e-01] [-1.11911996e+00 -1.00941556e+00] [ 1.06888481e+00 -7.52488916e-01] [ 1.60011708e+00 -2.34582115e-01] [-1.43613752e+00 2.32089575e-01] [ 2.76827686e+00 4.64236811e-02] [-5.27535636e-01 -7.75962187e-01] [-1.12495302e+00 -3.48897817e-01] [-5.97493581e-01 2.84369327e-01] [ 3.45347914e-01 1.16839513e+00] [-1.54558688e+00 2.49992131e-01] [-2.11190805e+00 5.67828308e-01] [-5.00570911e-01 1.34624679e-01] [ 7.30058096e-01 -1.38506744e+00] [ 2.00731322e-02 1.13366903e-03] [-7.94813873e-01 -1.46685836e+00] [-8.49770854e-01 -1.94732677e+00] [ 8.68067635e-02 -7.13487927e-01] [ 1.01843956e+00 -1.62252298e-01] [-2.62704850e-04 -8.52242843e-01] [ 4.52246355e-01 8.36870515e-01] [ 9.50844740e-01 1.46941955e+00] [ 1.90165032e-02 3.00832009e-01] [-2.62871813e-01 -4.76389989e-01] [-5.45010309e-01 5.07698362e-01] [-2.14740421e+00 1.74494787e-01] [-6.58151595e-01 -1.02180818e-03] [-1.09019280e+00 -1.11610784e+00] [ 3.31202122e-01 1.75803925e-03] [ 9.27351637e-01 -3.54472878e-01] [ 1.85478899e+00 -8.68877729e-01] [-9.51952893e-01 -7.13536855e-01] [ 6.93507185e-01 -7.70291182e-02] [-1.32085755e-01 5.72530157e-01] [ 5.92167545e-01 7.65998597e-01] [ 6.53578237e-03 2.91517827e-01] [-1.66554376e+00 4.16894866e-01] [ 1.70570182e+00 -4.39818760e-01] [ 9.93463550e-01 1.80889097e-01] [-1.54201625e+00 3.60513419e-01] [ 6.19968519e-02 6.84228241e-01] [ 8.40418524e-01 4.45707454e-02] [ 7.77167702e-01 -4.71110263e-01] [-1.28971360e+00 -8.92542205e-02] [ 7.79037778e-01 4.24205626e-01] [ 1.36277668e+00 1.64887445e+00] [-6.02546972e-02 5.91612964e-01] [ 1.12119654e+00 2.18273688e-01] [ 6.08112346e-01 1.57850160e+00] [-3.67369089e-02 1.59761060e+00] [-2.99789258e-01 3.80572874e-01] [ 1.77598813e+00 -2.82818418e-01] [-8.06582150e-01 4.00439731e-01] [-8.00358430e-01 -1.11907538e+00] [-1.22356349e+00 2.02790064e+00] [ 4.73654124e-01 3.28665793e-01] [-3.96535159e-01 -3.15869916e+00] [ 2.24847589e-01 -3.27497295e-01] [-3.85249076e-01 -6.02969593e-01] [-9.06580248e-01 -5.77946901e-01] [ 5.30289533e-01 6.27977318e-01] [-9.97598790e-02 1.80817464e+00] [ 5.46139734e-01 3.01609168e-01] [ 4.08523590e-01 -4.94147030e-01] [-9.44716490e-01 3.01617405e-01] [ 3.52401157e-01 -8.57517147e-01] [-9.81752356e-01 2.47824413e+00] [-1.60518987e+00 1.20014542e+00] [-5.60320176e-02 -4.19654462e-01]]
import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize=(6, 9))

X = np.arange(-5, 5.0, 0.05)

fig, ax = plt.subplots(2, 
           sharex='col', sharey='row')

ax[0].plot(X, f(X), 'bo', X, f(X), 'k')
ax[0].set(title='The function f')

ax[1].plot(X, fp(X), 'go', X, fp(X), 'k')
ax[1].set(xlabel='X Values', ylabel='Y Values',
       title='Derivative Function of f')

plt.show()
Output:
import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize=(6, 9))

X = np.arange(-5, 5.0, 0.05)

fig, ax = plt.subplots(2, 
           sharex='col', sharey='row')

ax[0].plot(X, f(X), 'r', X, f(X), '*')
ax[0].set(title='The function f')

ax[1].plot(X, fp(X), 'g', X, fp(X), '.')
ax[1].set(xlabel='X Values', ylabel='Y Values',
       title='Derivative Function of f')

plt.scatter()
plt.show()
Error:
NameError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_124/2710991408.py in <module> 8 sharex='col', sharey='row') 9 ---> 10 ax[0].plot(X, f(X), 'r', X, f(X), '*') 11 ax[0].set(title='The function f') 12 NameError: name 'f' is not defined <Figure size 432x648 with 0 Axes>
import numpy as np
import matplotlib.pyplot as plt

x , y = np.random.rand(100,2)

fig,axes=plt.subplots(ncols=2)
sc1 = axes[0].scatter(x,y, marker="*", color="r")
sc2 = axes[1].scatter(x,y, marker=".", color="g")
axes[0].set(xlabel="dimension 1", ylabel="dimension 2")
axes[1].set(xlabel="dimension 1", ylabel="dimension 2")

axes[0].legend([sc1], ["Admitted"])
axes[1].legend([sc2], ["Not-Admitted"])
plt.show()
Error:
ValueError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_124/997060509.py in <module> 2 import matplotlib.pyplot as plt 3 ----> 4 x , y = np.random.rand(100,2) 5 6 fig,axes=plt.subplots(ncols=2) ValueError: too many values to unpack (expected 2)
You know how it works by now. Please read BBcode to learn how to post your code and your (complete) error message. (Like Larz60+ told you in your previous topic.)
(Feb-15-2022, 12:59 PM)ibreeden Wrote: [ -> ]You know how it works by now. Please read BBcode to learn how to post your code and your (complete) error message. (Like Larz60+ told you in your previous topic.)

I attached my file but I write it again. Some of my output are charts. Could you tell me, how should I put them in output command in my post, please?