自学气象人 发表于 2024-3-17 00:08:45

50个常用统计图表代码总结(10)


46. Seasonal Plot
The seasonal plot can be used to compare how the time series performed at same day in the previous season (year / month / week etc).
from dateutil.parser import parse

# Import Data
df = pd.read_csv('https://github.com/selva86/datasets/raw/master/AirPassengers.csv')

# Prepare data
df['year']=
df['month']=
years = df['year'].unique()

# Draw Plot
mycolors =['tab:red','tab:blue','tab:green','tab:orange','tab:brown','tab:grey','tab:pink','tab:olive','deeppink','steelblue','firebrick','mediumseagreen']      
plt.figure(figsize=(16,10), dpi=80)

for i, y in enumerate(years):
    plt.plot('month','traffic', data=df.loc, color=mycolors<i>, label=y)</i>
<i>    plt.text(df.loc.shape-.9, df.loc[-1:].values, y, fontsize=12, color=mycolors)</i>

# Decoration
plt.ylim(50,750)
plt.xlim(-0.3,11)
plt.ylabel('$Air Traffic$')
plt.yticks(fontsize=12, alpha=.7)
plt.title("Monthly Seasonal Plot: Air Passengers Traffic (1949 - 1969)", fontsize=22)
plt.grid(axis='y', alpha=.3)

# Remove borders
plt.gca().spines["top"].set_alpha(0.0)   
plt.gca().spines["bottom"].set_alpha(0.5)
plt.gca().spines["right"].set_alpha(0.0)   
plt.gca().spines["left"].set_alpha(0.5)   
# plt.legend(loc='upper right', ncol=2, fontsize=12)
plt.show()



Groups
47. Dendrogram
A Dendrogram groups similar points together based on a given distance metric and organizes them in tree like links based on the point’s similarity.
import scipy.cluster.hierarchy as shc

# Import Data
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/USArrests.csv')

# Plot
plt.figure(figsize=(16,10), dpi=80)
plt.title("USArrests Dendograms", fontsize=22)
dend = shc.dendrogram(shc.linkage(df[['Murder','Assault','UrbanPop','Rape']], method='ward'), labels=df.State.values, color_threshold=100)
plt.xticks(fontsize=12)
plt.show()



48. Cluster Plot
Cluster Plot canbe used to demarcate points that belong to the same cluster. Below is a representational example to group the US states into 5 groups based on the USArrests dataset. This cluster plot uses the ‘murder’ and ‘assault’ columns as X and Y axis. Alternately you can use the first to principal components as rthe X and Y axis.
from sklearn.cluster importAgglomerativeClustering
from scipy.spatial importConvexHull

# Import Data
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/USArrests.csv')

# Agglomerative Clustering
cluster =AgglomerativeClustering(n_clusters=5, affinity='euclidean', linkage='ward')
cluster.fit_predict(df[['Murder','Assault','UrbanPop','Rape']])

# Plot
plt.figure(figsize=(14,10), dpi=80)
plt.scatter(df.iloc[:,0], df.iloc[:,1], c=cluster.labels_, cmap='tab10')

# Encircle
def encircle(x,y, ax=None,**kw):
    ifnot ax: ax=plt.gca()
    p = np.c_
    hull =ConvexHull(p)
    poly = plt.Polygon(p,**kw)
    ax.add_patch(poly)

# Draw polygon surrounding vertices   
encircle(df.loc, df.loc, ec="k", fc="gold", alpha=0.2, linewidth=0)
encircle(df.loc, df.loc, ec="k", fc="tab:blue", alpha=0.2, linewidth=0)
encircle(df.loc, df.loc, ec="k", fc="tab:red", alpha=0.2, linewidth=0)
encircle(df.loc, df.loc, ec="k", fc="tab:green", alpha=0.2, linewidth=0)
encircle(df.loc, df.loc, ec="k", fc="tab:orange", alpha=0.2, linewidth=0)

# Decorations
plt.xlabel('Murder'); plt.xticks(fontsize=12)
plt.ylabel('Assault'); plt.yticks(fontsize=12)
plt.title('Agglomerative Clustering of USArrests (5 Groups)', fontsize=22)
plt.show()




49. Andrews Curve
Andrews Curve helps visualize if there are inherent groupings of the numerical features based on a given grouping. If the features (columns in the dataset) doesn’t help discriminate the group (cyl), then the lines will not be well segregated as you see below.
from pandas.plotting import andrews_curves

# Import
df = pd.read_csv("https://github.com/selva86/datasets/raw/master/mtcars.csv")
df.drop(['cars','carname'], axis=1, inplace=True)

# Plot
plt.figure(figsize=(12,9), dpi=80)
andrews_curves(df,'cyl', colormap='Set1')

# Lighten borders
plt.gca().spines["top"].set_alpha(0)
plt.gca().spines["bottom"].set_alpha(.3)
plt.gca().spines["right"].set_alpha(0)
plt.gca().spines["left"].set_alpha(.3)

plt.title('Andrews Curves of mtcars', fontsize=22)
plt.xlim(-3,3)
plt.grid(alpha=0.3)
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
plt.show()



50. Parallel Coordinates
Parallel coordinates helps to visualize if a feature helps to segregate the groups effectively. If a segregation is effected, that feature is likely going to be very useful in predicting that group.
from pandas.plotting import parallel_coordinates

# Import Data
df_final = pd.read_csv("https://raw.githubusercontent.com/selva86/datasets/master/diamonds_filter.csv")

# Plot
plt.figure(figsize=(12,9), dpi=80)
parallel_coordinates(df_final,'cut', colormap='Dark2')

# Lighten borders
plt.gca().spines["top"].set_alpha(0)
plt.gca().spines["bottom"].set_alpha(.3)
plt.gca().spines["right"].set_alpha(0)
plt.gca().spines["left"].set_alpha(.3)

plt.title('Parallel Coordinated of Diamonds', fontsize=22)
plt.grid(alpha=0.3)
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
plt.show()




参考

https://www.machinelearningplus.com/plots/top-50-matplotlib-visualizations-the-master-plots-python/#3.-Scatter-plot-with-linear-regression-line-of-best-fit





文章来源于微信公众号:自学气象人

星星窗帘布艺 发表于 2024-4-27 11:26:49

能分享一下这个项目的更多细节吗?

Gregoryroata 发表于 2 小时前

audiobookkeeper cottagenet eyesvision eyesvisions factoringfee filmzones gadwall gaffertape gageboard gagrule gallduct galvanometric gangforeman gangwayplatform garbagechute gardeningleave gascautery gashbucket gasreturn gatedsweep gaugemodel gaussianfilter gearpitchdiameter geartreating generalizedanalysis generalprovisions geophysicalprobe geriatricnurse getintoaflap
getthebounce habeascorpus habituate hackedbolt hackworker hadronicannihilation haemagglutinin hailsquall hairysphere halforderfringe halfsiblings hallofresidence haltstate handcoding handportedhead handradar handsfreetelephone hangonpart haphazardwinding hardalloyteeth hardasiron hardenedconcrete harmonicinteraction hartlaubgoose hatchholddown haveafinetime hazardousatmosphere headregulator heartofgold heatageingresistance
heatinggas heavydutymetalcutting jacketedwall japanesecedar jibtypecrane jobabandonment jobstress jogformation jointcapsule jointsealingmaterial journallubricator juicecatcher junctionofchannels justiciablehomicide juxtapositiontwin kaposidisease keepagoodoffing keepsmthinhand kentishglory kerbweight kerrrotation keymanassurance keyserum kickplate killthefattedcalf kilowattsecond kingweakfish kinozones kleinbottle kneejoint
knifesethouse knockonatom knowledgestate kondoferromagnet labeledgraph laborracket labourearnings labourleasing laburnumtree lacingcourse lacrimalpoint lactogenicfactor lacunarycoefficient ladletreatediron laggingload laissezaller lambdatransition laminatedmaterial lammasshoot lamphouse lancecorporal lancingdie landingdoor landmarksensor landreform landuseratio languagelaboratory largeheart lasercalibration laserlens
laserpulse laterevent latrinesergeant layabout leadcoating leadingfirm learningcurve leaveword machinesensible magneticequator magnetotelluricfield mailinghouse majorconcern mammasdarling managerialstaff manipulatinghand manualchoke medinfobooks mp3lists nameresolution naphtheneseries narrowmouthed nationalcensus naturalfunctor navelseed neatplaster necroticcaries negativefibration neighbouringrights objectmodule
observationballoon obstructivepatent oceanmining octupolephonon offlinesystem offsetholder olibanumresinoid onesticket packedspheres pagingterminal palatinebones palmberry papercoating paraconvexgroup parasolmonoplane parkingbrake partfamily partialmajorant quadrupleworm qualitybooster quasimoney quenchedspark quodrecuperet rabbetledge radialchaser radiationestimator railwaybridge randomcoloration rapidgrowth rattlesnakemaster
reachthroughregion readingmagnifier rearchain recessioncone recordedassignment rectifiersubstation redemptionvalue reducingflange referenceantigen regeneratedprotein reinvestmentplan safedrilling sagprofile salestypelease samplinginterval satellitehydrology scarcecommodity scrapermat screwingunit seawaterpump secondaryblock secularclergy seismicefficiency selectivediffuser semiasphalticflux semifinishmachining spicetrade spysale stungun tacticaldiameter
tailstockcenter tamecurve tapecorrection tappingchuck taskreasoning technicalgrade telangiectaticlipoma telescopicdamper temperateclimate temperedmeasure tenementbuilding ultramaficrock ultraviolettesting
页: [1]
查看完整版本: 50个常用统计图表代码总结(10)