Change xx axis in Python

Asked

Viewed 12 times

0

Good afternoon. I’m trying to create a graph in Python, but I wanted to separate the lines and the dots on the left and right so that you can see these dots and not get too close together. How could I do that?

Python code made by me

#trabalho abandono escolar taxas de conclusao do ensino básico e secundário
import matplotlib.pyplot as plt
from numpy import *
import numpy as np
import pandas as pd
import time
import numpy as np
from scipy import interpolate
from matplotlib import style
style.use('ggplot')
plt.rcParams["figure.figsize"] = [14,8]
x = np.array(['2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019'])
y = np.array([28.3, 23.0, 20.5, 18.9, 17.4, 13.7, 14.0, 12.6, 11.8, 10.6])
y0 = np.array([28.2, 26.3, 24.7, 23.6, 21.9, 20.0, 19.0, 18.3, 17.9, 17.3])
y1 = np.array([18.6, 17.8, 17.3, 16.8, 15.0, 14.7, 13.8, 14.0, 14.5, 13.5])
y2 = np.array([13.5, 12.9, 11.3, 10.1, 9.0, 7.9, 6.2, 6.0, 4.7, 4.1])
y3 = np.array([12.7, 12.3, 11.8, 9.7, 8.8, 9.2, 8.8, 8.8, 8.7, 8.2])
y4 = np.array([17.4, 16.6, 14.8, 13.7, 11.7, 10.2, 10.9, 10.4, 9.9, 9.9])
y5 = np.array([13.8, 13.2, 12.6, 11.8, 11.1, 11.0, 10.6, 10.5, 10.5, 10.2])
y6 = np.array([14.8, 14.9, 13.4, 12.4, 11.8, 10.8, 11.2, 10.6, 10.7, 10.9])
#y7 = np.array([13.9, 13.4, 12.7, 11.9, 11.2, 11.0, 10.7, 10.5, 10.5, 10.3])

n = len(x)
x_map = np.arange(0,n)
dx = 0.001
x_int = np.arange(0, n - 1, dx)  # vector where we interpolate

# We create the interpolants our three datasets separately:
f_y = interpolate.interp1d(x_map, y, 'cubic')
f_y0 = interpolate.interp1d(x_map, y0, 'cubic')
f_y1 = interpolate.interp1d(x_map, y1, 'cubic')
f_y2 = interpolate.interp1d(x_map, y2, 'cubic')
f_y3 = interpolate.interp1d(x_map, y3, 'cubic')
f_y4 = interpolate.interp1d(x_map, y4, 'cubic')
f_y5 = interpolate.interp1d(x_map, y5, 'cubic')
f_y6 = interpolate.interp1d(x_map, y6, 'cubic')
#f_y7 = interpolate.interp1d(x_map, y7, 'cubic')

# And interpolate in the resampled x-coordinates:
y_int = f_y(x_int)
y0_int = f_y0(x_int)
y1_int = f_y1(x_int)
y2_int = f_y2(x_int)
y3_int = f_y3(x_int)
y4_int = f_y4(x_int)
y5_int = f_y5(x_int)
y6_int = f_y6(x_int)
#y7_int = f_y7(x_int)

# Finally, plot the interpolated data:
fig, ax = plt.subplots()
ax.plot(x_int, y_int, lw = 4, alpha = 0.8, color = 'red')
ax.plot(x_int, y0_int, lw = 4, alpha = 0.8, color = 'Blue')
ax.plot(x_int, y1_int, lw = 4, alpha = 0.8, color = 'pink')
ax.plot(x_int, y2_int, lw = 4, alpha = 0.8, color = 'Black')
ax.plot(x_int, y3_int, lw = 4, alpha = 0.8, color = 'Green')
ax.plot(x_int, y4_int, lw = 4, alpha = 0.8, color = 'Orange')
ax.plot(x_int, y5_int, lw = 4, alpha = 0.8, color = 'Purple')
ax.plot(x_int, y6_int, lw = 4, alpha = 0.8, color = 'Brown')
#ax.plot(x_int, y7_int, lw = 4, alpha = 0.8, color = 'Blue')
# Set the correct xticks
ax.set_xticks(x_map)
ax.set_xticklabels(x)
plt.rc('xtick', labelsize=14)
plt.rc('ytick', labelsize=14)
ax.tick_params(axis='x', colors='black')
ax.tick_params(axis='y', colors='black')
ax.yaxis.label.set_color('black')
ax.xaxis.label.set_color('black')
ax.set_xlabel('Ano letivo', fontsize=16, labelpad=15)
ax.set_ylabel('Taxas de conclusão dos Ensinos Básico e Secundário (%)', fontsize=16, labelpad=15)
plt.ylim([0, 30])
plt.plot(x, y, "o", color = 'red', label = 'Portugal', markersize = 12)
plt.plot(x, y0, "^", color= 'Blue', label = 'Espanha', markersize = 12)
plt.plot(x, y1, "s", color= 'pink', label = 'Itália', markersize = 12)
plt.plot(x, y2, "v", color= 'Black', label = 'Grécia', markersize = 12)
plt.plot(x, y3, "D", color= 'Green', label = 'França', markersize = 12)
plt.plot(x, y4, "p", color= 'Orange', label = 'Noruega', markersize = 16)
plt.plot(x, y5, "H", color= 'Purple', label = 'União Europeia (desde 2020)', markersize = 12)
plt.plot(x, y6, "*", color= 'Brown', label = 'Reino Unido', markersize = 16)
#plt.plot(x, y7, "^", color= 'Blue', label = 'União Europeia (2013-2020)', markersize = 12)
ax.legend(loc=9, 
          bbox_to_anchor=(.65,1),
          labelspacing=1.5,
          columnspacing=0.5,
          ncol=4, fontsize=16,
          frameon=False)


ax.set_facecolor('xkcd:blue')
ax.patch.set_alpha(0.1)
plt.savefig('output.png', dpi=500, bbox_inches='tight', facecolor='white', transparent=False)
plt.show()

Graph obtained: inserir a descrição da imagem aqui

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.