Aprendiendo Plotly. Disponemos del siguiente DataFrame
import pandas_datareader.data as pdr
start = "2016-1-4"
end = "2020-10-19"
iberdrola = pdr.DataReader("IBE.MC", "yahoo", start, end)
iberdrola = iberdrola[["Close"]]
iberdrola[:3]
Utilizando matplotlib, repesento la gráfica de la evolución en el tiempo de la variable y una recta horizontal representando el valor medio de la cotización.
# Import Matplotlib's `pyplot` module as `plt`
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')
%matplotlib inline
""" Figure 32 """
# Creating a figure, setting its size and plotting close prices on it
fig = plt.figure(figsize=(10, 5))
plt.plot(iberdrola, color='purple')
# Customizing the axes
plt.xticks(rotation=45, color='teal', size=12)
plt.yticks(rotation=45, color='teal', size=12)
# Setting axes labels
plt.xlabel('Fechas', {'color': 'orange', 'fontsize':15})
plt.ylabel('Precios', {'color': 'orange', 'fontsize':15})
# Importing NumPy library
import numpy as np
# Calculating the mean value of close prices
mean_price = np.mean(iberdrola["Close"])
# Plotting the horizontal line along with the close prices
plt.axhline(mean_price, color='r', linestyle='dashed')
# Importing DateTime from DateTime library
from datetime import datetime
# Plotting text on date 2014-1-7 and price 120
plt.text(datetime(2016,5,7), 8, 'Precio medio', size=15, color='r') # coordenadas x e y, texto,size y color
plt.show()
Deseo hacer lo mismo utilizando plotly. Consigo representar la gráfica de la siguiente manera.
# Usando plotly.express
import plotly.express as px
# Precio medio de las cotizaciones
mean_price = np.mean(iberdrola["Close"])
df = iberdrola.reset_index()
fig = px.line(df, x='Date', y="Close")
fig.show()
En este punto me he quedado bloqueado y no he sido capaz de representar en la misma gráfica la línea horizontal y el texto. Agradeceré ayuda para superar este paso.