创建更多类别的测试数据 # 模拟不同月份中每天的某个指标(例如:每日平均步数,单位:千步) days = [ f"Day {i}" for i in range ( 1 , 21 )] # 生成 20 天的数据 np.random.seed( 42 ) # 设置随机种子,确保每次运行结果一致 # 生成 1 到 5 之间的随机数值作为示例数据 values = np. round (np.random.uniform( 1.0 , 5.0 , size= len (days)), 1 ) # --- 创建子图 --- fig, (ax1, ax2) = plt.subplots( 2 , 1 , figsize=( 12 , 10 )) # 调整为垂直布局,方便比较 # --- 绘制条形图 (上图) --- bars = ax1.bar(days, values, color= "skyblue" , edgecolor= "navy" , linewidth= 0.7 ) ax1.set_title( "条形图 (Bar Chart) - 每日步数" , fontsize= 14 ) ax1.set_ylabel( "数值 (千步)" ) # ax1.set_xlabel('日期') # x轴标签已在下方图中 ax1.grid(axis= "y" , linestyle= "--" , alpha= 0.7 ) # 在条形图上添加数值标签 for bar, val in zip (bars, values): height = bar.get_height() ax1.text( bar.get_x() + bar.get_width() / 2.0 , height + 0.05 , f" {val}" , ha= "center" , va= "bottom" , fontsize= 8 , ) # 标签旋转90度节省空间 # --- 绘制棒棒糖图 (下图) --- # 1. 绘制线条 ax2.vlines(x= range ( len (days)), ymin= 0 , ymax=values, color= "navy" , linewidth= 2 ) # 2. 在线条顶部绘制圆点 ax2.scatter(x= range ( len (days)), y=values, color= "red" , s= 50 , zorder= 3 ) # 3. 添加数值标签 for i, val in enumerate (values):mf.xiangxiu.net.cn ax2.text(i, val + 0.1 , f" {val}" , ha= "center" , va= "bottom" , fontsize= 8 ) ax2.set_title( "棒棒糖图 (Lollipop Chart) - 每日步数" , fontsize= 14 ) ax2.set_ylabel( "数值 (千步)" ) ax2.set_xlabel( "日期" ) ax2.set_xticks( range ( len (days))) ax2.set_xticklabels(days, rotation= 45 , ha= "right" ) # 旋转x轴标签以便阅读 ax2.grid(axis= "y" , linestyle= "--" , alpha= 0.7 )
"2月" , "3月" , "4月" , "5月" , "6月" , "7月" , "8月" , "9月" , "10月" , "11月" , "12月" , ] # 生成有一定趋势和波动的销售额数据,例如有季节性高峰 sales = [ 120 , 110 , 135 , 140 , 155 , 170 , 185 , 180 , 160 , 150 , 145 , 165 ] # --- 创建子图 --- fig, (ax1, ax2) = plt.subplots( 1 , 2 , figsize=( 16 , 6 )) # --- 绘制条形图 (左图) --- bars = ax1.bar(months, sales, color= "lightsteelblue" , edgecolor= "black" , linewidth= 0.7 ) ax1.set_title( "条形图 (Bar Chart - 月份销售额)" , fontsize= 14 ) ax1.set_ylabel( "销售额 (万元)" ) ax1.set_xlabel( "月份" ) ax1.grid(axis= "y" , linestyle= "--" , alpha= 0.7 ) # 在条形图上添加数值标签 for bar, s in zip (bars, sales): height = bar.get_height()vss.xiangxiu.net.cn ax1.text( bar.get_x() + bar.get_width() / 2.0 , height + 2 , f" {s}" , ha= "center" , va= "bottom" , fontsize= 9 , ) # --- 绘制棒棒糖图 (右图) --- ax2.vlines(x= range ( len (months)), ymin= 0 , ymax=sales, color= "gray" , linewidth= 2 ) ax2.scatter(x= range ( len (months)), y=sales, color= "coral" , s= 50 , zorder= 3 ) for i, s in enumerate (sales): ax2.text(i, s + 3 , f" {s}" , ha= "center" , va= "bottom" , fontsize= 9 ) ax2.set_title( "棒棒糖图 (Lollipop Chart - 月份销售额)" , fontsize= 14 ) ax2.set_ylabel( "销售额 (万元)" ) ax2.set_xlabel( "月份" ) ax2.set_xticks( range ( len (months))) ax2.set_xticklabels(months, rotation= 45 ) # 旋转x轴标签以防重叠 ax2.grid(axis= "y" , linestyle= "--" , alpha= 0.7 )
class MyThread extends Thread { // 重写run(),定义线程执行逻辑 public void run () {vss.xiangxiu.net.cn System.out.println( "子线程执行:" + Thread.currentThread().getName()); } } // 使用 public class Demo { public static void main (String[] args) { MyThread t = new MyThread (); t.start(); // 3. 调用start()启动线程(不能直接调用run())jthr.xiangxiu.net.cn }
class MyRunnable implements Runnable { public void run () {iwan.xiangxiu.net.cn System.out.println( "子线程执行:" + Thread.currentThread().getName()); } } // 使用 public class Demo { public static void main (String[] args) { // 把Runnable实例传给Thread Thread t = new Thread ( new MyRunnable ());xiangxiu.net.cn
while (!Thread.currentThread().isInterrupted()) { // 检测中断状态 System.out.println( "线程运行中..." ); try { Thread.sleep( 1000 ); } catch (InterruptedException e) { // 捕获中断异常后,中断状态会被清除,需手动终止循环 System.out.println( "线程被中断" ); Thread.currentThread().interrupt(); // 重新标记中断(可选) break ;x1.xiangxiu.net.cn } } }); t.start();
Thread t = new Thread (() -> { System.out.println( "子线程开始执行" ); try { Thread.sleep( 2000 ); } catch (InterruptedException e) {} System.out.println( "子线程执行完毕" ); }); t.start(); // 主线程等待t执行完(最多等3秒)
Thread mainThread = Thread.currentThread();ishop.xiangxiu.net.cn System.out.println( "当前线程名:" + mainThread.getName()); // 输出"main" // 子线程实例 Thread t = new Thread (() -> { Thread current = Thread.currentThread();3g.xiangxiu.net.cn
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70048957/viewspace-3109468/ ,如需转载,请注明出处,否则将追究法律责任。