Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dotplot : Added functionality to plot multiple dots,Fixed some errors in plot_line function #90

Merged
merged 5 commits into from
Feb 2, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.pyc
.temp/
venv/
.png
test.py
62 changes: 60 additions & 2 deletions lib/plotutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def plot(func, xpoints, color_name, xlabel, ylabel, theme, gui, line_style, file

if file_path != "":
plt.savefig(file_path)

plt.grid(True)

if not gui:
Expand Down Expand Up @@ -113,7 +113,7 @@ def plot_line(arrays, color_name, xlabel, ylabel, theme, gui, line_style, file_p
plt.savefig(file_path)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(r'$ ' + func + ' $')
plt.title(r'$ ' + 'Line:' + str(xvals) +','+ str(yvals) + ' $')

if file_path != "":
plt.savefig(file_path)
Expand All @@ -139,3 +139,61 @@ def plot_line(arrays, color_name, xlabel, ylabel, theme, gui, line_style, file_p

plt.cla()
plt.clf()

def plot_dot(xyval, color_name, xlabel, ylabel, theme, gui, dot_style, file_path):

# Show plot summary
print('***** Plot Summary *****')
print('X,Y Value: {}'.format(xyval))
print('Color: {}'.format(color_name))
print('X-label: {}'.format(xlabel))
print('Y-label: {}'.format(ylabel))

if theme == 'dark':
mplstyle.use('dark_background')
else:
mplstyle.use('default')

try:
# Check if color is hex code
is_hex = re.search(r'^#(?:[0-9a-fA-F]{3}){1,2}$', color_name)
if not is_hex:
colors = mcolors.cnames
if color_name not in colors:
print(color_name, ": Color not found.")
color_name = 'blue'

xy=xyval.split(',')
l=len(xy)
#Check if even number of arguments are given
if (l%2==0):
#Extract x-values from xyval string
xval=[float(xy[i]) for i in range(0,l,2)]
#Extract y-values from xyval string
yval=[float(xy[i]) for i in range(1,l+1,2)]

plt.scatter(xval, yval, color=color_name, marker=dot_style)
plt.savefig(file_path)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(r'$ ' + xyval + ' $')

if file_path != "":
plt.savefig(file_path)

plt.grid(True)

if not gui:
plt.show()
else:
if not os.path.exists('.temp/'):
os.mkdir('.temp/')
plt.savefig(".temp/generated_plot.png")
else:
print("Cannot plot odd Number of Coordinates")

except Exception as e:
print("An error occured.",e)

plt.cla()
plt.clf()
31 changes: 20 additions & 11 deletions plotit.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
xlabel = "X-axis"
ylabel = "Y-axis"
line_style = "-"
dot_style = "o"
dot_size = 5
file_path = ""


Expand Down Expand Up @@ -41,21 +43,24 @@ def sigint_handler(signum, frame):
parser.add_option('-p', '--points', dest='xpoints',
help='Enter discrete x values for which the \
function will be plotted like [x1,x2,x3,...,xn]')
parser.add_option('-d', '--dot', dest='dot', help='Enter comma separated X and Y points like\
5,9,4,7,4,6 to plot the dots at\
(5,9),(4,7),(4,6) coordinates.')
parser.add_option('-l', '--line', dest='line',
help='Enter 2 Arrays of X and Y Coordinates like \
[x1,x2,x3,...,xn],[y1,y2,y3,...,yn]')
\'[x1,x2,x3,...,xn],[y1,y2,y3,...,yn]\'')
parser.add_option('-t', '--theme', dest='theme',
help='Enter theme for displaying plot (dark or light)')
parser.add_option('--symbol', dest='line_style',
help='Enter linestyle for plot, accepted linestyles "-", ":", "-.", "--"',
choices=["-",":","-.","--"])
parser.add_option('--symbol', dest='line_style',
help='Enter linestyle for plot, accepted linestyles "-", ":", "-.", "--"',
choices=["-",":","-.","--"])
parser.add_option('--save', dest='file_path',
help='Enter file path eg: path/filename.png')
help='Enter file path eg: path/filename.png')

(options, args) = parser.parse_args()

if not options.func and not options.line:
print('Please enter a function or 2 arrays of x and y coordinates')
if not options.func and not options.line and not options.dot:
print('Please enter a function or 2 arrays of x and y coordinates or Coordinates of a single dot')
exit(0)

if options.color:
Expand All @@ -68,15 +73,15 @@ def sigint_handler(signum, frame):
ylabel = str(options.ylabel)

if options.line_style:
line_style = str(options.line_style)
line_style = str(options.line_style)

if options.theme:
theme = str(options.theme)
else:
theme = 'default'

if options.file_path:
file_path = str(options.file_path)
file_path = str(options.file_path)


if options.func:
Expand Down Expand Up @@ -109,8 +114,12 @@ def sigint_handler(signum, frame):

plu.plot(func, xpoints, color, xlabel, ylabel, theme, False, line_style, file_path, discrete)

else: # No function, hence try to take points for line
elif options.dot:
xyval = options.dot
plu.plot_dot(xyval, color, xlabel, ylabel, theme, False, dot_style, file_path)

elif options.line:
xypoints = options.line
plu.plot_line(xypoints, color, xlabel, ylabel, theme, False, line_style, file_paths)
plu.plot_line(xypoints, color, xlabel, ylabel, theme, False, line_style, file_path)

# Visualise using matplotlib