Plotly library in python not showing my chart - python

I am trying to get my code to show the piechart but I don't see the visualization after execution.
import pandas as pd
import numpy as np
import plotly.express as px
data = pd.read_csv("C:\\Users\\nasir\\credit card.csv")
print(data.head())
print(data.isnull().sum())
# Exploring transaction type
print(data.type.value_counts())
type = data["type"].value_counts()
transactions = type.index
quantity = type.values
figure = px.pie(data, values=quantity, names=transactions, hole=0.5, title="Distribution of Transaction Type")
figure.show()

Related

Plotting data with pandas and matplotlib but no error also not showing data

import matplotlib.pyplot as plt
import pandas as pd
sickpay = pd.read_csv('sickleavedata.csv', index_col = 0)
plt.bar(sickpay, height=1)
plt.xlabel('JobTitle')
plt.ylabel('SickLeaveHours')
plt.title('Ages of different persons')
plt.legend()
plt.show()
Trying to create a visual data but it wont show any data on the chart also not getting any errors
as per the given code sickpay is your dataframe, not a column of a dataframe
so you can give a column as flows or use dataframe.plot(kind='bar') option
import matplotlib.pyplot as plt
import pandas as pd
sickpay = pd.read_csv('sickleavedata.csv', index_col = 0)
sickpay.plot(kind='bar')
# or
plt.bar(sickpay['column_name'], height=1)

Missing part of the data in graph

I'm writing a program which will show the candlestick chart of Gold and detect patterns. I'm getting the data from yfinance and trying to draw the chart with plotly, but I see that some parts of the data are missing. I checked the data with mplfinance and everything worked successfully, but I need it in plotly.
import plotly.graph_objects as go
import pandas as pd
import yfinance as yf
import talib
import mplfinance as mpf
data = yf.download(tickers="GC=F", period="5d", interval="5m")
fig = go.Figure(data=[go.Candlestick(x=data.index,
open=data['Open'], high=data['High'],
low=data['Low'], close=data['Close'])
])
fig.update_layout(xaxis_rangeslider_visible=False)
fig.write_html('first_figure.html', auto_open=True)
There are indeed some lacunae in the original yahoo data (the website has inconsistent X axis rather than showing gaps). For the purpose of time series analysis, applying data = data.resample('5T').ffill() (or interpolate()) is about the best you can do I presume.
If you wish to imitate yahoo chart behaviour, you'll have to configure rangebreaks like in this question.
I don't have the code for mplfinance so I don't know, but I think nontarading=True is set and the gap is automatically removed. plotly has a feature for market holidays and nighttime exclusions. Since your time series data is in 5 minute increments, set dvalue=1000ms60sec60min minutes. The main point is to prepare and set the time series list you want to remove.
import plotly.graph_objects as go
import pandas as pd
import yfinance as yf
import numpy as np
data = yf.download(tickers="GC=F", period="5d", interval="5m")
full_range = pd.date_range(data.index[0],data.index[-1], freq='5min')
data = data.reindex(full_range, fill_value=np.NaN, axis=0)
delete_range = data[data['Open'].isnull()].index
fig = go.Figure(data=[go.Candlestick(x=data.index,
open=data['Open'], high=data['High'],
low=data['Low'], close=data['Close'])
])
fig.update_layout(xaxis_rangeslider_visible=False)
fig.update_xaxes(rangebreaks=[
dict(values=delete_range, dvalue=3600000)
])
# fig.write_html('first_figure.html', auto_open=True)
fig.show()

how to plot a map using geopandas and matplotlib

there seems to be an issue with my code. My goal is to plot a map that represents an outcome (population) accross the regions of Benin.
import pandas as pd
import matplotlib as mpl
database_path = "datafinalproject.csv"
database = pd.read_csv(database_path)
#Creating a geodataframe
points = gpd.points_from_xy(database["longitude"], database["latitude"], crs="EPSG:4326")
map = gpd.GeoDataFrame (database, geometry=points)
I get this message when I type map.plot and I when I type map.plot(column='population'), I get an empty map.
Can you help me solve this problem?
database.head() gives :
map.plot() will work in a Jupyter notebook but not in a normal Python environment.
You should import matplotlib.pyplot and add plt.show() at the end of your code:
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
database_path = "datafinalproject.csv"
database = pd.read_csv(database_path)
#Creating a geodataframe
points = gpd.points_from_xy(database["longitude"], database["latitude"], crs="EPSG:4326")
map = gpd.GeoDataFrame (database, geometry=points)
map.plot()
plt.show()

Valueerror invalid element(s) received for the 'data' property of scatter. Tried to plot multiple graphs sharing one x-axis using plotly but failed

I got errors everytime I try to combine graphs together using plotly. I have no problem when it's just x1,y1. But when I try to have x1,x2,.. and so on, it starts giving me the error as mentioned in the title. Here is my code:
import pandas as pd
import plotly
#plotly.offline.init_notebook_mode(connected=True)
import plotly.graph_objs as go
import chart_studio.plotly as py
excel_file = 'C:\\Users\\Taffy R. Mantang\\Desktop\\matrixtester.csv'
df = pd.read_csv(excel_file)
df.head()
data0 = [go.Scatter(x=df['Date'],y=df['0/0'],mode='lines',name='0/0')]
data1 = [go.Scatter(x=df['Date'],y=df['0/1'],mode='lines',name='0/1')]
data2 = [go.Scatter(x=df['Date'],y=df['0/2'],mode='lines',name='0/2')]
data3 = [go.Scatter(x=df['Date'],y=df['0/3'],mode='lines',name='0/3')]
layout = go.Layout(title='processor ISW-1',plot_bgcolor='rgb(230,230,230)',showlegend=True)
fig = go.Figure(data=[data0,data1,data2,data3],layout=layout)
py.offline.plot(fig)
When I only plot data0, or data1 and so on, it works. But when I try data = [data0,data1,data2,data3] it gives me the error.
What exactly is the problem? Help :'(((
I used the code from this website:
https://chart-studio.plotly.com/~notebook_demo/84.embed
You can create separate traces using the fig.add_trace() command, so this will allow you to create multiple traces of graphs on a single plot. This website should help: https://plotly.com/python/creating-and-updating-figures/#adding-traces,
In your instance you could do
import pandas as pd
import plotly
#plotly.offline.init_notebook_mode(connected=True)
import plotly.graph_objs as go
import chart_studio.plotly as py
excel_file = 'C:\\Users\\Taffy R. Mantang\\Desktop\\matrixtester.csv'
df = pd.read_csv(excel_file)
df.head()
data0 = go.Scatter(x=df['Date'],y=df['0/0'],mode='lines',name='0/0')
data1 = go.Scatter(x=df['Date'],y=df['0/1'],mode='lines',name='0/1')
data2 = go.Scatter(x=df['Date'],y=df['0/2'],mode='lines',name='0/2')
data3 = go.Scatter(x=df['Date'],y=df['0/3'],mode='lines',name='0/3')
layout = go.Layout(title='processor ISW-1',plot_bgcolor='rgb(230,230,230)',showlegend=True)
fig = go.Figure(layout=layout)
fig.add_trace(data0)
fig.add_trace(data1)
fig.add_trace(data2)
fig.add_trace(data3)
plotly.offline.plot(fig)

How to plot this graph using Python properly

I am trying to plot the graph bellow using python, but I am getting an error.
The Python commands I am using are:
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('data/filtro_bovespa_final.csv')
data.loc[(data['codigo'] == 'BBAS3') & (data['codigo'] == 'BBDC4')]
data.date = pd.to_datetime(data['date'],format='%Y%m%d')
data.set_index(['date','codigo'])
plt.plot(data.date,data.preco)
plt.show()
The error I am getting is:
I got this graph, but it is not what I need:
The csv file I am using: Bovespa
I need a graph that allows me to compare the price linked with both the codes (BBAS3 and BBDC4) as the first graph I showed.
What else should I do to get the graph I need?
To draw them by attribute, we use a pivot to turn the data frames into columns by attribute. I've also changed the extraction condition to OR.
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('./Data/filtro_bovespa_final.csv')
data.date = pd.to_datetime(data['date'],format='%Y%m%d')
data = data.loc[(data['codigo'] == 'BBAS3') | (data['codigo'] == 'BBDC4')]
data.set_index('date', inplace=True)
data = data.pivot(columns='codigo')
data.columns = ['BBAS3','BBDC4']
data.plot()
plt.show()

Categories

Resources