ITPub博客

首页 > 应用开发 > XML/XSL > 让我们通过实际的对比示例,直观地感受棒棒糖图与传统条形图的不同表现

让我们通过实际的对比示例,直观地感受棒棒糖图与传统条形图的不同表现

投稿 XML/XSL 作者:ecclw459 时间:2026-01-29 19:56:22 0 删除 编辑

创建更多类别的测试数据 # 模拟不同月份中每天的某个指标(例如:每日平均步数,单位:千步) 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(),定义线程执行逻辑     @Override     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 {     @Override     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


  • 程可能正在 CPU 上执行,也可能正在等待操作系统分配时间片。
  • 切换:
    • 就绪 -> 运行:获得 CPU 时间片。
    • 运行 -> 就绪:CPU 时间片用完,或主动调用 Thread.yield()。
  1. BLOCKED (阻塞) i01.xiangxiu.net.cn
  • 含义:线程正在等待获取一个排他锁(如进入synchronized 代码块/方法),但该锁目前被其他线程持有。
  • dbg.xiangxiu.net.cn  切换:
    • RUNNABLE -> BLOCKED:尝试进入 synchronized 区域失败。
    • BLOCKED -> RUNNABLE:其他线程释放锁,当前线程成功竞争到锁。
  1. WAITING (等待)
  • 含义:线程处于无限期的等待状态,不会被分配 CPU 时间,必须等待其他线程显式地唤醒。
  • 切换:
    • RUNNABLE -> WAITING:调用 Object.wait()(不带参数)、Thread.join()(不带参数)或 LockSupport.park()。
    • WAITING -> RUNNABLE:其他线程调用 Object.notify()、notifyAll() 或 LockSupport.unpark()。
  1. TIMED_WAITING (超时等待)mf.xiangxiu.net.cn
  • 含义:与 WAITING 类似,但在指定的时间后会自动唤醒,不需要其他线程显式唤醒。
  • 切换:
    • RUNNABLE -> TIMED_WAITING:调用 Thread.sleep(ms)、Object.wait(ms)、Thread.join(ms) 等带时间参数的方法。
    • TIMED_WAITING -> RUNNABLE:时间结束,或被提前唤醒(如 notify())。dir.xiangxiu.net.cn
  1. TERMINATED (终止)mse.xiangxiu.net.cn
  • 含义:线程已经执行完毕(run() 方法正常结束)或因异常退出了执行。
  • 切换:线程一旦进入此状态,生命周期结束,不可再次启动(再次调用 start() 会抛出异常)。





来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70048957/viewspace-3109468/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2026-01-07

  • 博文量
    108
  • 访问量
    102119