I have created a function which takes values from a CSV file into a dataframe in Python. I have automated the code so that it makes graphs of all the countries and saves it in a folder. If i pass in just one country the graph shows fine. But, when i pass in more than one country the graph appears with axes but does not show the graph lines itself. How can i fix this? Thanks in advance. Here is my code:
import pandas as pd
import numpy as np
from pandasql import sqldf
import pandasql as pdsql
import pandasql as psql
import pandas_datareader as pdr
import matplotlib.pyplot as plt
%matplotlib inline
covid=pd.read_csv("C:/Users/Test/Desktop/covid.csv")
countries = ['Canada', 'Brazil']
for country in countries:
covid = psql.sqldf(f"select co, new_deaths from covid where co= '{country}'")
plt.figure(figsize=(15,10))
plt.grid(True)
covid['MA_5'] = covid.new_deaths.rolling(5).mean()
covid['MA_10'] = covid.new_deaths.rolling(10).mean()
plt.plot(covid['new_deaths'],label='new_deaths')
plt.plot(covid['MA_5'], label='MA 5 day')
plt.plot(covid['MA_10'], label='MA 10 day')
plt.legend(loc=2)
plt.plot(country)
plt.savefig(country+".png")
plt.show()
With one countrycountries = ['Canada'] I get this:
https://i.stack.imgur.com/d6hC3.png
If i pass two countries like so: countries = ['Canada', 'Brazil'] I get this:
https://i.stack.imgur.com/d6hC3.png
https://i.stack.imgur.com/Sb6tZ.png
After countless hours of research and editing my code i have finally found the solution. You have to move the CSV code inside the for loop. Whenever you run the code, Python's memory is refreshed and it goes to the next string in the list. The graph shows fine. Here i have provided the code:
import pandas as pd
import numpy as np
from pandasql import sqldf
import pandasql as pdsql
import pandasql as psql
import pandas_datareader as pdr
import matplotlib.pyplot as plt
%matplotlib inline
countries = ['Canada', 'Brazil']
for country in countries:
covid=pd.read_csv("C:/Users/Test/Desktop/covid.csv")
covid = psql.sqldf(f"select co, new_deaths from covid where co= '{country}'")
plt.figure(figsize=(15,10))
plt.grid(True)
covid['MA_5'] = covid.new_deaths.rolling(5).mean()
covid['MA_10'] = covid.new_deaths.rolling(10).mean()
plt.plot(covid['new_deaths'],label='new_deaths')
plt.plot(covid['MA_5'], label='MA 5 day')
plt.plot(covid['MA_10'], label='MA 10 day')
plt.legend(loc=2)
plt.plot(country)
plt.savefig(country+".png")
plt.show()
Related
#imports
import pandas as pd
import yfinance as yf
from datetime import date, timedelta
from matplotlib import pyplot as plt
import numpy as np
plt.style.use('fivethirtyeight')
#Get Albemarle Information
ALBINFO = yf.Ticker("ALB")
# Valid options are 1d, 5d, 1mo, 3mo, 6mo, 1y, 2y,
# 5y, 10y and ytd.
print(ALBINFO.history(period="2y"))
#Graph the Close Values
plt.figure(figsize = (12.2,4.5))
plt.plot(ALBINFO['Close'], label = 'Close')
plt.xticks(rotation = 45)
plt.title('Close Price History')
plt.xlabel('Date')
plt.ylabel('Price USD')
plt.show()
I am writing this code on Python Visual Studio Code.
I am trying to be a graphical representation of the ALB closing stock price.
However, whenever I run the code, only a blank screen shows up.
The computer states that there is a problem with the line
plt.plot(ALBINFO['Close'], label = 'Close').
Could someone help understand what is wrong with this line?
Thank you
I coded the lines above and excepted a graph of closing prices but only got a blank graph.
You need to store a dataframe(object) in order to use it. In this case, ALBINFO is overwritten:
import pandas as pd
import yfinance as yf
from datetime import date, timedelta
from matplotlib import pyplot as plt
import numpy as np
plt.style.use('fivethirtyeight')
#Get Albemarle Information
ALBINFO = yf.Ticker("ALB")
# Valid options are 1d, 5d, 1mo, 3mo, 6mo, 1y, 2y,
# 5y, 10y and ytd.
ALBINFO = ALBINFO.history(period="2y")
#Graph the Close Values
plt.figure(figsize = (12.2,4.5))
plt.plot(ALBINFO['Close'], label = 'Close')
plt.xticks(rotation = 45)
plt.title('Close Price History')
plt.xlabel('Date')
plt.ylabel('Price USD')
plt.show()
So I'm still getting to grips with Python after coming over from R recently.
I'm struggling to automatically annotate plots from DF Column. Which is easily done in R.
I was helped the other day on the same matter on MPL Scatter plots.
But I've been tearing my hair out trying to figure this out. I'll add some random data, and show a picture of the sort of thing I'm after.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns
d = {'Player': ['Messi', 'Ronaldo','Mbappe','Kovacic', 'Werner','Salah'], '% of Squad pass': [3.2,3.2,4.4,9.9,7.4,4.8)
df = pd.DataFrame(data = d)
This is what I'm doing at the minute.
fig, ax = plt.subplots(1,1, figsize=(4,4))
sns.swarmplot(data=df, x ='% of Squad Pass', ax = ax)
Which gets me this,
Is there a loop function I can use that will automatically annotate the plot points with text from the 'Player' column in the dataframe?
So I'd end with something like this
Thanks and hopefully this will be my last question on the matter!
This is my proposal. You need import random library.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns
import random
d = {'Player': ['Messi', 'Ronaldo','Mbappe','Kovacic', 'Werner','Salah'], '% of Squad pass': [3.2,3.2,4.4,9.9,7.4,4.8]}
df = pd.DataFrame(data = d)
fig, ax = plt.subplots()
sns.swarmplot(data = df, x=df['% of Squad pass'], ax = ax)
for i, j in enumerate(df['% of Squad pass']):
plt.annotate(df['Player'][i],
xy=(df['% of Squad pass'][i],0),
xytext=(df['% of Squad pass'][i], random.uniform(0.2,0.4)),
arrowprops=dict(arrowstyle="->"))
Trying to create a bar chart using a CSV file and Matplotlib. However, there are two headings (COUNTRY & COST) which means that the code isn't able to run properly and produce the bar chart. How do I edit the code so that it will ignore the headings? The first image is what the CSV file actually looks like and the second image is what the code is able to understand and run.
EDIT: the python assisstant tells me that the error seems to be occurring in Line 14 of the code: price.append(float(row[1]))
import matplotlib.pyplot as plt
import csv
price = []
countries = []
with open ("Europe.csv","r") as csvfile:
plot = csv.reader(csvfile)
for idx, row in enumerate(plot):
if idx == 0:
continue
price.append(float(row[1]))
countries.append(str(row[0]))
plt.style.use('grayscale')
plt.bar( countries, price, label='Europe', color='red')
plt.ylabel('Price in US$')
plt.title('Cost of spotify premium per country')
plt.xticks(rotation=90)
plt.legend(loc='best')
plt.show()
I would use pandas for this. With that you can then more easily create the bar plot using this function.
Example using your variables countries and price:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({"country": countries, "price": price})
df.plot.bar(x="country", y="price")
plt.show()
Just using pandas.read_csv then using skiprows=[0],header=None like this:
import pandas as pd
df = pd.read_csv('data.csv',sep=';',skiprows=[0],header=None)
Iam using separator ';' to data because I assume your csv file create in ms.excel
But I think just read the csv file without skiprows, like this:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('data.csv',sep=';')
price = df['cost']
countries = df['country']
plt.style.use('grayscale')
plt.bar( countries, price, label='Europe', color='red')
plt.ylabel('Price in US$')
plt.title('Cost of spotify premium per country')
plt.xticks(rotation=90)
plt.legend(loc='best')
plt.show()
for data like this :
and the result like this :
I'm trying:
import numpy as np
import matplotlib.pyplot as plt
But it gives me an error message, as shown below:
ModuleNotFoundError: No module named 'plotly'
Let's try this bit of code from a game on November 24, 2018 at Kyle Field.
import pandas as pd
import numpy as np
%matplotlib inline
df = pd.read_html('https://www.sports-reference.com/cfb/boxscores/2018-11-24-texas-am.html')[8]
df['Quarter']=df['Quarter'].ffill()
df_1 = df.drop_duplicates(subset=['Quarter'], keep='last')
df_1 = df_1.set_index('Quarter')
ax = df_1[['LSU','TAMU']].diff().fillna(df_1[['LSU','TAMU']]).plot.bar(title="TAMU over LSU in 7 OTs")
_ = ax.set_ylabel('Score by Period')
Output:
I get one choropleth map using the following code:
%matplotlib inline
import seaborn as sns
import pandas as pd
import pysal as ps
import geopandas as gpd
import numpy as np
import matplotlib.pyplot as pet
imd_shp = 'desktop/sgfwu/E07000222_IMD/shapefiles/E07000222.shp'
imd = gpd.read_file(imd_shp)
imd = imd.set_index('LSOA11CD')
imd.plot(column='imd_score', scheme='fisher_jenks', alpha=0.8, k=7,
colormap=plt.cm.Blues, legend=True, axes=ax1)
The result is:
But how can I change the text of the legend to the words like the map below, rather than numbers?
This question has been here for a while, but I just had the same problem. This solved it for me:
leg = ax1.get_legend()
leg.get_texts()[0].set_text('New label 1')
leg.get_texts()[1].set_text('New label 2')
and so on for as many labels as you want to change.