I'm currently working on a program that plots many graphs. I'm using Jupyter. It works okay so far, but it is printing each graph to its own window. This is less than ideal because there are hundreds of graphs.
What are some ways to condense the output? I am hoping for a way to have the many graphs sent to a single document/window.
Also, I am iterating over a dictionary and only plotting graphs when the program encounters something of interest, so the frequency is not very predictable. It is something like this:
while still_true:
if my_condition is True:
a = np.arange(20) // not actually a range, but a dynamic np array
plt.plot(a)
plt.ylabel("some numbers")
plt.show()
Related
I have a datafram with following structure
,mphA,gyrA,parC,tet59,qnrVC
sample1,TRUE,FALSE,FALSE,FALSE,FALSE
sample2,TRUE,FALSE,FALSE,FALSE,TRUE
sample3,FALSE,FALSE,FALSE,TRUE,FALSE
sample4,FALSE,FALSE,FALSE,TRUE,TRUE
sample5,TRUE,FALSE,TRUE,FALSE,TRUE
sample6,TRUE,TRUE,FALSE,FALSE,FALSE
sample7,TRUE,TRUE,TRUE,FALSE,TRUE
sample8,TRUE,TRUE,TRUE,TRUE,TRUE
sample9,FALSE,TRUE,TRUE,FALSE,TRUE
sample10,TRUE,TRUE,FALSE,FALSE,TRUE
And I need to generate a frequency vs total count bar plot similar to the following figure in python. Its a combination of 3 plots so I guess you need to plot them independently and put them in a single canvas. I frequently see this plot in journals so I guess it should be implemented already. However, I did not have any success with online search. Does anybody know how it can be done? Thanks.
It can be done easily using UpSetPlot
https://pypi.org/project/UpSetPlot/
I'm using the matplotlib Python library to graph information about text. On my y-axis, I have a word, and on my x-axis, I have the number of times that word appears in the text. The thing is, with large pieces of text, the number of unique of words becomes undisplayable on a screen.
I'm currently using the PyCharm IDE and there is a helpful tool which shows my bar graph in its entirety, just zoomed out a lot. I can zoom into this graph and see all my data nicely.
My question is if there is a way to do such a thing with matplotlib. That is, make a graph such that in the case that there are too many words to display, it resizes and zooms out so that all the words do not overlap one another.
If there are any other means to display data such as mine in a better way, I would highly appreciate any suggestions. Thanks!
You can increase the figure size before plotting.
import matplotlib.pyplot as plt
plt.figure(figsize=(X, Y)
I'm trying to create a matplotlib graph that shows how to a certain data set changes over time. What I've been trying to do is create a plot and show it, pause for one second, clear the plot, and then show the next one in the array. I've gotten pretty close with the code below, but sadly it just crashes as is.
for expo in sorted_data:
plt.plot(expo["x"], expo["y"])
plt.show(block=False)
time.sleep(1)
plt.gcf().clear()
sorted_data contains the data sorted by when the data was collected.
Use matplotlib.animation. You can find many examples here: http://matplotlib.org/examples/animation/index.html
Claim 1: no precomputed array
Claim 2: no set_ydata
I have read answer from Dynamically updating plot in matplotlib and How to update a plot in matplotlib? and their scenarios are different.
Case 0, you are iteratively solving large linear algebra problem, how can you monitor convergence of the solution at real time -- so you can stop the calculation once the solution shows signs of explosion and save waiting time?
Case 1, you have a two dimensional array and you are changing value at random (i, j), you can think of this as ising model or percolation model or whatever model you are familar with. How to show the evolution of the array?
Case 2, you have a triangular mesh and you are doing depth-first search of the nodes and you want to mark the path, or you are doing something simpler: tracing the mesh boundaries using half-edge data structure. A visualization at real time is of massive help for checking correctness of the algorithm implementation.
Enough background, the 0th one can be solved by the set_ydata method if your number of iterations is not large (or you don't care about memory), but case 1 and 2? I have no hint. Currently my 'solution' is to save hundreds of figures on disk. I've tried to just plot one figure and update it, but matplotlib seems prefer waiting for everything is done and plot everything at once. Can anybody tell me how to dynamically update a figure? Thank you very much!
Test code is below (currently it only plots once in the end):
from numpy import *
import matplotlib.pyplot as plt
from time import sleep
M = zeros((5,5))
fig = plt.figure(1)
for n in range(10):
i = random.randint(5)
j = random.randint(5)
print 'open site at ', i, j
M[i, j] = 1
plt.imshow(M, 'gray', interpolation = 'nearest')
plt.plot(j, i, 'ro') # also mark open site (I have reason to do this)
fig.canvas.draw()
sleep(0.3)
plt.show()
I just found the answer after finding this question: Matplotlib ion() function fails to be interactive
The code above can be made 'real-time' just by replacing the sleep(0.3) with plt.pause(0.3), no other change is necessary. This is quite unexpected and I never used plt.pause() function before, but it simply works.
I while ago, I was comparing the output of two functions using python and matplotlib. The result was as good as simple, since plotting with matplotlib is quite easy: I just plotted two arrays with different markers. Piece of cake.
Now I find myself with the same problem, but now I have a lot of pair of curves to compare. I initially tried plotting everything with different colors and markers. This did not satisfy me since the ranges of each curve are not quite the same. In addition to this, I quickly ran out of colors and markers that were sufficiently different to identify (RGBCMYK, after that, custom colors resemble any of the previous ones).
I also tried subplotting each pair of curves, obtaining a window with many plots. Too crowded.
I tried one window per plot, too many windows.
So I was just wondering if there is any existing widget or if you have any suggestion (or a different idea) to accomplish this:
I want to see a pair of curves and then select easily the next one, with a slidebar, button, mouse scroll, or any other widget or event. By changing curves, the previous one should disappear, the legend should change and its axis as well.
Well I managed to do it with an event handler for mouse clicks. I will change it for something more useful, but I post my solution anyway.
import matplotlib.pyplot as plt
figure = plt.figure()
# plotting
plt.plot([1,2,3],[10,20,30],'bo-')
plt.grid()
plt.legend()
def on_press(event):
print 'you pressed', event.button, event.xdata, event.ydata
event.canvas.figure.clear()
# select new curves to plot, in this example [1,2,3] [0,0,0]
event.canvas.figure.gca().plot([1,2,3],[0,0,0], 'ro-')
event.canvas.figure.gca().grid()
event.canvas.figure.gca().legend()
event.canvas.draw()
figure.canvas.mpl_connect('button_press_event', on_press)
Sounds like you want to embed matplotlib in an application. There are some resources available for that:
user interface examples
Embedding in WX
I really like using traits. If you follow the tutorial Writing a graphical application for scientific programming , you should be able to do what you want. The tutorial shows how to interact with a matplotlib graph using graphical user interface.