I want to connect airplanes in origin (lat_1 lon_1) to dest(lat_2 lon_2). I use these data.
callsign
latitude_1
longitude_1
latitude_2
longitude_2
0
HBAL102
-4.82114
-76.3194
-4.5249
-79.0103
1
AUA1028
-33.9635
151.181
48.1174
16.55
2
ABW120
41.9659
-87.8832
55.9835
37.4958
3
CSN461
33.9363
-118.414
50.0357
8.5723
4
ETH3730
25.3864
55.4221
50.6342
5.43903
But unfortunately, I would get an incorrect result when creating LineString with shapely. I used everything like rotate and affine but it didn't correct.
Code:
cols = pd.read_csv("/content/dirct_lines.csv",sep=";")
line = cols[["callsign","latitude_1","longitude_1","latitude_2","longitude_2"]].dropna()
line['geometry'] = line.apply(lambda x: [(x['latitude_1'],
x['longitude_1']),
(x['latitude_2'],
x['longitude_2'])], axis = 1)
geoline = gpd.GeoDataFrame(line,geometry="geometry",
crs="EPSG:4326")
import matplotlib.pyplot as plt
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
ax = world.plot(figsize=(14,9),
color='white', edgecolor='black')
geoline.plot(figsize=(14,9),ax=ax,facecolor = 'lightgrey', linewidth = 1.75,
edgecolor = 'red',
alpha = 2)
plt.show()
Shapely Output:
something that was interesting for me was that when I use Matplotlib to create lines everything is correct.
Code:
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(projection=ccrs.PlateCarree())
ax.stock_img()
org_lon, org_lat = cols["longitude_1"], cols["latitude_1"]
dst_lon, dst_lat = cols["longitude_2"], cols["latitude_2"]
plt.plot([org_lon, dst_lon], [org_lat, dst_lat],
color='black', linewidth=0.5, marker='_',
transform=ccrs.PlateCarree()
)
plt.savefig(f"fight_path.png",dpi=60,facecolor = None, bbox_inches = 'tight', pad_inches = None)
plt.show()
Matplotlib Output:
What is the problem?
why isn't correct by shapely?
it's just the way you are creating the geometry. Below works correctly.
import io
import geopandas as gpd
import pandas as pd
import shapely.geometry
df = pd.read_csv(
io.StringIO(
"""callsign,latitude_1,longitude_1,latitude_2,longitude_2
HBAL102,-4.82114,-76.3194,-4.5249,-79.0103
AUA1028,-33.9635,151.181,48.1174,16.55
ABW120,41.9659,-87.8832,55.9835,37.4958
CSN461,33.9363,-118.414,50.0357,8.5723
ETH3730,25.3864,55.4221,50.6342,5.43903
"""
)
)
geoline = gpd.GeoDataFrame(
geometry=[
shapely.geometry.LineString(points)
for points in zip(
gpd.points_from_xy(df["longitude_1"], df["latitude_1"]),
gpd.points_from_xy(df["longitude_2"], df["latitude_2"]),
)
],
data=df,
)
import matplotlib.pyplot as plt
world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
ax = world.plot(figsize=(14, 9), color="white", edgecolor="black")
geoline.plot(
figsize=(14, 9),
ax=ax,
facecolor="lightgrey",
linewidth=1.75,
edgecolor="red",
)
plt.show()
Does anyone know how to show the labels of the minor ticks on a logarithmic scale with Python/Matplotlib?
You can use plt.tick_params(axis='y', which='minor') to set the minor ticks on and format them with the matplotlib.ticker FormatStrFormatter. For example,
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter
x = np.linspace(0,4,1000)
y = np.exp(x)
plt.plot(x, y)
ax = plt.gca()
ax.set_yscale('log')
plt.tick_params(axis='y', which='minor')
ax.yaxis.set_minor_formatter(FormatStrFormatter("%.1f"))
plt.show()
One option is to use matplotlib.ticker.LogLocator
import numpy
import pylab
import matplotlib.pyplot
import matplotlib.ticker
## setup styles
from matplotlib import rc
rc('font', **{'family': 'sans-serif', 'sans-serif': ['Times-Roman']})
rc('text', usetex = True)
matplotlib.rcParams['text.latex.preamble'] = [r"\usepackage{amsmath}"]
## make figure
figure, ax = matplotlib.pyplot.subplots(1, sharex = True, squeeze = True)
x = numpy.linspace(0.0, 20.0, 1000)
y = numpy.exp(x)
ax.plot(x, y)
ax.set_yscale('log')
## set y ticks
y_major = matplotlib.ticker.LogLocator(base = 10.0, numticks = 5)
ax.yaxis.set_major_locator(y_major)
y_minor = matplotlib.ticker.LogLocator(base = 10.0, subs = numpy.arange(1.0, 10.0) * 0.1, numticks = 10)
ax.yaxis.set_minor_locator(y_minor)
ax.yaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())
## save figure
pylab.tight_layout()
pylab.savefig('./test.png', dpi = 200)
you would get
the only thing you need to manually adjust is the numticks input for both major and minor ticks, they both have to be a fraction of total possible number of major ticks.
I have a code that should produce two different graphs and place them into one image and cannot figure out why it returns the last mentioned graph twice. The code is as follows:
import spacepy as sp
from spacepy import pycdf
from pylab import *
from spacepy.toolbox import windowMean, normalize
from spacepy.plot.utils import annotate_xaxis
import pylab
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.cbook as cbook
import matplotlib.ticker as ticker
from matplotlib.colors import LogNorm
from matplotlib.ticker import LogLocator
from matplotlib.dates import DateFormatter
from matplotlib.dates import DayLocator, HourLocator, MinuteLocator
from matplotlib import rc, rcParams
import matplotlib.dates as mdates
import datetime as dt
import bisect as bi
import seaborn as sea
import sys
import os
import multilabel as ml
import pandas as pd
sea.set_context('poster')
# sea.set_style('ticks',{'axes.facecolor':'yellow'})
sea.set_style('whitegrid')
sea.set_palette('muted',color_codes=True)
rc('text', usetex=True)
rc('font', family='Mono')
rcParams['text.latex.preamble']=[r'\usepackage{amsmath}']
MMS_1_HPCA_SURVEY_ION = pycdf.CDF(r'/home/ary/Desktop/Arya/Project/Data/MMS/1/HPCA/Survey/Ion/mms1_hpca_srvy_l2_ion_20151025120000_v1.0.0.cdf')
EPOCH_SURVEY_ION_1 = MMS_1_HPCA_SURVEY_ION['Epoch'][...]
H_Flux_SURVEY_ION_1 = MMS_1_HPCA_SURVEY_ION['mms1_hpca_hplus_flux'][...]
O_Flux_SURVEY_ION_1 = MMS_1_HPCA_SURVEY_ION['mms1_hpca_oplus_flux'][...]
Ion_Energy_SURVEY_ION_1 = MMS_1_HPCA_SURVEY_ION['mms1_hpca_ion_energy'][...]
MMS_SURVEY_ION_1_Start_time = dt.datetime(2015, 10, 25, 12, 0, 0, 908117)
MMS_SURVEY_ION_1_Finish_time = dt.datetime(2015, 10, 25, 16, 22, 24, 403623)
dt_MMS = dt.timedelta(seconds = 15)
plt.close('all')
fig_MMS, axs_MMS = plt.subplots(2,sharex=True)
cmap = plt.get_cmap(cm.jet)
cmap.set_bad('black')
sidx_MMS_1_SURVEY_ION = bi.bisect_left(EPOCH_SURVEY_ION_1,MMS_SURVEY_ION_1_Start_time)
sidx_MMS_1_SURVEY_ION = int(sidx_MMS_1_SURVEY_ION-(sidx_MMS_1_SURVEY_ION/100))
lidx_MMS_1_SURVEY_ION = bi.bisect_left(EPOCH_SURVEY_ION_1, MMS_SURVEY_ION_1_Finish_time)
lidx_MMS_1_SURVEY_ION = int(lidx_MMS_1_SURVEY_ION+((len(EPOCH_SURVEY_ION_1)-lidx_MMS_1_SURVEY_ION)/100))
if MMS_SURVEY_ION_1_Start_time.date() == MMS_SURVEY_ION_1_Finish_time.date():
stopfmt = '%H:%M'
else:
stopfmt = '%-m/%-d/%y %H:%M'
title_1 = MMS_SURVEY_ION_1_Start_time.strftime('%m/%d/%y %H:%M')+' -'+MMS_SURVEY_ION_1_Finish_time.strftime(stopfmt)
if dt_MMS.seconds !=0:
title_1 = title_1 + ' with '+str(dt_MMS.seconds)+' second time averaging'
for j, ax in enumerate(axs_MMS.T.flatten()):
flix_1 = np.array(H_Flux_SURVEY_ION_1[sidx_MMS_1_SURVEY_ION:lidx_MMS_1_SURVEY_ION,
j, :].T)
if dt_MMS==dt.timedelta(0):
fluxwin_1 = flix_1
timewin_1 = EPOCH_SURVEY_ION_1[sidx_MMS_1_SURVEY_ION:lidx_MMS_1_SURVEY_ION]
else:
fluxwin_1=[[0 for y in range(len(flix_1))] for x_1 in range(len(flix_1))]
for i, flox in enumerate(flix_1):
fluxwin_1[i], timewin_1 = windowMean(flox, EPOCH_SURVEY_ION_1[sidx_MMS_1_SURVEY_ION:lidx_MMS_1_SURVEY_ION],
winsize=dt_MMS, overlap=dt.timedelta(0))
fluxwin_1[i] = np.array(fluxwin_1[i])
for x_1 in np.where(np.diff(EPOCH_SURVEY_ION_1[sidx_MMS_1_SURVEY_ION:lidx_MMS_1_SURVEY_ION])
>dt.timedelta(hours=1))[0]+sidx_MMS_1_SURVEY_ION:
fluxwin_1[i][bi.bisect_right(timewin_1, EPOCH_SURVEY_ION_1[x_1]):bi.bisect_right(timewin_1,
EPOCH_SURVEY_ION_1[x_1+1])]=0
fluxwin_1 = np.array(fluxwin_1)
fluxwin_1[np.where(fluxwin_1<=0)] = 0
x_1 = mdates.date2num(timewin_1)
pax_1 = ax.pcolormesh(x_1, Ion_Energy_SURVEY_ION_1, fluxwin_1, shading='turkey',cmap=cmap, vmin=1,
vmax=np.nanmax(H_Flux_SURVEY_ION_1[sidx_MMS_1_SURVEY_ION:lidx_MMS_1_SURVEY_ION,:,:]),
norm=LogNorm())
sax_1 = ax.twinx()
plt.setp(sax_1.get_yticklabels(), visible=False)
sax_1.tick_params(axis='y', right='off')
ax.set_xlim(MMS_SURVEY_ION_1_Start_time, MMS_SURVEY_ION_1_Finish_time)
ax.set_yscale('log')
ax.set_yticks([10, 100, 1000,10000])
#Allows non-log formatted values to be used for ticks
ax.yaxis.set_major_formatter(plt.ScalarFormatter())
axs_MMS[0].set_ylabel('Energy (eV)')
axs_MMS[0].set_title(title_1)
for j, ax in enumerate(axs_MMS.T.flatten()):
flix_2 = np.array(O_Flux_SURVEY_ION_1[sidx_MMS_1_SURVEY_ION:lidx_MMS_1_SURVEY_ION,
j, :].T)
if dt_MMS==dt.timedelta(0):
fluxwin_2 = flix_2
timewin_2 = EPOCH_SURVEY_ION_2[sidx_MMS_1_SURVEY_ION:lidx_MMS_1_SURVEY_ION]
else:
fluxwin_2=[[0 for y in range(len(flix_2))] for x_2 in range(len(flix_2))]
for i, flox in enumerate(flix_2):
fluxwin_2[i], timewin_2 = windowMean(flox, EPOCH_SURVEY_ION_1[sidx_MMS_1_SURVEY_ION:lidx_MMS_1_SURVEY_ION],
winsize=dt_MMS, overlap=dt.timedelta(0))
fluxwin_2[i] = np.array(fluxwin_2[i])
for x_2 in np.where(np.diff(EPOCH_SURVEY_ION_1[sidx_MMS_1_SURVEY_ION:lidx_MMS_1_SURVEY_ION])
>dt.timedelta(hours=1))[0]+sidx_MMS_1_SURVEY_ION:
fluxwin_2[i][bi.bisect_right(timewin_2, EPOCH_SURVEY_ION_1[x_2]):bi.bisect_right(timewin_2,
EPOCH_SURVEY_ION_1[x_1+1])]=0
fluxwin_2 = np.array(fluxwin_2)
fluxwin_2[np.where(fluxwin_2<=0)] = 0
x_2 = mdates.date2num(timewin_2)
pax_2 = ax.pcolormesh(x_2, Ion_Energy_SURVEY_ION_1, fluxwin_2, shading='turkey',cmap=cmap, vmin=1,
vmax=np.nanmax(O_Flux_SURVEY_ION_1[sidx_MMS_1_SURVEY_ION:lidx_MMS_1_SURVEY_ION,:,:]),
norm=LogNorm())
sax_2 = ax.twinx()
plt.setp(sax_2.get_yticklabels(), visible=False)
sax_2.tick_params(axis='y', right='off')
ax.set_xlim(MMS_SURVEY_ION_1_Start_time, MMS_SURVEY_ION_1_Finish_time)
ax.set_yscale('log')
ax.set_yticks([10, 100, 1000,10000])
#Allows non-log formatted values to be used for ticks
ax.yaxis.set_major_formatter(plt.ScalarFormatter())
axs_MMS[1].set_ylabel('Energy (eV)')
cbar_ax_1 = fig_MMS.add_axes([0.93, 0.15, 0.02, 0.7])
cb_MMS_1 = fig_MMS.colorbar(pax_1, cax=cbar_ax_1)
cb_MMS_1.set_label(r'Counts sec$^{-1}$ ster$^{-1}$ cm$^{-2}$ keV$^{-1}$')
#Sets the colorbar value range
cb_MMS_1.set_clim(1, np.nanmax(H_Flux_SURVEY_ION_1[sidx_MMS_1_SURVEY_ION:lidx_MMS_1_SURVEY_ION,:,:]))
#Redraws the colorbar
cb_MMS_1.draw_all()
and the image returned looks as such:
enter image description here
Consider the following example which corresponds to your code:
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2,sharex=True, figsize=(4,2.4))
for j, ax in enumerate(axs.T.flatten()):
x_1 = [[0,1,2],[0,1,2],[0,1,2]]
y_1 = [[0,0,0],[1,1,1],[2,2,2]]
z_1 = [[6,5,4],[2,3,4],[6,5,4]]
pax_1 = ax.pcolormesh(x_1, y_1, z_1)
axs[0].set_ylabel('Energy (eV)')
axs[0].set_title("Title1")
for j, ax in enumerate(axs.T.flatten()):
x_2 = [[3,4,5],[3,4,5],[3,4,5]]
y_2 = [[0,0,0],[1,1,1],[2,2,2]]
z_2 = [[2,1,2],[2,1,2],[2,1,2]]
pax_2 = ax.pcolormesh(x_2, y_2, z_2)
axs[1].set_ylabel('Energy (eV)')
plt.show()
Here you plot each plot both axes.
Instead you need to plot to the different axes:
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2,sharex=True, figsize=(4,2.4))
x_1 = [[0,1,2],[0,1,2],[0,1,2]]
y_1 = [[0,0,0],[1,1,1],[2,2,2]]
z_1 = [[6,5,4],[2,3,4],[6,5,4]]
pax_1 = axs[0].pcolormesh(x_1, y_1, z_1)
axs[0].set_ylabel('Energy (eV)')
axs[0].set_title("Title1")
x_2 = [[3,4,5],[3,4,5],[3,4,5]]
y_2 = [[0,0,0],[1,1,1],[2,2,2]]
z_2 = [[2,1,2],[2,1,2],[2,1,2]]
pax_2 = axs[1].pcolormesh(x_2, y_2, z_2)
axs[1].set_ylabel('Energy (eV)')
plt.show()
I want to draw a scatter trend line on matplot. How can I do that?
Python
import pandas as pd
import matplotlib.pyplot as plt
csv = pd.read_csv('/tmp/test.csv')
data = csv[['fee', 'time']]
x = data['fee']
y = data['time']
plt.scatter(x, y)
plt.show()
CSV
fee,time
100,650
90,700
80,860
70,800
60,1000
50,1200
time is integer value.
Scatter chart
I'm sorry I found the answer by myself.
How to add trendline in python matplotlib dot (scatter) graphs?
Python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
csv = pd.read_csv('/tmp/test.csv')
data = csv[['fee', 'time']]
x = data['fee']
y = data['time']
plt.scatter(x, y)
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
plt.plot(x,p(x),"r--")
plt.show()
Chart
With text:
from sklearn.metrics import r2_score
plt.plot(x,y,"+", ms=10, mec="k")
z = np.polyfit(x, y, 1)
y_hat = np.poly1d(z)(x)
plt.plot(x, y_hat, "r--", lw=1)
text = f"$y={z[0]:0.3f}\;x{z[1]:+0.3f}$\n$R^2 = {r2_score(y,y_hat):0.3f}$"
plt.gca().text(0.05, 0.95, text,transform=plt.gca().transAxes,
fontsize=14, verticalalignment='top')
You also can use Seaborn lmplot:
import seaborn as sns
import pandas as pd
from io import StringIO
textfile = StringIO("""fee,time
100,650
90,700
80,860
70,800
60,1000
50,1200""")
df = pd.read_csv(textfile)
_ = sns.lmplot(x='fee', y='time', data=df, ci=None)
Output: