I have some plots generated by matplotlib, which I have been generating using the same code (for different data) for several years. The plots always come out looking normal. This time, I re-ran the same code using different data, and the plots showed up like
(Note that the missing border at the top is the result of my erasing the legend. The problem is the choppy, broken up blue and orange data lines.) Many of the plots generated are broken up like this, but not all are. All were normal the previous times that I ran this code.
I have no idea what's going on to make the lines all broken up like this. I tried re-starting my computer and it didn't help. I can't seem to find anything online about this issue. Does anyone have any idea what's going on here?
Here's the code that generated it (excerpted)
`
import matplotlib
from numpy import NaN
matplotlib.use('Agg')
from matplotlib import pyplot as plt
from PyPDF2 import PdfFileMerger, PdfFileReader
for chan in channels:
dProd.plot(x='Date',y=[a_chan,b_chan],secondary_y=b_chan)
plt.title(chan).get_figure().savefig(folder+'\\'+prod+'\\plots\\'+chan+'.pdf')
plt.close('all')
`
Related
I am working on a Coursera cert and I noticed that the plots in their example labs and assignments come out like this:
I would like to do a couple things. One, if possible(but might not be necessary), to remove the legend "figure 1", the on/off symbol and the buttons at the bottom of the image. By the way, I really don't understand where this feature comes from, I hope someone can explain. Two, the main thing I want to fix is making all the labels(ticks) in the y-axis visible, i.e. move them towards the right.
The Python version in their Jupyter notebook is:
from platform import python_version
print(python_version())
3.6.2
The imports I am working with:
%matplotlib notebook
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings('ignore')
And the code that generated the plot:
categ_series.plot(kind='barh', figsize=(8, 6))
plt.show()
I've been trying to change the tick label of my axis. I've imported them from a csv, and have changed the label, but I can't change the tick. I'm just starting out, so if I have any of the terms wrong just let me know. My code is outlined below.
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv ('countries.csv')
uk = data[data.ISO_3_CODE == "GBR"]
plt.plot(uk.date_epicrv)
plt.plot(uk.CumCase)
plt.xlabel('Time')
plt.ylabel("Cumulative infection rate")
plt.show
The csv can be found at https://data.humdata.org/dataset/coronavirus-covid-19-cases-and-deaths if you need it. Sorry I couldn't be more specific, trying to learn a new skill and I believe I'm using the wrong terms to search.
Well, I feel like a fool. I was formatting my graph wrong.
Where I had
plt.plot(uk.date_epicrv)
plt.plot(uk.CumCase)
I should have used
plt.plot(uk.date_epicrv, uk.CumCase)
I was trying to plot both graphs on the x-axis. Closing the question now, hope this helps anyone with the issue down the line.
I am creating a bar chart with seaborn, and it's not generating any sort of error, but nothing happens either.
This is the code I have:
import pandas
import numpy
import matplotlib.pyplot as plt
import seaborn
data = pandas.read_csv('fy15crime.csv', low_memory = False)
seaborn.countplot(x="primary_type", data=data)
plt.xlabel('crime')
plt.ylabel('amount')
seaborn.plt.show()
I added "seaborn.plt.show() in an effort to have it show up, but it isn't working still.
You should place this line somewhere in the top cell in Jupyter to enable inline plotting:
%matplotlib inline
It's simply plt.show() you were close. No need for seaborn
I was using PyCharm using a standard Python file and I had the best luck with the following:
Move code to a Jupyter notebook (which can you do inside of PyCharm by right clicking on the project and choosing new - Jupyter Notebook)
If running a chart that takes a lot of processing time it might not have been obvious before, but in Jupyter mode you can easily see when the cell has finished processing.
when I use inline plots in iPython (QtConsole), the first plot looks (more or less) fine, but then it gets weirder and weirder. When I plot something several times (so plot, see it displayed, plot again, see output etc.), it looks like it is being overlaid with the skewed previous picture. So after plotting a diagonal line (x=y) 4 times in a row I get something like this
If i right click and export it as svg everything looks good
(Exported PNG picture remains wrecked as the first one).
I guess the problem is similar to https://github.com/ipython/ipython/issues/1866, but I didn't got the upshot of the discussion (it got too technical and complicated for me to follow).
Is there any solution or work around for this issue?
I'm using
python 2.7
matplotlib 1.4.1
IPython 2.1.0
Here is a working example:
%matplotlib inline
% config InlineBackend.figure_format = 'svg'
import matplotlib.pyplot as plt
a=range(10)
fig,ax=plt.subplots()
ax.plot(a,a)
ax.axis('off')
if you remove plt.axis('off') line, weird things happen only outside of the axis box.
P.S. Originally I encountered this problem in connection with drawing graphs with networkx. If I use draw from networkx this problem does not occur. If I use draw_networkx, same as described above happens. That might point to the core of the problem... I'm trying to figure out what line of code makes one work better than the other...
After tinkering around with the draw and draw_networkx functions from networkx module, I found the workaround which makes the difference between draw and draw_networkx in this case.
Adding fig.set_facecolor('w') overlays whatever is in the background, so the new plots are started with a white sheet (but not a blank one, I guess).
So new working example is:
%matplotlib inline
% config InlineBackend.figure_format = 'svg'
import matplotlib.pyplot as plt
a=range(10)
fig,ax=plt.subplots()
fig.set_facecolor('w')
ax.plot(a,a)
ax.axis('off')
I am using matplotlib to graph out some data in which takes time over a time, therefore I have to use plot_date in order to plot my lines. But for some reason Plot_Date and Plot have completely different formatting as far as connecting lines.
Here is what It looks like when using plot(x,y)
Using plot(x,y,'bo') or plot_date(x,y,'bo')
Plot_date(x,y) looks like that ^^ too.
and using plot_date(x,y,'bo-')
How do I make it so that the result of plot_date looks like the first picture? I have looked all over the Matplotlib website and couldn't find anything.
Thanks in advance
Upon further investigation I found that in order to display a solid line without dots, I needed to use the line style 'b-', making the code plot_date(x,y,'b-').
If you want to have the markers connected with the lines, you can also use
plt.plot_date(y,x,linestyle='solid')
So the complete code for the beginners would be:
from matplotlib import pyplot as plt
plt.plot_date(y,x,linestyle='solid')
plt.show()
Do not forget you need to have matplotlib installed first. For that you need to open command line and write:
pip install matplotlib