H a n d s o n, p r o j e c t b a s e d


Changing the Label Type and Line Thickness


Download 4.21 Mb.
Pdf ko'rish
bet251/344
Sana31.01.2024
Hajmi4.21 Mb.
#1818553
1   ...   247   248   249   250   251   252   253   254   ...   344
Bog'liq
Python Crash Course, 2nd Edition

Changing the Label Type and Line Thickness
Although the plot in Figure 15­1 shows that the numbers are increasing, the 
label type is too small and the line is a little thin to read easily. Fortunately
Matplotlib allows you to adjust every feature of a visualization.


308
Chapter 15
We’ll use a few of the available customizations to improve this plot’s 
readability, as shown here:
import matplotlib.pyplot as plt
squares = [1, 4, 9, 16, 25]
fig, ax = plt.subplots()

ax.plot(squares, linewidth=3)
# Set chart title and label axes.

ax.set_title("Square Numbers", fontsize=24)

ax.set_xlabel("Value", fontsize=14)
ax.set_ylabel("Square of Value", fontsize=14)
# Set size of tick labels.

ax.tick_params(axis='both', labelsize=14)
plt.show()
The 
linewidth
parameter at  controls the thickness of the line that 
plot()
generates. The 
set_title()
method at  sets a title for the chart. The 
fontsize
parameters, which appear repeatedly throughout the code, control 
the size of the text in various elements on the chart.
The 
set_xlabel()
and 
set_ylabel()
methods allow you to set a title for 
each of the axes , and the method 
tick_params()
styles the tick marks . 
The arguments shown here affect the tick marks on both the x­ and y­axes 
(
axis='both'
) and set the font size of the tick mark labels to 14 (
labelsize=14
).
As you can see in Figure 15­2, the resulting chart is much easier to read. 
The label type is bigger, and the line graph is thicker. It’s often worth experi­
menting with these values to get an idea of what will look best in the result­
ing graph. 
Figure 15-2: The chart is much easier to read now.
mpl_squares.py


Generating Data

Download 4.21 Mb.

Do'stlaringiz bilan baham:
1   ...   247   248   249   250   251   252   253   254   ...   344




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling