Skip to content

Instantly share code, notes, and snippets.

@vigilantPotato
Created April 25, 2024 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vigilantPotato/750bb667658fec496fb3411dfc940458 to your computer and use it in GitHub Desktop.
Save vigilantPotato/750bb667658fec496fb3411dfc940458 to your computer and use it in GitHub Desktop.
How to create scatter graph by matplotlib
import ctypes
import tkinter
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
if __name__ == "__main__":
ctypes.windll.shcore.SetProcessDpiAwareness(1)
root = tkinter.Tk()
#graph data
x = [0, 1, 2, 3, 4, 5]
y = [3, 2, 9, 7, 0, 1]
#create graph objects
fig, ax = plt.subplots(ncols=2)
#size
area = [1, 10, 100, 200, 300, 400]
ax[0].scatter(x, y, s=area)
#color
colors = np.random.rand(6)
ax[1].scatter(x, y, c=colors) #color
#set title
fig.suptitle("scatter examples")
ax[0].set_title("size")
ax[1].set_title("color")
plt.tight_layout()
#set graph to tkinter window
canvas = FigureCanvasTkAgg(fig, root)
toolbar = NavigationToolbar2Tk(canvas, root)
canvas.get_tk_widget().pack() #show graph
plt.close()
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment