Grantx
Grantx

Reputation: 345

How do I plot this as a scatterplot?

This is killing me. How do I modify this code so that the chart displays as a scatterplot instead of a line graph? I think it has something to do with this line but I dont know what ax=ax does.

data.plot(ax=ax)

Here is my code:

#import libraries
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
%matplotlib inline
from matplotlib.dates import MO, TU, WE, TH, FR, SA, SU

#read data from csv selecting columns inline
data = pd.read_csv('EURCHF_Daily.csv', skipinitialspace=True,parse_dates=['Date'])
#set date as index
data.set_index('Date',inplace=True)
data = data.loc['2019/11/5':'2019/12/30']
#data = data[['Open','High','Low','Last']]
#data['High_Low_Mean'] = data['High'] - data['Low']
data = data[['Last']]

#plot data
fig, ax = plt.subplots(figsize=(15,7))
data.plot(ax=ax)
ax.tick_params(axis='x', colors='White',rotation=90)
ax.tick_params(axis='y', colors='White')
plt.style.use('seaborn-paper')

#set ticks every week
ax.xaxis.set_major_locator(mdates.WeekdayLocator(byweekday=(MO, TU, WE, TH, FR,)))
#set major ticks format
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %d'))

Upvotes: 1

Views: 82

Answers (3)

JohanC
JohanC

Reputation: 80299

Here is a way using scatter from the standard matplotlib, setting the date (the index of the dataframe) as x and the closing value as y. As the default xlim is much too wide in this case, the code sets it using the first and last date.

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.dates import MO, TU, WE, TH, FR

dates = ['2019-12-02', '2019-12-03', '2019-12-04', '2019-12-05', '2019-12-06', '2019-12-09', '2019-12-10',
         '2019-12-11', '2019-12-12', '2019-12-13', '2019-12-16', '2019-12-17', '2019-12-18', '2019-12-19',
         '2019-12-20', '2019-12-23', '2019-12-24', '2019-12-25', '2019-12-26', '2019-12-27', '2019-12-30',
         '2019-12-31']
close = [1.0981, 1.0936, 1.0957, 1.0954, 1.0943, 1.0930, 1.0913, 1.0936, 1.0958, 1.0937, 1.0950, 1.0929, 1.0898,
         1.0877, 1.0880, 1.0889, 1.0866, 1.0880, 1.0889, 1.0881, 1.0849, 1.0853]

data = pd.DataFrame({'Close': close}, index=pd.to_datetime(dates))

fig, ax = plt.subplots(figsize=(15,7))
plt.scatter(data.index, data.Close, color='crimson')
ax.tick_params(axis='x', colors='black', rotation=90)
ax.tick_params(axis='y', colors='black')
ax.set_xlim(data.index[0] - pd.offsets.Hour(12), data.index[-1]+ pd.offsets.Hour(12))
#set ticks every week
ax.xaxis.set_major_locator(mdates.WeekdayLocator(byweekday=(MO, TU, WE, TH, FR)))
# set major ticks format
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %d'))
# automatically set the padding to fit everything
plt.tight_layout()
plt.show()

example plot

Upvotes: 1

Vijayant
Vijayant

Reputation: 30

You can directly add plt.scatter(x, y) to your code where x and y are the columns of Dataframe. It will generate the proper graph as per your requirement. Also put the code plt.show() to show the graph.

See for more details: https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.scatter.html

Upvotes: 1

Andreas Schmid
Andreas Schmid

Reputation: 59

The plot() function of a pandas DataFrame (which your data object is) uses a line plot by default.

You can use DataFrame.plot.scatter(x, y) to create a scatter plot of your data. The arguments x and y specify the columns of the DataFrame that will be used as the plot's X- and Y-axis.

See the documentation for more details: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.scatter.html

Upvotes: 1

Related Questions