Matplotlib line plot: data point not displayed - python-3.x

I have an multi-dimensional array of 147637 rows and 175 columns. Now I want to plot only one of the column , i.e, the last column. The last column is filled with 0s in all rows except these 5 rows: 29528, 59051, 88583, 118110, 147636. These rows have a value of 1.
Below is the code that I used to plot:
import matplotlib.pyplot as plt
workBoundary=-1
fig,(ax1)=plt.subplots(nrows=1,ncols=1)
ax1.plot(allPathsDistance[:,workBoundary],color='maroon')
plt.show()
Below is the output:
Notice the x axis. According to this figure there is no '1' value between rows 0 and 60000.
But when I zoom in to the picture:
A '1' valued data point appears at roughly 30000th row as it should.(29528th row, to be exact). I can't figure out why this is happening. Can anyone help rectify this ? I restarted the Spyder IDE but this behavior did not change.
Additional Info:
if the x axis limits are edited to not show 0 as follows:
import matplotlib.pyplot as plt
workBoundary=-1
fig,(ax1)=plt.subplots(nrows=1,ncols=1)
ax1.plot(allPathsDistance[:,workBoundary],color='maroon')
ax1.set_xlim(left=10)
plt.show()
then the line at 30000th row appears. result:
This seems to suggest that the matplotlib is not plotting the data at some of the positions on the plot area.
Version info:
matplotlib: 2.1.0
Spyder: 3.1.4
Windows:7
Python: 3.6.3

If you really have just very sharp spikes that are sparsely distributed in your data, you can use ax.vlines() to make sure they are all visible:
from matplotlib import pyplot as plt
import numpy as np
##setting up the data
allPathsDistance = np.zeros(147637)
allPathsDistance[[29528, 59051, 88583, 118110, 147636]] = 1.0
xvals = np.arange(allPathsDistance.shape[0])
##masking:
mask = allPathsDistance>0
##plotting
fig, ax = plt.subplots()
ax.vlines(xvals[mask], np.zeros_like(xvals[mask]),allPathsDistance[mask])
ax.set_xlim([xvals[0]-1000,xvals[-1]+1000])
plt.show()
...and here is the result:
Hope this helps.

Related

Why does scipy.interpolate not print interpolated values in scatter plot?

I am trying to use scipy linear interpolation to fill gaps in my data so that I can draw a scatter plot of my data.
I also want to print the values changed by linear interpolation, but I am not sure at all how to go about this.
My code does not seem to interpolate at all - the graph it produces has the same gaps as in the original data. When I run my interpolation function f6 for my missing data points, it returns nan just as before. I will paste my code below. (My data goes from 5 minutes to 1440 minutes with 5-minute intervals).
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as interpolate
node_6 = prob_1.iloc[:,1]
t= np.arange(5,1441,5)
f6 = interpolate.interp1d(t,node_6,kind='linear')
xli = np.linspace(5,1440,10000)
y_6 = f6(xli)
plt.scatter(xli, y_6, c='r', s=1)
plt.show()
Screenshot of the graph this code produces
Thanks
Answer - My excel data had missing values which were coming through to python as nans. I needed to remove these nans before interpolating for the interpolation to work.

How to get error bars on barchart PowerBI?

I want to have such barchart:
The error bar on each column should show dispersion (I have it calculated in one of the columns). And top lines show whether there is a significant difference. Right now I have only achieved such graph:
I am using simple clustered barchart in PowerBI Desktop. Maybe there is another visual for that or another program which could do it? Maybe Python somehow?
A mentioned here you can do that with matplotlib from python. Just as an example:
import numpy as np
import pylab as plt
data = np.array(np.random.rand(1000))
y,binEdges = np.histogram(data,bins=10)
bincenters = 0.5*(binEdges[1:]+binEdges[:-1])
menStd = np.sqrt(y)
width = 0.05
plt.bar(bincenters, y, width=width, color='r', yerr=menStd)
plt.show()

How to print percentages outside the pie with python

I would like to have percentage values outside the pie. Maybe you can help
Here is my code :
import matplotlib.pyplot as plt
import pandas as pd
dict={'a':45, 'b': 123, 'c':2, 'd':1755, 'e':13}
ser = pd.Series(dict)
print(ser)
ser.plot(kind='pie', shadow=True, autopct='%1.2f%%')
plt.show()
As you can see in my case percentage values are not visible
According to the docs pctdistance controls the "ratio between the center of each pie slice and the start of the text generated by autopct", and it has a default value of 0.6, which causes the percentage values to be inside the pie.
Try a pctdistance value > 1:
>>> ser.plot(kind='pie', shadow=True, autopct='%1.2f%%', pctdistance=1.15)
(The above results in an "ugly" plot in which the percentages and the labels overlap. You can fix that by "moving" the labels using labeldistance.)

Fixing incorrect contour lines occurring around 0 longitude

I'm fairly new to plotting contour lines. When plotting ice data that crosses over longitude zero in the Arctic, the contour lines create horizontal lines that span the x axis. Ideally I'd merge the lines so they created one solid contour, but failing that just removing the horizontal lines would be enough.
https://imgur.com/VU1IlNA (I'm new and not allowed to post pictures yet, but this shows the problem clearly)
from netCDF4 import Dataset, MFDataset, num2date
import numpy as np
import cartopy.crs as ccrs
from cartopy.util import add_cyclic_point
import pandas as pd
from netCDF4 import Dataset as NetCDFFile
import matplotlib.pyplot as plt
nc = NetCDFFile('LongitudeLatitudeGrid-n3125-Svalbard- from20190129.hdf')
lats = nc.variables['Latitudes'][:]
lons = nc.variables['Longitudes'][:]
nc17 = NetCDFFile('asi-AMSR2-n3125-20190517-v5.4.hdf')
ice17 = nc17.variables['ASI Ice Concentration'][:]
fig = plt.figure(figsize=(30,20))
ax6 = plt.subplot(2,3,6,projection=ccrs.Mercator(min_latitude=77,max_latitude=81))
mm = ax6.contour(lons,lats,ice17,vmin=0,vmax=100,
transform=ccrs.PlateCarree(),cmap='BuPu',zorder=1)
plt.title('May 17th stations: δ15N vaules',size='x-large')
ax6.set_extent([-10,10,77,81])
ax6.coastlines()
Expected results are a clean contour line, with no gap, but instead a gap appears as shown.
I managed to fix this, the issue was that my longitudinal values jumped from 0 to 360 at longitude zero. By subtracting 360 from all longitude values > 180 the problem was solved, and the plot looks appropriate now.

Matplotlib bar plot with table formatting

I have added a table to the bottom of my plot, but there are a number of issues with it:
The right has too much padding.
The left has too little padding.
The bottom has no padding.
The cells are too small for the text within them.
The table is too close to the bottom of the plot.
The cells belonging to the row names are not colored to match those of the bars.
I'm going out of my mind fiddling with this. Can someone help me fix these issues?
Here is the code (Python 3):
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
# Set styles
plt.style.use(['seaborn-paper', 'seaborn-whitegrid'])
plt.style.use(['seaborn'])
sns.set(palette='colorblind')
matplotlib.rc("font", family="Times New Roman", size=12)
labels = ['n=1','n=2','n=3','n=4','n=5']
a = [98.8,98.8,98.8,98.8,98.8]
b = [98.6,97.8,97.0,96.2,95.4]
bar_width = 0.20
data = [a,b]
print(data)
colors = plt.cm.BuPu(np.linspace(0, 0.5, len(labels)))
columns = ('n=1', 'n=2', 'n=3', 'n=4', 'n=5')
index = np.arange(len(labels))
plt.bar(index, a, bar_width)
plt.bar(index+bar_width+.02, b, bar_width)
plt.table(cellText=data,
rowLabels=['a', 'b'],
rowColours=colors,
colLabels=columns,
loc='bottom')
plt.subplots_adjust(bottom=0.7)
plt.ylabel('Some y label which effect the bottom padding!')
plt.xticks([])
plt.title('Some title')
plt.show()
This is the output:
Update
This is working now, but in case someone else is having issues: Make sure you are not viewing your plots and the changes you make to them with IntelliJ SciView as it does not represent changes accurately and introduces some formatting issues!
I think you can fix the first problem by setting the bounding box when you make the table using bbox like this:
bbox=[0, 0.225, 1, 0.2]
where the parameters are [left, bottom, width, height].
For the second issue (the coloring), that is because the color array is not corresponding to the seaborn coloring. You can query the seaborn color palette with
sns.color_palette(palette='colorblind')
this will give you a list of the colors seaborn is using.
Check the modifications below:
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
# Set styles
plt.style.use(['seaborn-paper', 'seaborn-whitegrid'])
plt.style.use(['seaborn'])
sns.set(palette='colorblind')
matplotlib.rc("font", family="Times New Roman", size=12)
labels = ['n=1','n=2','n=3','n=4','n=5']
a = [98.8,98.8,98.8,98.8,98.8]
b = [98.6,97.8,97.0,96.2,95.4]
bar_width = 0.20
data = [a,b]
colors = sns.color_palette(palette='colorblind')
columns = ('n=1', 'n=2', 'n=3', 'n=4', 'n=5')
index = np.arange(len(labels))
fig = plt.figure(figsize=(12,9))
plt.bar(index, a, bar_width)
plt.bar(index+bar_width+.02, b, bar_width)
plt.table(cellText=data,
rowLabels=[' a ', ' b '],
rowColours=colors,
colLabels=columns,
loc='bottom',
bbox=[0, 0.225, 1, 0.2])
fig.subplots_adjust(bottom=0.1)
plt.ylabel('Some y label which effect the bottom padding!')
plt.xticks([])
plt.title('Some title')
plt.show()
I also changed the subplot adjustment to subplot_adjust(bottom=0.1) because it wasn't coming out right otherwise. Here is the output:

Resources