import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x = np.linspace(-3, 3, 10)
y = np.exp(x)
print(x)
print(y)
[-3. -2.33333333 -1.66666667 -1. -0.33333333 0.33333333
1. 1.66666667 2.33333333 3. ]
[ 0.04978707 0.09697197 0.1888756 0.36787944 0.71653131 1.39561243
2.71828183 5.29449005 10.3122585 20.08553692]
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x7f0518676c50>]
plt.plot(x,y)
# plt.xlabel()Label the x-axis with
plt.xlabel('Efforts')
# plt.ylabel()Label the y-axis with
plt.ylabel('Earning')
# plt.title()Give a title to the figure with
plt.title('This is how your efforts earns')
Text(0.5, 1.0, 'This is how your efforts earns')
# plt.plot(label='label')でplotにlabelをつけ, plt.legend()To give a legend
plt.plot(x, y, label='Earning with effort')
plt.legend()
# plt.xticks()Add arbitrary ticks to the x-axis with
plt.xticks(np.arange(-3, 4, 0.5))
# plt.yticks()Attach arbitrary ticks to the y-axis with
plt.yticks([0, 5, 10, 20])
plt.show()
x = np.linspace(-3, 3, 10)
y1 = np.exp(x)
y2 = np.exp(x)*2
plt.plot(x, y1, label='first')
plt.plot(x, y2, label='second')
plt.legend()
<matplotlib.legend.Legend at 0x7f05182abc50>
plt.plot(x, y1, label='first')
plt.plot(x, y2, label='second')
plt.axis('off')
# plt.axis('off')Erase the axis with
plt.legend()
<matplotlib.legend.Legend at 0x7f05182f8e10>
subplot
x = np.linspace(-3, 3, 10)
y1 = np.exp(x)
y2 = x*x
plt.subplot(1, 2, 1)
plt.plot(x, y1)
plt.subplot(1, 2, 2)
plt.plot(x, y2)
[<matplotlib.lines.Line2D at 0x7f0505d1d690>]
fig = plt.figure()
type(fig)
matplotlib.figure.Figure
<Figure size 432x288 with 0 Axes>
fig = plt.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)
ax1.plot(x, y1)
ax2.plot(x, y2)
[<matplotlib.lines.Line2D at 0x7f0505aead10>]
#Create a 1-by-2 plot Each axes object is returned as a list in axes
fig, axes = plt.subplots(nrows=1, ncols=2)
axes
array([<matplotlib.axes._subplots.AxesSubplot object at 0x7f0505bd2650>,
<matplotlib.axes._subplots.AxesSubplot object at 0x7f0505a22a10>],
dtype=object)
fig, axes = plt.subplots(nrows=1, ncols=2)
axes[0].plot(x, y1)
axes[1].plot(x, y2)
[<matplotlib.lines.Line2D at 0x7f0505894fd0>]
fig, axes = plt.subplots(nrows=3, ncols=3)
print(axes.shape)
(3, 3)
fig, axes = plt.subplots(nrows=3, ncols=3)
axes[1,2].plot(x,y2)
[<matplotlib.lines.Line2D at 0x7f05055cee90>]
fig, axes = plt.subplots(nrows=1, ncols=2)
axes[0].plot(x, y1, label='something')
axes[1].plot(x, y1)
axes[0].set_xlabel('xlabel1')
axes[0].set_ylabel('xlabel2')
axes[0].set_title('plot title')
axes[0].set_xticks([-3, -2, -1, 3])
axes[0].set_yticks([0, 10, 20])
axes[0].legend()
axes[1].axis('off')
(-3.3, 3.3, -0.9520004243731263, 21.08732441592866)
x = np.linspace(-3, 3, 10)
y1 = np.exp(x)
y2 = np.exp(x)*2
fig, axes = plt.subplots()
axes.plot(x, y1)
[<matplotlib.lines.Line2D at 0x7f0505c7cb10>]
#In this case, a graph of 100 pixels x 100 pixels is displayed on the monitor.
fig, axes = plt.subplots(figsize=(1,1), dpi=100)
axes.plot(x, y1)
[<matplotlib.lines.Line2D at 0x7f0505308750>]
fig, axes = plt.subplots(figsize=(10, 3))
axes.plot(x, y1)
[<matplotlib.lines.Line2D at 0x7f05056641d0>]
fig, axes = plt.subplots(2, 1, figsize=(10, 3))
axes[0].plot(x, y1, label='something')
axes[1].plot(x, x*x)
axes[0].set_title('first')
axes[1].set_title('second')
axes[0].set_xlabel('x')
axes[0].set_ylabel('y')
axes[1].set_xlabel('x')
axes[1].set_ylabel('y')
fig.savefig('savefig_sample.png')
fig, axes = plt.subplots(2, 1, figsize=(10, 3))
axes[0].plot(x, y1, label='something')
axes[1].plot(x, x*x)
axes[0].set_title('first')
axes[1].set_title('second')
axes[0].set_xlabel('x')
axes[0].set_ylabel('y')
axes[1].set_xlabel('x')
axes[1].set_ylabel('y')
plt.tight_layout() #Adjust the graph to make it easier to see
fig.savefig('savefig_sample.png')
fig, axes = plt.subplots()
axes.plot(x, y1, label='first')
axes.plot(x, y2, label='second')
axes.plot(x, y1+y2, label='first+second')
axes.legend()
<matplotlib.legend.Legend at 0x7f0505664f50>
from matplotlib.backends.backend_pdf import PdfPages
pdf = PdfPages('savefig_sample.pdf')
from matplotlib.backends.backend_pdf import PdfPages
pdf = PdfPages('savefig_sample.pdf')
#------Graph creation-------
fig, axes = plt.subplots()
axes.plot(x, y1, label='first')
axes.plot(x, y2, label='second')
axes.plot(x, y1+y2, label='first+second')
axes.legend(loc=0)
#---------------------
#Save to pdf
pdf.savefig(fig)
#close processing (I will do it for the time being)
pdf.close()
pdf = PdfPages('savemultifig_sample.pdf')
for i in range(0, 10):
#------Graph creation--------
fig, axes = plt.subplots( )
#It was designed so that the shape of the graph gradually changes. (Appropriate.)
axes.plot(x, y1 + x*i)
#Give it a title. Please confirm that you can search for characters in pdf.
axes.set_title('ID:#{}'.format(i))
#-----------------------
#Save in for loop
pdf.savefig(fig)
#Close after loop
pdf.close()
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x = np.linspace(-3, 3, 10)
y = np.exp(x)
plt.plot(x, y)
[<matplotlib.lines.Line2D at 0x7f050476fd90>]
plt.plot(x, y, color='red', lw=5, ls='--', marker='o', markersize=15, markerfacecolor='yellow', markeredgecolor='blue',
markeredgewidth=4, alpha=0.5)
[<matplotlib.lines.Line2D at 0x7f05050fbd90>]
import pandas as pd
df = pd.read_csv('train.csv')
plt.scatter(df['Age'], df['Fare'], alpha=0.3)
<matplotlib.collections.PathCollection at 0x7f04b99fbf50>
plt.hist(df['Age'])
plt.show()
/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/histograms.py:829: RuntimeWarning: invalid value encountered in greater_equal
keep = (tmp_a >= first_edge)
/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/histograms.py:830: RuntimeWarning: invalid value encountered in less_equal
keep &= (tmp_a <= last_edge)
plt.hist(df['Age'], bins=50)
plt.show()
df = df.dropna(subset=['Age'])
plt.boxplot(df['Age'])
plt.show()
Recommended Posts