Charts are an essential tool in data analysis and information visualization. They allow to visually represent the relationship between different variables and facilitate the understanding of patterns, trends and comparisons. In addition, graphics can convey complex information clearly and concisely, making them a valuable communication tool.
In the context of learning Python, creating charts is a great way to apply and consolidate the knowledge gained. By using libraries such as Matplotlib, Seaborn or Plotly, we can generate bar charts, line charts, scatter charts and many other types of visualizations. These libraries offer a wide range of customization and configuration options, allowing us to adapt the graphics to our specific needs.
Charts in ChatGPT
The creation of graphs in ChatGPT gives us several advantages to learn Python. First, it allows us to practice the syntax and functions needed to generate graphics in the Python language. As we explore different types of charts and adjust their parameters, we are applying concepts learned in programming, such as list manipulation, indexing, loops, and control structures.
In addition, when creating graphs in ChatGPT, we are working with real or fictitious data, which allows us to develop data manipulation and analysis skills. We can apply data cleansing and transformation techniques before visualizing it, which helps us better understand how data is handled in the context of visualization.
On the other hand, the graphs generated in ChatGPT provide us with immediate visual feedback on the results of our code. We can look at the graphs in real time and check if the data is represented correctly and if it meets our visualization goals. This helps us identify potential errors and make adjustments to our code to improve the visual presentation of the data.
Creating data that ChatGPT provides us for charts
The distribution of tourist expenditure by destination country will be analysed. The dataset is not provided by ChatGPT itself. I have not checked whether they are correct data or not.
The following code is used:
import matplotlib.pyplot as plt import numpy as np # Data on tourism expenditure in countries years = [2019, 2020, 2021, 2022] countries = ['Spain', 'France', 'Italy', 'Germany', 'UK', 'Greece'] spain = [92.278, 19.723, 22.961, 24.520] france = [55.674, 17.312, 24.751, 28.157] italy = [45.150, 17.720, 23.532, 27.613] germany = [38.861, 16.361, 21.181, 24.553] uk = [41.861, 8.542, 14.852, 16.963] greece = [18.198, 4.370, 7.712, 9.628] # Configuración del gráfico plt.figure(figsize=(10, 6)) # Tamaño del gráfico bar_width = 0.1 opacity = 0.8 index = np.arange(len(years)) # Colores para cada país colors = ['blue', 'orange', 'green', 'red', 'purple', 'brown'] # Dibujar las barras para cada país y año for i, country in enumerate(countries): plt.bar(index + i * bar_width, eval(country.lower()), bar_width, alpha=opacity, color=colors[i], label=country) # Mostrar el valor encima de cada columna for j, value in enumerate(eval(country.lower())): plt.text(j + i * bar_width, value + 0.5, str(value), ha='center', va='bottom', fontsize=5) plt.xlabel('Años') plt.ylabel('Gasto Turístico (miles de millones de euros)') plt.title('Gasto Turístico de países europeos (2019-2022)') plt.xticks(index + bar_width * (len(countries) / 2 - 0.5), years) plt.legend() # Mostrar el gráfico plt.show() # Guardar el gráfico en formato PNG plt.savefig('nombre_del_archivo.png', dpi=300, bbox_inches='tight')
The following graph is obtained:
Checking the percentage change from year to year
Now we’re going to ask ChatGPT to create a table showing the percentage change from year to year for each country and then a graph similar to the one above showing that percentage change. This code is used:
import matplotlib.pyplot as plt import numpy as np # Datos del gasto turístico de los países years = [2019, 2020, 2021, 2022] countries = ['Spain', 'France', 'Italy', 'Germany', 'UK', 'Greece'] spain = [92.278, 19.723, 22.961, 24.520] france = [55.674, 17.312, 24.751, 28.157] italy = [45.150, 17.720, 23.532, 27.613] germany = [38.861, 16.361, 21.181, 24.553] uk = [41.861, 8.542, 14.852, 16.963] greece = [18.198, 4.370, 7.712, 9.628] # Calcular el porcentaje de cambio de año a año para cada país spain_pct_change = [(spain[i+1] - spain[i]) / spain[i] * 100 for i in range(len(spain)-1)] france_pct_change = [(france[i+1] - france[i]) / france[i] * 100 for i in range(len(france)-1)] italy_pct_change = [(italy[i+1] - italy[i]) / italy[i] * 100 for i in range(len(italy)-1)] germany_pct_change = [(germany[i+1] - germany[i]) / germany[i] * 100 for i in range(len(germany)-1)] uk_pct_change = [(uk[i+1] - uk[i]) / uk[i] * 100 for i in range(len(uk)-1)] greece_pct_change = [(greece[i+1] - greece[i]) / greece[i] * 100 for i in range(len(greece)-1)] # Crear una tabla que muestra el porcentaje de cambio de año a año table_data = np.vstack((spain_pct_change, france_pct_change, italy_pct_change, germany_pct_change, uk_pct_change, greece_pct_change)) # Configuración del formato de la tabla row_labels = ['Spain', 'France', 'Italy', 'Germany', 'UK', 'Greece'] col_labels = ['2019-2020', '2020-2021', '2021-2022'] table_title = 'Porcentaje de Cambio de Año a Año del Gasto Turístico' cell_text = [['{:.2f}%'.format(value) for value in row] for row in table_data] colors = plt.cm.get_cmap('Set2', len(countries))(np.arange(len(countries))) # Configuración del gráfico de barras plt.figure(figsize=(10, 6)) # Tamaño del gráfico bar_width = 0.1 # Modifica el valor para ajustar el tamaño de las barras opacity = 0.8 index = np.arange(len(years)-1) # Dibujar las barras para cada país y año for i, country in enumerate(countries): values = table_data[i] pos_values = [value if value >= 0 else 0 for value in values] neg_values = [value if value < 0 else 0 for value in values] plt.bar(index + i * bar_width, pos_values, bar_width, alpha=opacity, color=colors[i], label=country) plt.bar(index + i * bar_width, neg_values, bar_width, alpha=opacity, color=colors[i]) # Mostrar el valor en cada barra for j, value in enumerate(values): if value >= 0: plt.text(index[j] + i * bar_width, value + 0.5, '{:.2f}%'.format(value), ha='center', va='bottom', fontsize=8) else: plt.text(index[j] + i * bar_width, value - 0.5, '{:.2f}%'.format(value), ha='center', va='top', fontsize=8) plt.xlabel('Años') plt.ylabel('Cambio Porcentual del Gasto Turístico') plt.title('Cambio Porcentual de los Gastos Turísticos de países europeos (2019-2022)') plt.xticks(index + bar_width * (len(countries) / 2 - 0.5), col_labels) plt.legend() # Guardar el gráfico en formato PNG plt.savefig('porcentual.png', dpi=300, bbox_inches='tight') # Mostrar el gráfico plt.show()
The data comes from the following table:
Country | 2019-2020 | 2020-2021 | 2021-2022 |
Spain | -78,6265 | 16,41738 | 6,789774 |
France | -68,9047 | 42,97019 | 13,76106 |
Italy | -60,753 | 32,7991 | 17,34234 |
Germany | -57,8987 | 29,4603 | 15,91993 |
UK | -79,5944 | 73,87029 | 14,21357 |
Greece | -75,9864 | 76,47597 | 24,8444 |
And they give rise to the following graph:
Summary
In short, creating charts in ChatGPT is a great way to learn Python and consolidate the concepts of programming and data analysis. It allows us to apply Python syntax and functions in the context of data visualization, develop data manipulation and analysis skills, and get immediate visual feedback on our results. In addition, by generating graphs, we can communicate and convey information effectively, which is essential in data analysis and informed decision making.
On many occasions the result we obtain is not correct. The most appropriate way to solve it is to tell ChatGPT exactly that it is wrong and he will only debug the code little by little. The result obtained in this article has been after several attempts, asking the right questions to get what you are looking for.